【发布时间】:2021-08-29 11:25:56
【问题描述】:
想法:
客户将一个盒子放入购物车(四种不同的简单产品)。它们每盒可容纳 4、9、16 或 24 件。
然后客户开始选择要放入盒子的产品。这些产品应该来自混搭产品标签。如果不是,它们将被单独包装,但更重要的是,它们不会被视为混搭产品。
我不知道该怎么做:
该功能需要统计购物车中有多少个盒子,并自动计算可以添加多少件。
整体示例:
客户添加可容纳 4 件的盒子和可容纳 16 件的盒子。客户现在总共可以将 20 种混搭产品添加到购物车中。
如果客户未将 20 种混合搭配产品添加到购物车,则会显示一条消息。如果客户将超过 20 种混合搭配产品添加到他们的购物车中,则会显示不同的消息。
以下是一些消息示例:
“您已添加适合XX件的盒子。请在您的购物车中添加额外的XX件混搭产品。注意:非混搭产品将单独包装在玻璃纸袋中。”
“您已添加 XX 盒。您可以添加 XX 件。请在您的购物车中添加额外的 XX 混搭产品。注意:非混搭产品将单独包装在玻璃纸袋中。”
这是我需要帮助修改的代码:
add_action('woocommerce_before_shop_loop', 'number_of_pieces_in_boxes', 1, 1 );
add_action('woocommerce_before_add_to_cart_form', 'number_of_pieces_in_boxes', 1, 1 );
function number_of_pieces_in_boxes( $cart ) {
if ( is_admin() && !defined( 'DOING_AJAX' ) ) return;
global $product;
$product_count = WC()->cart->cart_contents_count;
$product_ids = array( 1718, 1719, 1720, 1721 );
$fits_four = '4';
$fits_nine = '9';
$fits_sixteen = '16';
$fits_twentyfour = '24';
$piece_slug = 'mix-and-match';
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( in_array( $product_in_cart, $product_ids ) && has_term( 'mix-and-match', 'product_tag', $product_in_cart, $product_ids ) ) {
$in_cart = true;
break;
}
}
if ( ! $in_cart ) {
echo '<div class="product-count">You have '.$product_count.' pieces in your cart whereof none fits inside a box. We will pack and wrap them for you.</div>';
} else {
echo '<div class="product-count">You have '.$product_count.' pieces in your cart.</div>';
}
}
我真的很感激我能在这方面得到任何帮助。
【问题讨论】:
标签: php wordpress woocommerce cart