WooCommerce count active cart content for specifiy product
up vote
0
down vote
favorite
I know I can count the number of items a user has in his active cart with WC()->cart->get_cart_contents_count()
. But that only counts the number of items the current user has in his cart.
I want to display how often a specific product is currently added to the carts of all users in sum including users with an account AND guest users. What I want to achieve ist an output like "X customers have Product Y currently in their carts."
Therefore I think I need to collect the data from woocommerce_sessions table because in usermeta table only the needed values for users with an account are stored.
I tried with $array = $wpdb->get_results("select session_value from ".$wpdb->prefix."woocommerce_sessions");
which gives me all informations but the array looks like this:
Array ( [0] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:412:"a:1:{s:32:"2838023a778dfaecdc212708f721b788";a:11:{s:3:"key";s:32:"2838023a778dfaecdc212708f721b788";s:10:"product_id";i:51;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:100;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:100;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:408:"a:15:{s:8:"subtotal";s:6:"100.00";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:6:"100.00";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:6:"100.00";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:417:"a:2:{s:12:"package_hash";s:40:"wc_ship_56551d06e63bb366b9363da1321e7c51";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:26:"Pink Down Jacket × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";} ) [1] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:415:"a:1:{s:32:"0266e33d3f546cb5436a10798e657d97";a:11:{s:3:"key";s:32:"0266e33d3f546cb5436a10798e657d97";s:10:"product_id";i:245;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:89.9;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:89.9;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:405:"a:15:{s:8:"subtotal";s:5:"89.90";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:5:"89.90";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:5:"89.90";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:426:"a:2:{s:12:"package_hash";s:40:"wc_ship_5780f5fbceeddc4ac9f9e5a17c60abdd";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:35:"Shiny brown Winter Jacket × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";} ) [2] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:413:"a:1:{s:32:"a4a042cf4fd6bfb47701cbc8a1653ada";a:11:{s:3:"key";s:32:"a4a042cf4fd6bfb47701cbc8a1653ada";s:10:"product_id";i:171;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:180;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:180;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:408:"a:15:{s:8:"subtotal";s:6:"180.00";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:6:"180.00";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:6:"180.00";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:421:"a:2:{s:12:"package_hash";s:40:"wc_ship_f39e348e171105f3b846f45ad82bcaa8";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:30:"Red Dress with black × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";}
I tried to unserialize this output because I need to count how often [product_id] is special value (e.g. 51, in the code above it's written as s:10:"product_id";i:51;
) but I cannot get it work. So question is how can I determinate in the array shown shown above how often [product_id] === 51
?
I managed it only for one key but I need it for the whole array and although (for that single key) I got an output that looks promising like this Array ( [2838023a778dfaecdc212708f721b788] => Array ( [key] => 2838023a778dfaecdc212708f721b788 [product_id] => 51 [variation_id] => 0 [variation] => Array ( ) [quantity] => 1 [data_hash] => b5c1d5ca8bae6d4896cf1807cdf763f0 [line_tax_data] => Array ( [subtotal] => Array ( ) [total] => Array ( ) ) [line_subtotal] => 100 [line_subtotal_tax] => 0 [line_total] => 100 [line_tax] => 0 ) )
I cannot count.
$count = 0;
foreach ($array2 as $item) {
if ($item->product_id === '51') {
$count++;
}
}
print_r ($count);
gives me a 0 although it should be 1.
So I have two problem I think: How can I complete unserialize a multidimensional array and how can I count the occurrence of a certain value in that array.
Any help would be much appreciated.
Thanks in advance.
php wordpress multidimensional-array woocommerce key-value
add a comment |
up vote
0
down vote
favorite
I know I can count the number of items a user has in his active cart with WC()->cart->get_cart_contents_count()
. But that only counts the number of items the current user has in his cart.
I want to display how often a specific product is currently added to the carts of all users in sum including users with an account AND guest users. What I want to achieve ist an output like "X customers have Product Y currently in their carts."
Therefore I think I need to collect the data from woocommerce_sessions table because in usermeta table only the needed values for users with an account are stored.
I tried with $array = $wpdb->get_results("select session_value from ".$wpdb->prefix."woocommerce_sessions");
which gives me all informations but the array looks like this:
Array ( [0] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:412:"a:1:{s:32:"2838023a778dfaecdc212708f721b788";a:11:{s:3:"key";s:32:"2838023a778dfaecdc212708f721b788";s:10:"product_id";i:51;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:100;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:100;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:408:"a:15:{s:8:"subtotal";s:6:"100.00";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:6:"100.00";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:6:"100.00";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:417:"a:2:{s:12:"package_hash";s:40:"wc_ship_56551d06e63bb366b9363da1321e7c51";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:26:"Pink Down Jacket × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";} ) [1] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:415:"a:1:{s:32:"0266e33d3f546cb5436a10798e657d97";a:11:{s:3:"key";s:32:"0266e33d3f546cb5436a10798e657d97";s:10:"product_id";i:245;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:89.9;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:89.9;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:405:"a:15:{s:8:"subtotal";s:5:"89.90";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:5:"89.90";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:5:"89.90";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:426:"a:2:{s:12:"package_hash";s:40:"wc_ship_5780f5fbceeddc4ac9f9e5a17c60abdd";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:35:"Shiny brown Winter Jacket × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";} ) [2] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:413:"a:1:{s:32:"a4a042cf4fd6bfb47701cbc8a1653ada";a:11:{s:3:"key";s:32:"a4a042cf4fd6bfb47701cbc8a1653ada";s:10:"product_id";i:171;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:180;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:180;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:408:"a:15:{s:8:"subtotal";s:6:"180.00";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:6:"180.00";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:6:"180.00";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:421:"a:2:{s:12:"package_hash";s:40:"wc_ship_f39e348e171105f3b846f45ad82bcaa8";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:30:"Red Dress with black × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";}
I tried to unserialize this output because I need to count how often [product_id] is special value (e.g. 51, in the code above it's written as s:10:"product_id";i:51;
) but I cannot get it work. So question is how can I determinate in the array shown shown above how often [product_id] === 51
?
I managed it only for one key but I need it for the whole array and although (for that single key) I got an output that looks promising like this Array ( [2838023a778dfaecdc212708f721b788] => Array ( [key] => 2838023a778dfaecdc212708f721b788 [product_id] => 51 [variation_id] => 0 [variation] => Array ( ) [quantity] => 1 [data_hash] => b5c1d5ca8bae6d4896cf1807cdf763f0 [line_tax_data] => Array ( [subtotal] => Array ( ) [total] => Array ( ) ) [line_subtotal] => 100 [line_subtotal_tax] => 0 [line_total] => 100 [line_tax] => 0 ) )
I cannot count.
$count = 0;
foreach ($array2 as $item) {
if ($item->product_id === '51') {
$count++;
}
}
print_r ($count);
gives me a 0 although it should be 1.
So I have two problem I think: How can I complete unserialize a multidimensional array and how can I count the occurrence of a certain value in that array.
Any help would be much appreciated.
Thanks in advance.
php wordpress multidimensional-array woocommerce key-value
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I know I can count the number of items a user has in his active cart with WC()->cart->get_cart_contents_count()
. But that only counts the number of items the current user has in his cart.
I want to display how often a specific product is currently added to the carts of all users in sum including users with an account AND guest users. What I want to achieve ist an output like "X customers have Product Y currently in their carts."
Therefore I think I need to collect the data from woocommerce_sessions table because in usermeta table only the needed values for users with an account are stored.
I tried with $array = $wpdb->get_results("select session_value from ".$wpdb->prefix."woocommerce_sessions");
which gives me all informations but the array looks like this:
Array ( [0] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:412:"a:1:{s:32:"2838023a778dfaecdc212708f721b788";a:11:{s:3:"key";s:32:"2838023a778dfaecdc212708f721b788";s:10:"product_id";i:51;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:100;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:100;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:408:"a:15:{s:8:"subtotal";s:6:"100.00";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:6:"100.00";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:6:"100.00";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:417:"a:2:{s:12:"package_hash";s:40:"wc_ship_56551d06e63bb366b9363da1321e7c51";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:26:"Pink Down Jacket × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";} ) [1] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:415:"a:1:{s:32:"0266e33d3f546cb5436a10798e657d97";a:11:{s:3:"key";s:32:"0266e33d3f546cb5436a10798e657d97";s:10:"product_id";i:245;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:89.9;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:89.9;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:405:"a:15:{s:8:"subtotal";s:5:"89.90";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:5:"89.90";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:5:"89.90";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:426:"a:2:{s:12:"package_hash";s:40:"wc_ship_5780f5fbceeddc4ac9f9e5a17c60abdd";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:35:"Shiny brown Winter Jacket × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";} ) [2] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:413:"a:1:{s:32:"a4a042cf4fd6bfb47701cbc8a1653ada";a:11:{s:3:"key";s:32:"a4a042cf4fd6bfb47701cbc8a1653ada";s:10:"product_id";i:171;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:180;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:180;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:408:"a:15:{s:8:"subtotal";s:6:"180.00";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:6:"180.00";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:6:"180.00";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:421:"a:2:{s:12:"package_hash";s:40:"wc_ship_f39e348e171105f3b846f45ad82bcaa8";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:30:"Red Dress with black × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";}
I tried to unserialize this output because I need to count how often [product_id] is special value (e.g. 51, in the code above it's written as s:10:"product_id";i:51;
) but I cannot get it work. So question is how can I determinate in the array shown shown above how often [product_id] === 51
?
I managed it only for one key but I need it for the whole array and although (for that single key) I got an output that looks promising like this Array ( [2838023a778dfaecdc212708f721b788] => Array ( [key] => 2838023a778dfaecdc212708f721b788 [product_id] => 51 [variation_id] => 0 [variation] => Array ( ) [quantity] => 1 [data_hash] => b5c1d5ca8bae6d4896cf1807cdf763f0 [line_tax_data] => Array ( [subtotal] => Array ( ) [total] => Array ( ) ) [line_subtotal] => 100 [line_subtotal_tax] => 0 [line_total] => 100 [line_tax] => 0 ) )
I cannot count.
$count = 0;
foreach ($array2 as $item) {
if ($item->product_id === '51') {
$count++;
}
}
print_r ($count);
gives me a 0 although it should be 1.
So I have two problem I think: How can I complete unserialize a multidimensional array and how can I count the occurrence of a certain value in that array.
Any help would be much appreciated.
Thanks in advance.
php wordpress multidimensional-array woocommerce key-value
I know I can count the number of items a user has in his active cart with WC()->cart->get_cart_contents_count()
. But that only counts the number of items the current user has in his cart.
I want to display how often a specific product is currently added to the carts of all users in sum including users with an account AND guest users. What I want to achieve ist an output like "X customers have Product Y currently in their carts."
Therefore I think I need to collect the data from woocommerce_sessions table because in usermeta table only the needed values for users with an account are stored.
I tried with $array = $wpdb->get_results("select session_value from ".$wpdb->prefix."woocommerce_sessions");
which gives me all informations but the array looks like this:
Array ( [0] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:412:"a:1:{s:32:"2838023a778dfaecdc212708f721b788";a:11:{s:3:"key";s:32:"2838023a778dfaecdc212708f721b788";s:10:"product_id";i:51;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:100;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:100;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:408:"a:15:{s:8:"subtotal";s:6:"100.00";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:6:"100.00";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:6:"100.00";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:417:"a:2:{s:12:"package_hash";s:40:"wc_ship_56551d06e63bb366b9363da1321e7c51";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:26:"Pink Down Jacket × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";} ) [1] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:415:"a:1:{s:32:"0266e33d3f546cb5436a10798e657d97";a:11:{s:3:"key";s:32:"0266e33d3f546cb5436a10798e657d97";s:10:"product_id";i:245;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:89.9;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:89.9;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:405:"a:15:{s:8:"subtotal";s:5:"89.90";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:5:"89.90";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:5:"89.90";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:426:"a:2:{s:12:"package_hash";s:40:"wc_ship_5780f5fbceeddc4ac9f9e5a17c60abdd";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:35:"Shiny brown Winter Jacket × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";} ) [2] => stdClass Object ( [session_value] => a:12:{s:4:"cart";s:413:"a:1:{s:32:"a4a042cf4fd6bfb47701cbc8a1653ada";a:11:{s:3:"key";s:32:"a4a042cf4fd6bfb47701cbc8a1653ada";s:10:"product_id";i:171;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:9:"data_hash";s:32:"b5c1d5ca8bae6d4896cf1807cdf763f0";s:13:"line_tax_data";a:2:{s:8:"subtotal";a:0:{}s:5:"total";a:0:{}}s:13:"line_subtotal";d:180;s:17:"line_subtotal_tax";i:0;s:10:"line_total";d:180;s:8:"line_tax";i:0;}}";s:11:"cart_totals";s:408:"a:15:{s:8:"subtotal";s:6:"180.00";s:12:"subtotal_tax";d:0;s:14:"shipping_total";s:4:"0.00";s:12:"shipping_tax";d:0;s:14:"shipping_taxes";a:0:{}s:14:"discount_total";d:0;s:12:"discount_tax";d:0;s:19:"cart_contents_total";s:6:"180.00";s:17:"cart_contents_tax";d:0;s:19:"cart_contents_taxes";a:0:{}s:9:"fee_total";s:4:"0.00";s:7:"fee_tax";d:0;s:9:"fee_taxes";a:0:{}s:5:"total";s:6:"180.00";s:9:"total_tax";d:0;}";s:15:"applied_coupons";s:6:"a:0:{}";s:22:"coupon_discount_totals";s:6:"a:0:{}";s:26:"coupon_discount_tax_totals";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:22:"shipping_for_package_0";s:421:"a:2:{s:12:"package_hash";s:40:"wc_ship_f39e348e171105f3b846f45ad82bcaa8";s:5:"rates";a:1:{s:20:"legacy_free_shipping";O:16:"WC_Shipping_Rate":2:{s:7:"*data";a:6:{s:2:"id";s:20:"legacy_free_shipping";s:9:"method_id";s:20:"legacy_free_shipping";s:11:"instance_id";i:0;s:5:"label";s:13:"Free Shipping";s:4:"cost";s:4:"0.00";s:5:"taxes";a:0:{}}s:12:"*meta_data";a:1:{s:5:"Items";s:30:"Red Dress with black × 1";}}}}";s:25:"previous_shipping_methods";s:48:"a:1:{i:0;a:1:{i:0;s:20:"legacy_free_shipping";}}";s:23:"chosen_shipping_methods";s:38:"a:1:{i:0;s:20:"legacy_free_shipping";}";s:22:"shipping_method_counts";s:14:"a:1:{i:0;i:1;}";s:10:"wc_notices";N;s:8:"customer";s:687:"a:26:{s:2:"id";s:1:"0";s:13:"date_modified";s:0:"";s:8:"postcode";s:0:"";s:4:"city";s:0:"";s:9:"address_1";s:0:"";s:7:"address";s:0:"";s:9:"address_2";s:0:"";s:5:"state";s:0:"";s:7:"country";s:2:"DE";s:17:"shipping_postcode";s:0:"";s:13:"shipping_city";s:0:"";s:18:"shipping_address_1";s:0:"";s:16:"shipping_address";s:0:"";s:18:"shipping_address_2";s:0:"";s:14:"shipping_state";s:0:"";s:16:"shipping_country";s:2:"DE";s:13:"is_vat_exempt";s:0:"";s:19:"calculated_shipping";s:0:"";s:10:"first_name";s:0:"";s:9:"last_name";s:0:"";s:7:"company";s:0:"";s:5:"phone";s:0:"";s:5:"email";s:0:"";s:19:"shipping_first_name";s:0:"";s:18:"shipping_last_name";s:0:"";s:16:"shipping_company";s:0:"";}";}
I tried to unserialize this output because I need to count how often [product_id] is special value (e.g. 51, in the code above it's written as s:10:"product_id";i:51;
) but I cannot get it work. So question is how can I determinate in the array shown shown above how often [product_id] === 51
?
I managed it only for one key but I need it for the whole array and although (for that single key) I got an output that looks promising like this Array ( [2838023a778dfaecdc212708f721b788] => Array ( [key] => 2838023a778dfaecdc212708f721b788 [product_id] => 51 [variation_id] => 0 [variation] => Array ( ) [quantity] => 1 [data_hash] => b5c1d5ca8bae6d4896cf1807cdf763f0 [line_tax_data] => Array ( [subtotal] => Array ( ) [total] => Array ( ) ) [line_subtotal] => 100 [line_subtotal_tax] => 0 [line_total] => 100 [line_tax] => 0 ) )
I cannot count.
$count = 0;
foreach ($array2 as $item) {
if ($item->product_id === '51') {
$count++;
}
}
print_r ($count);
gives me a 0 although it should be 1.
So I have two problem I think: How can I complete unserialize a multidimensional array and how can I count the occurrence of a certain value in that array.
Any help would be much appreciated.
Thanks in advance.
php wordpress multidimensional-array woocommerce key-value
php wordpress multidimensional-array woocommerce key-value
asked Nov 9 at 23:08
wsc-support
13
13
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234404%2fwoocommerce-count-active-cart-content-for-specifiy-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234404%2fwoocommerce-count-active-cart-content-for-specifiy-product%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown