WooCommerce统计指定标签商品购物车数量 计算搭配盒子可装容量
需求背景
- 客户将盒子加入购物车,共有4款不同的简单产品,单盒可容纳数量分别为4件、9件、16件、24件。
- 客户后续选择装入盒子的商品需带有mix-and-match产品标签,非该标签商品将单独打包,不计入mix-and-match商品统计范围。
待实现功能
编写函数统计购物车内盒子总数,自动计算可添加的mix-and-match商品总容量上限,根据当前搭配商品数量输出对应提示。
场景示例
- 客户同时加入可装4件的盒子和可装16件的盒子,总计可添加20件mix-and-match标签商品到购物车。
- 若客户添加的mix-and-match商品不足20件,展示补量提示;若超出20件,展示超量提示。
提示文案参考
"您已添加可容纳XX件商品的盒子,请再添加XX件mix-and-match搭配商品。注意:非搭配商品将单独使用玻璃纸袋打包。"
"您已添加XX个盒子,总计可容纳XX件商品,请再添加XX件mix-and-match搭配商品。注意:非搭配商品将单独使用玻璃纸袋打包。"
现有待修改代码
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>'; } }
代码修改方案
修改后代码直接兼容需求,内置盒子ID和容量的映射关系,自动统计盒子总数、总容量、搭配商品数量,按场景输出对应中文提示:
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; // 配置项:盒子产品ID对应单盒容量 $box_capacity_map = [ 1718 => 4, 1719 => 9, 1720 => 16, 1721 => 24 ]; $mix_tag_slug = 'mix-and-match'; $total_box_count = 0; $total_capacity = 0; $mix_product_count = 0; // 遍历购物车统计数据 foreach( WC()->cart->get_cart() as $cart_item ) { $pid = $cart_item['product_id']; $qty = $cart_item['quantity']; // 统计盒子相关数据 if (isset($box_capacity_map[$pid])) { $total_box_count += $qty; $total_capacity += $box_capacity_map[$pid] * $qty; continue; } // 统计mix-and-match标签商品数量 if (has_term($mix_tag_slug, 'product_tag', $pid)) { $mix_product_count += $qty; } } echo '<div class="product-count">'; // 无盒子场景 if ($total_box_count == 0) { echo '您的购物车内暂无搭配盒子,所有商品将单独使用玻璃纸袋打包。'; } else { $diff = $total_capacity - $mix_product_count; if ($diff > 0) { // 容量剩余提示 echo sprintf('您已添加%d个盒子,总计可容纳%d件商品,请再添加%d件%s搭配商品。注意:非搭配商品将单独使用玻璃纸袋打包。', $total_box_count, $total_capacity, $diff, $mix_tag_slug); } elseif ($diff == 0) { echo sprintf('您已添加的%d件%s搭配商品刚好装满所有盒子,非搭配商品将单独使用玻璃纸袋打包。', $mix_product_count, $mix_tag_slug); } else { // 超量提示 echo sprintf('警告:您已添加的%s搭配商品超出盒子总容量%d件,超出部分将单独使用玻璃纸袋打包。', $mix_tag_slug, abs($diff)); } } echo '</div>'; }
内容的提问来源于stack exchange,提问作者user14906105
相关产品推荐
相关产品推荐

