根据 Woocommerce 中的产品数量替换特定的购物车项目
Replace specific cart item based on product quantity in Woocommerce
我正在尝试根据购物车中的产品数量更改购物车内容。我店里只有 5 件商品:
- Product_1 => 1 个面板,
- Product_2 => 12 个面板,
- Product_3 => 18 个面板,
- Product_4 => 30 个面板,
- Product_5 => 60 个面板,
它们被配置为不同的产品,因此没有捆绑包、套件或任何东西。多一块面板的产品,显然比单独加装多块单面板要便宜。
然后我还有一个定制产品,它根据客户提供的地板尺寸计算所需的面板数量,然后将必要数量的单面板添加到购物车。
我想在审核时动态更改购物车的内容。
因此 例如,如果地板配置器计算 54 个单板 并将它们添加到购物车,我想更改购物车项目:
- 1 Product_4(30 个面板)
- 1 Product_2(12 个面板)
- 2 Product_1(1 个面板)
- 并打印一条说明更改的消息。
我检查了不同的解决方案,但 none 提供了这种功能:
- How to bundle products with WooCommerce
- WooCommerce Product Bundles - Bulk Discounts
- WooCommerce Dynamic Pricing & Discounts
因此,基于 ,我认为我需要使用 hook woocommerce_before_calculate_totals
并实现逻辑,但需要一些帮助,因为我是一个 wordpress 开发新手。
请注意,在楼层配置器中使用 Ajax 调用添加到购物车按钮:
jQuery("button.ajax_add_to_cart").attr("data-quantity", parseInt(numLozasrealW * numLozasrealH));
感谢任何帮助。
下面的代码应该可以解决问题,它将用您的其他产品替换自定义产品(数量)。您将必须设置您的自定义产品 ID、您的 5 个普通产品 ID 和您的自定义文本通知。
代码:
add_action( 'woocommerce_add_to_cart', 'action_before_cart', 20, 3 );
function action_before_cart( $cart_item_key, $product_id, $quantity ) {
// HERE set your specific product ID
$specific_product_id = 862;
if( $product_id != $specific_product_id ) return;
// HERE set your other product IDs related to panels
$products_panels = array(
861 => 50, // Product ID => for 50 panels
860 => 30, // Product ID => for 30 panels
859 => 18, // Product ID => for 18 panels
858 => 12, // Product ID => for 12 panels
857 => 1, // Product ID => for 1 panel
);
// Loop through all "panels" products IDs
foreach ( $products_panels as $product_id => $panels ) {
if( $quantity >= $panels ){
$qty = floor($quantity / $panels);
$quantity = $quantity % $panels;
$data = 'Panels: '.$panels.' | Quantity: '.$qty.' | Remain: '.$quantity;
print_pr($data);
WC()->cart->add_to_cart( $product_id, $qty );
}
}
// Removing the specific product
WC()->cart->remove_cart_item( $cart_item_key );
// HERE set your custom notice text
wc_add_notice('Here your custom notice text', 'notice');
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
正如您在下面看到的,当自定义产品添加到购物车时,它被其他产品替换以匹配面板数量并显示自定义通知:
我正在尝试根据购物车中的产品数量更改购物车内容。我店里只有 5 件商品:
- Product_1 => 1 个面板,
- Product_2 => 12 个面板,
- Product_3 => 18 个面板,
- Product_4 => 30 个面板,
- Product_5 => 60 个面板,
它们被配置为不同的产品,因此没有捆绑包、套件或任何东西。多一块面板的产品,显然比单独加装多块单面板要便宜。
然后我还有一个定制产品,它根据客户提供的地板尺寸计算所需的面板数量,然后将必要数量的单面板添加到购物车。
我想在审核时动态更改购物车的内容。
因此 例如,如果地板配置器计算 54 个单板 并将它们添加到购物车,我想更改购物车项目:
- 1 Product_4(30 个面板)
- 1 Product_2(12 个面板)
- 2 Product_1(1 个面板)
- 并打印一条说明更改的消息。
我检查了不同的解决方案,但 none 提供了这种功能:
- How to bundle products with WooCommerce
- WooCommerce Product Bundles - Bulk Discounts
- WooCommerce Dynamic Pricing & Discounts
因此,基于 woocommerce_before_calculate_totals
并实现逻辑,但需要一些帮助,因为我是一个 wordpress 开发新手。
请注意,在楼层配置器中使用 Ajax 调用添加到购物车按钮:
jQuery("button.ajax_add_to_cart").attr("data-quantity", parseInt(numLozasrealW * numLozasrealH));
感谢任何帮助。
下面的代码应该可以解决问题,它将用您的其他产品替换自定义产品(数量)。您将必须设置您的自定义产品 ID、您的 5 个普通产品 ID 和您的自定义文本通知。
代码:
add_action( 'woocommerce_add_to_cart', 'action_before_cart', 20, 3 );
function action_before_cart( $cart_item_key, $product_id, $quantity ) {
// HERE set your specific product ID
$specific_product_id = 862;
if( $product_id != $specific_product_id ) return;
// HERE set your other product IDs related to panels
$products_panels = array(
861 => 50, // Product ID => for 50 panels
860 => 30, // Product ID => for 30 panels
859 => 18, // Product ID => for 18 panels
858 => 12, // Product ID => for 12 panels
857 => 1, // Product ID => for 1 panel
);
// Loop through all "panels" products IDs
foreach ( $products_panels as $product_id => $panels ) {
if( $quantity >= $panels ){
$qty = floor($quantity / $panels);
$quantity = $quantity % $panels;
$data = 'Panels: '.$panels.' | Quantity: '.$qty.' | Remain: '.$quantity;
print_pr($data);
WC()->cart->add_to_cart( $product_id, $qty );
}
}
// Removing the specific product
WC()->cart->remove_cart_item( $cart_item_key );
// HERE set your custom notice text
wc_add_notice('Here your custom notice text', 'notice');
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
正如您在下面看到的,当自定义产品添加到购物车时,它被其他产品替换以匹配面板数量并显示自定义通知: