【发布时间】:2021-02-08 19:57:48
【问题描述】:
我们正在研究一种解决方案,以按供应商商店名称对购物车中的产品进行分类和结帐。我们在这里找到了这个:Woocommerce sort cart products by product category,但我们必须为我们的案例扩展这个代码。每个产品都有一个供应商。所以我们不必检查产品是否没有供应商。
我不确定如何以正确的方式输出数组。我可以这样吗?
add_action( 'woocommerce_cart_loaded_from_session', function() {
global $woocommerce;
// Build product array
$products_in_cart = array();
// Loop through cart items
foreach ( $woocommerce->cart->cart_contents as $key => $item ) {
// Get product object
$product = $item->get_product();
// Author id
$author_id = $product->post->post_author;
// Shopname
$vendor = dokan()->vendor->get( $author_id );
$shop_name = $vendor->get_shop_name();
$products_in_cart[ $key ] = $shop_name[0]->name;
}
ksort( $products_in_cart );
// Build cart array
$cart_contents = array();
foreach ( $products_in_cart as $cart_key => $Vendor_store ) {
$cart_contents[ $cart_key ] = $woocommerce->cart->cart_contents[ $cart_key ];
}
// Output sorted cart
$woocommerce->cart->cart_contents = $cart_contents;
}, 100 );
【问题讨论】:
标签: php wordpress woocommerce cart dokan