Only one currently added product into Woocommerce cart?

前端 未结 2 616
孤独总比滥情好
孤独总比滥情好 2021-01-23 10:47

I would like Woocommerce to only allow 1 product in the cart. If a product is already in the cart and another one is added then it should remove the previous one.

I foun

2条回答
  •  长情又很酷
    2021-01-23 11:19

    This working perfecty on Woocommerce 3.3.X

    add_filter( 'woocommerce_add_to_cart_validation', 
    'bbloomer_only_one_in_cart', 99, 2 );
    
    function bbloomer_only_one_in_cart( $passed, $added_product_id ) {
    
    global $woocommerce;
    
    // empty cart: new item will replace previous
    $woocommerce->cart->empty_cart();
    
    // display a message if you like
    wc_add_notice( 'Product added to cart!', 'notice' );
    
    return $passed;
    }
    

提交回复
热议问题