在Woocommerce中,当我添加捆绑的产品 plt.figure(1)
plt.subplot(121)
时,我要删除产品 A
, B
, C
(该捆绑包中已经存在的单个产品),并且如果产品 D
(捆绑包)在购物车中,不允许添加 A
, B
或 C
产品。
答案Woocomerce Remove a specific cart items when adding to cart another specific items与我想要的非常接近。
如何向后做?
答案 0 :(得分:2)
对于第一种情况,您确实可以使用woocommerce_add_to_cart
钩子;对于第二种情况,则最好使用woocommerce_add_to_cart_validation
钩子。
所以我们将两个条件同时应用,我们得到了
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Set ID's
$product_a = 30;
$product_b_c_d = array ( 813, 815, 817 );
// Get current product id
$product_id = $variation_id > 0 ? $variation_id : $product_id;
// Product A is added
if ( $product_a == $product_id ) {
// Loop trough cart items
foreach( WC()->cart->get_cart() as $key => $item ) {
// Product ID is in array
if ( in_array( $item['product_id'], $product_b_c_d ) ) {
// Remove cart item(s)
WC()->cart->remove_cart_item( $key );
}
}
// Optionaly displaying a notice
wc_add_notice( __( 'Product A is added, Product B, C or D has been removed from cart.', 'woocommperce' ), 'notice' );
}
// Product B, C or D is added
elseif ( in_array( $product_id, $product_b_c_d ) ) {
// Generate card id
$product_cart_id = WC()->cart->generate_cart_id( $product_a );
// Check if product A in the cart
$item_key = WC()->cart->find_product_in_cart( $product_cart_id );
// if item_key true
if ( $item_key ) {
// Optionaly displaying a notice
wc_add_notice( __( 'Product A is in cart, Product B, C or D are not allowed', 'woocommerce' ), 'error' );
$passed = false;
}
}
// Return
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
答案 1 :(得分:2)
对于特定的捆绑产品,以下代码将:
因此,每种情况的一个挂钩函数将是一个更轻松的过程:
// When the bunddle product is added to cart remove single components from cart
add_action( 'woocommerce_add_to_cart', 'on_bundled_product_added_to_cart', 10, 4 );
function on_bundled_product_added_to_cart( $cart_item_key, $product_id, $quantity, $variation_id ) {
// SETTINGS
$bundled_product_id = 83; // Set HERE your bundled product ID
$item_ids_to_remove = array(37, 53, 31); // Set HERE the product ID(s) to remove(s)
$removed_items = 0; // Initializing
// When the bundled product is added to cart
if( $bundled_product_id == $product_id ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $item_key => $cart_item ){
// Get the cart item keys of the items to be removed
if( array_intersect( array($cart_item['product_id'], $cart_item['variation_id']), $item_ids_to_remove ) ) {
WC()->cart->remove_cart_item($item_key);
$removed_items++;
}
}
}
// Optionaly displaying a notice for the removed items
if( ! empty($removed_item_names) ){
wc_add_notice( sprintf( __( 'Some products have been removed from cart as they are already bundled in your cart', 'woocommerce' ), $items_text ), 'notice' );
}
}
// Add to cart validation for bundled components
add_filter( 'woocommerce_add_to_cart_validation', 'check_cart_items_for_bundle_product', 9999, 4 );
function check_cart_items_for_bundle_product( $passed, $product_id, $quantity, $variation_id = 0 ) {
// SETTINGS
$bundled_product_id = 83; // Set HERE your bundled product ID
$bundled_items_ids = array(37, 53, 31); // Set HERE the bundled items ID(s) from the bundled product
$bundled_in_cart = false;
if( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// check for bundled product
if( $bundled_product_id == $cart_item['product_id'] ) {
$bundled_in_cart = true;
break;
}
}
if( $bundled_in_cart && array_intersect(array($product_id, $variation_id), $bundled_items_ids) ) {
// Add a custom notice
wc_add_notice( __( 'This product is already a component of the bundled product in your cart', 'woocommerce' ), 'error' );
return false;
}
}
return $passed;
}
代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。
当已将已捆绑产品的组件添加到购物车中时,尝试将其添加到购物车时出现消息错误:
捆绑产品的组件在购物车中并且添加父捆绑产品(在购物车页面中) 时将其删除的消息: