Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当捆绑产品位于购物车中时,请从Woocommerce中的购物车中删除子组件_Php_Wordpress_Woocommerce_Bundle_Cart - Fatal编程技术网

Php 当捆绑产品位于购物车中时,请从Woocommerce中的购物车中删除子组件

Php 当捆绑产品位于购物车中时,请从Woocommerce中的购物车中删除子组件,php,wordpress,woocommerce,bundle,cart,Php,Wordpress,Woocommerce,Bundle,Cart,在Woocommerce中,当我添加捆绑产品$item){ //产品ID在数组中 if(在数组中($item['product\u id',$product\u b\u c\d)){ //删除购物车项目 WC()->购物车->删除购物车项目($key); } } //选择性地显示通知 wc_添加_通知(uuu(‘产品A已添加,产品B、C或D已从购物车中移除’、‘Woocomperce’、‘通知’); } //添加产品B、C或D elseif(在数组中($product_id,$pr

在Woocommerce中,当我添加捆绑产品
时,我想删除产品
D
(已经在捆绑包中的单个产品),如果产品
(捆绑包)在购物车中,不允许添加
B
C
D
产品

答案与我想要的非常接近


如何反向执行?

对于第一个条件,您确实可以使用
woocommerce\u add\u to\u cart
钩子,对于第二个条件,您最好使用
woocommerce\u add\u to\u cart\u验证
钩子

所以我们将这两个条件一起应用,得到

function filter\u woo commerce\u add\u to\u cart\u validation($passed,$product\u id,$quantity,$variation\u id=null,$variations=null){
//设置ID
$product_a=30;
$product_b_c_d=数组(813,815,817);
//获取当前产品id
$product\U id=$VARIANCE\U id>0?$VARIANCE\U id:$product\U id;
//添加产品A
如果($product\U a==$product\U id){
//循环槽购物车项目
foreach(WC()->cart->get_cart()作为$key=>$item){
//产品ID在数组中
if(在数组中($item['product\u id',$product\u b\u c\d)){
//删除购物车项目
WC()->购物车->删除购物车项目($key);
}       
}
//选择性地显示通知
wc_添加_通知(uuu(‘产品A已添加,产品B、C或D已从购物车中移除’、‘Woocomperce’、‘通知’);
}
//添加产品B、C或D
elseif(在数组中($product_id,$product_b_c_d)){
//生成卡id
$product\U cart\U id=WC()->cart->生成\U cart\U id($product\U a);
//检查购物车中是否有产品A
$item\u key=WC()->购物车->在购物车中查找商品($product\u cart\u id);
//如果项_键为true
如果($item\u key){
//选择性地显示通知
wc_添加_通知(uuu(‘产品A在购物车中,不允许使用产品B、C或D’、‘woocommerce’、‘error’));
$passed=false;
}
}
//返回
返回$passed;
}
添加过滤器(“woocommerce\u添加到购物车\u验证”、“filter\u woocommerce\u添加到购物车\u验证”,10,5);

对于特定的捆绑产品,以下代码将:

  • 将捆绑产品添加到购物车时,删除任何单个子产品
  • 当捆绑产品已在购物车中时,避免将单个子产品添加到购物车中
因此,对于每种情况,一个挂钩函数将是一个更轻的过程:

// 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文件。测试和工作


当捆绑产品的组件已在购物车中时,尝试将其添加到购物车时出现错误消息:


捆绑产品的组件在购物车中并且在添加父捆绑产品时将其删除(在购物车页面中)时的消息:


谢谢!很好用!只有一个问题:如果捆绑包(A)已经在购物车中,并且我尝试添加B、C或D,它将重新加载页面,并且不显示任何内容。如果产品已作为捆绑包存在购物车中,是否有办法不重新加载页面?或者更简单地说,在结帐时强制重定向并显示“错误”(“此产品已经是您购物车中捆绑产品的一个组件”)?如果我做相反的操作,将B、C或D添加到购物车,然后添加一个(捆绑包),它可以正常工作,因为它可以将其添加到购物车,而无需重新加载页面,非常感谢much@SimoneGatti我已经轻轻地更新了我的代码,因为有一个错误。我添加了一些截图。再试一次。当我在购物车中有捆绑包(A)并添加单个产品(B、C、D)时,使用“return false”,页面将重新加载。我如何避免B C D产品(如果捆绑包在购物车中)而不重新加载页面?如果这是不可能的,这将是足够的,我能够直接发送到结帐,而不是重新加载同一个页面,这是一个有点烦人的担心,我试图解决它,因为这个原因,我没有回答了。我不想不必要地打扰你。今天我决定解答你的代码,这样它也可以帮助其他人。对不起,打扰你了。无论如何,谢谢你的帮助!如果我在购物车中有一个B C D,它会删除它们并添加一个(捆绑包),然后它会工作,但是如果我在购物车中有一个(捆绑包)并添加B C D,它不会工作,因为它会将它们添加到购物车中,尽管它们已经在捆绑包中。为什么会这样?(我正在使用官方的woocommerce产品包插件)