需要Woocommerce只允许购物车中的1个产品。如果产品已经在购物车中并且另外添加了1,那么它应该删除之前的1

时间:2022-10-25 09:56:47

I think this code should work but not exactly sure where to place it. Everywhere I have tried has failed so far...

我认为这段代码应该可以工作,但不能确定在哪里放置它。我到过的每个地方到目前为止都失败了......

add_action('init', 'woocommerce_clear_cart');

function woocommerce_clear_cart() {
global $woocommerce, $post, $wpdb;

$url = explode('/', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$slug=$url[4];
$postid = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status='publish' AND post_name = '$slug'");

    if ($postid){
        if ($postid == PRODUCTID1 || $postid == PRODUCTID2){
        $woocommerce->cart->empty_cart();
        }
    }

}

8 个解决方案

#1


37  

Unfortunately there is no 'action' hook before WooCommerce adds an item to the cart. But they have a 'filter' hook before adding to cart. That is how I use it:

不幸的是,在WooCommerce将商品添加到购物车之前,没有“操作”挂钩。但是在添加到购物车之前他们有一个“过滤器”钩子。这就是我使用它的方式:

add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );

function woo_custom_add_to_cart( $cart_item_data ) {

    global $woocommerce;
    $woocommerce->cart->empty_cart();

    // Do nothing with the data and return
    return $cart_item_data;
}

#2


14  

Based on the accepted answer and the latest Woo version 2.5.1 I updated the function to be slightly cleaner using the code woo uses in class-wc-checkout.php to clear the cart

基于接受的答案和最新的Woo版本2.5.1我使用类wc-checkout.php中使用的代码更新了稍微更清洁的功能来清除购物车

add_filter( 'woocommerce_add_cart_item_data', '_empty_cart' );

    function _empty_cart( $cart_item_data ) {

        WC()->cart->empty_cart();

        return $cart_item_data;
    }

#3


10  

From my answer on another question:

从我对另一个问题的回答:

There is a filter/hook that runs before an item is added to the cart as each product goes through validation before it is added.

在将项目添加到购物车之前有一个过滤器/挂钩,因为每个产品在添加之前都经过验证。

So when validating a product, we can check if the item if there are already items in the cart and clears those (if the current item is able to be added) and adds an error message.

因此,在验证产品时,我们可以检查项目是否已经存在购物车中的商品并清除(如果能够添加当前商品)并添加错误消息。

/**
 * When an item is added to the cart, remove other products
 */
function so_27030769_maybe_empty_cart( $valid, $product_id, $quantity ) {

    if( ! empty ( WC()->cart->get_cart() ) && $valid ){
        WC()->cart->empty_cart();
        wc_add_notice( 'Whoa hold up. You can only have 1 item in your cart', 'error' );
    }

    return $valid;

}
add_filter( 'woocommerce_add_to_cart_validation', 'so_27030769_maybe_empty_cart', 10, 3 );

#4


4  

This worked like a charm for me, removes the previous product and adds the new one with the new product configuration. Cheers

这对我来说就像一个魅力,删除了以前的产品,并添加了新的产品配置。干杯

Update: For WooCommerce 3.0.X

更新:对于WooCommerce 3.0.X

function check_if_cart_has_product( $valid, $product_id, $quantity ) {  

            if(!empty(WC()->cart->get_cart()) && $valid){
                foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
                    $_product = $values['data'];

                    if( $product_id == $_product->get_id() ) {
                        unset(WC()->cart->cart_contents[$cart_item_key]);
                    }
                }
            }

            return $valid;

        }
        add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );

For WooCommerce version less than 3.0.X

对于低于3.0.X的WooCommerce版本

function check_if_cart_has_product( $valid, $product_id, $quantity ) {  

    if(!empty(WC()->cart->get_cart()) && $valid){
        foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
            $_product = $values['data'];

            if( $product_id == $_product->id ) {
                unset(WC()->cart->cart_contents[$cart_item_key]);
            }
        }
    }

    return $valid;

}
add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );

#5


3  

You have two options:

你有两个选择:

  1. WooCommerce Min/Max Quantities extension
  2. WooCommerce最小/最大数量扩展
  3. The following code added to your functions.php theme file
  4. 以下代码添加到functions.php主题文件中

    add_filter ( 'woocommerce_before_cart' , 'allow_single_quantity_in_cart' );
    function allow_single_quantity_in_cart() {
            global $woocommerce;

            $cart_contents  =  $woocommerce->cart->get_cart();
            $keys           =  array_keys ( $cart_contents );

            foreach ( $keys as $key ) {
                    $woocommerce->cart->set_quantity ( $key, 1, true );
            }
    }

#6


2  

Don't add functions directly to your commerce files...you'll lose all your code when you update.

不要将功能直接添加到您的商务文件中...您在更新时将丢失所有代码。

User functions should always be hooked through the functions.php of your theme.

用户函数应始终通过主题的functions.php挂钩。

#7


2  

Try this,

尝试这个,

For removing the all products from the cart and adding the last added one,

要从购物车中删除所有产品并添加最后添加的产品,

//For removing all the items from the cart
global $woocommerce;
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($product_id,$qty);

class file is wp-content/plugins/woocommerce/classes/class-wc-cart.php.

类文件是wp-content / plugins / woocommerce / classes / class-wc-cart.php。

You can add the above code on the add to cart function in functions.php.

您可以在functions.php中的添加到购物车功能中添加上述代码。

Hope its works..

希望它的作品..

#8


2  

If you want to restrict the number of products to just 1:

如果要将产品数量限制为1:

function check_if_cart_has_product( $valid, $product_id, $quantity ) {  

    if(!empty(WC()->cart->get_cart()) && $valid){
        foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
            $_product = $values['data'];

            if( $product_id == $_product->id ) {
                wc_add_notice( 'The product is already in cart', 'error' );
                return false;
            }
        }
    }

    return $valid;

}
add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );

#1


37  

Unfortunately there is no 'action' hook before WooCommerce adds an item to the cart. But they have a 'filter' hook before adding to cart. That is how I use it:

不幸的是,在WooCommerce将商品添加到购物车之前,没有“操作”挂钩。但是在添加到购物车之前他们有一个“过滤器”钩子。这就是我使用它的方式:

add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );

function woo_custom_add_to_cart( $cart_item_data ) {

    global $woocommerce;
    $woocommerce->cart->empty_cart();

    // Do nothing with the data and return
    return $cart_item_data;
}

#2


14  

Based on the accepted answer and the latest Woo version 2.5.1 I updated the function to be slightly cleaner using the code woo uses in class-wc-checkout.php to clear the cart

基于接受的答案和最新的Woo版本2.5.1我使用类wc-checkout.php中使用的代码更新了稍微更清洁的功能来清除购物车

add_filter( 'woocommerce_add_cart_item_data', '_empty_cart' );

    function _empty_cart( $cart_item_data ) {

        WC()->cart->empty_cart();

        return $cart_item_data;
    }

#3


10  

From my answer on another question:

从我对另一个问题的回答:

There is a filter/hook that runs before an item is added to the cart as each product goes through validation before it is added.

在将项目添加到购物车之前有一个过滤器/挂钩,因为每个产品在添加之前都经过验证。

So when validating a product, we can check if the item if there are already items in the cart and clears those (if the current item is able to be added) and adds an error message.

因此,在验证产品时,我们可以检查项目是否已经存在购物车中的商品并清除(如果能够添加当前商品)并添加错误消息。

/**
 * When an item is added to the cart, remove other products
 */
function so_27030769_maybe_empty_cart( $valid, $product_id, $quantity ) {

    if( ! empty ( WC()->cart->get_cart() ) && $valid ){
        WC()->cart->empty_cart();
        wc_add_notice( 'Whoa hold up. You can only have 1 item in your cart', 'error' );
    }

    return $valid;

}
add_filter( 'woocommerce_add_to_cart_validation', 'so_27030769_maybe_empty_cart', 10, 3 );

#4


4  

This worked like a charm for me, removes the previous product and adds the new one with the new product configuration. Cheers

这对我来说就像一个魅力,删除了以前的产品,并添加了新的产品配置。干杯

Update: For WooCommerce 3.0.X

更新:对于WooCommerce 3.0.X

function check_if_cart_has_product( $valid, $product_id, $quantity ) {  

            if(!empty(WC()->cart->get_cart()) && $valid){
                foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
                    $_product = $values['data'];

                    if( $product_id == $_product->get_id() ) {
                        unset(WC()->cart->cart_contents[$cart_item_key]);
                    }
                }
            }

            return $valid;

        }
        add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );

For WooCommerce version less than 3.0.X

对于低于3.0.X的WooCommerce版本

function check_if_cart_has_product( $valid, $product_id, $quantity ) {  

    if(!empty(WC()->cart->get_cart()) && $valid){
        foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
            $_product = $values['data'];

            if( $product_id == $_product->id ) {
                unset(WC()->cart->cart_contents[$cart_item_key]);
            }
        }
    }

    return $valid;

}
add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );

#5


3  

You have two options:

你有两个选择:

  1. WooCommerce Min/Max Quantities extension
  2. WooCommerce最小/最大数量扩展
  3. The following code added to your functions.php theme file
  4. 以下代码添加到functions.php主题文件中

    add_filter ( 'woocommerce_before_cart' , 'allow_single_quantity_in_cart' );
    function allow_single_quantity_in_cart() {
            global $woocommerce;

            $cart_contents  =  $woocommerce->cart->get_cart();
            $keys           =  array_keys ( $cart_contents );

            foreach ( $keys as $key ) {
                    $woocommerce->cart->set_quantity ( $key, 1, true );
            }
    }

#6


2  

Don't add functions directly to your commerce files...you'll lose all your code when you update.

不要将功能直接添加到您的商务文件中...您在更新时将丢失所有代码。

User functions should always be hooked through the functions.php of your theme.

用户函数应始终通过主题的functions.php挂钩。

#7


2  

Try this,

尝试这个,

For removing the all products from the cart and adding the last added one,

要从购物车中删除所有产品并添加最后添加的产品,

//For removing all the items from the cart
global $woocommerce;
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($product_id,$qty);

class file is wp-content/plugins/woocommerce/classes/class-wc-cart.php.

类文件是wp-content / plugins / woocommerce / classes / class-wc-cart.php。

You can add the above code on the add to cart function in functions.php.

您可以在functions.php中的添加到购物车功能中添加上述代码。

Hope its works..

希望它的作品..

#8


2  

If you want to restrict the number of products to just 1:

如果要将产品数量限制为1:

function check_if_cart_has_product( $valid, $product_id, $quantity ) {  

    if(!empty(WC()->cart->get_cart()) && $valid){
        foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
            $_product = $values['data'];

            if( $product_id == $_product->id ) {
                wc_add_notice( 'The product is already in cart', 'error' );
                return false;
            }
        }
    }

    return $valid;

}
add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );