WooCommerce计算特定产品的有效购物车内容

时间:2018-11-09 23:08:13

标签: php wordpress multidimensional-array woocommerce key-value

我知道我可以使用WC()->cart->get_cart_contents_count()计算用户在其活动购物车中拥有的商品数量。但这仅计算当前用户购物车中的商品数。 我想显示当前将特定产品添加到所有用户的购物车中的频率,包括具有帐户的用户和访客用户的总和。我想要实现的输出是“ X个客户的购物车中当前有产品Y”。

因此,我认为我需要从woocommerce_sessions表中收集数据,因为在usermeta表中仅存储具有帐户用户的所需值。

我尝试使用$array = $wpdb->get_results("select session_value from ".$wpdb->prefix."woocommerce_sessions");,该方法可以提供所有信息,但数组如下所示:

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:"";}";} 

我试图对该输出进行反序列化,因为我需要计算[product_id]是特殊值的频率(例如,在上面的代码中,{51}写为s:10:"product_id";i:51;),但是我无法使其正常工作。所以问题是如何确定上面显示的数组[product_id] === 51多久一次?

我仅对一个键进行管理,但对整个数组却需要它,尽管(对于单个键)我得到的输出看起来像这样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 ) ),我无法计数。

    $count = 0;
foreach ($array2 as $item) {
  if ($item->product_id === '51') {
    $count++;
  }
}
print_r ($count);

给我一​​个0,尽管应该是1。

所以我有两个问题:如何完成对多维数组的反序列化以及如何计算该数组中某个值的出现。

任何帮助将不胜感激。 预先感谢。

0 个答案:

没有答案