Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

php - Disable Woocommerce add to cart button if the product is already in cart

I am trying to set the functionality in Woocommerce on the Add to Cart button to only allow a particular product to be added once to the Cart.

Once a particular product has been added to cart the first time, the Add to Cart needs to be hidden.

In the cart I can have any number of products - just a max of quantity 1 for each product.

I was doing research and saw that I can use woocommerce_add_to_cart_validation hook. But have no idea how to start off.

How can I allow a particular product to be added once to the Cart?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Disable add to cart button if product is in cart using woocommerce_is_purchasable hook:

add_filter( 'woocommerce_is_purchasable', 'disable_add_to_cart_if_product_is_in_cart', 10, 2 );
function disable_add_to_cart_if_product_is_in_cart ( $is_purchasable, $product ){
    // Loop through cart items checking if the product is already in cart
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( $cart_item['data']->get_id() == $product->get_id() ) {
            return false;
        }
    }
    return $is_purchasable;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works (even for product variations in variable products).


Original answer: Here is an example using woocommerce_add_to_cart_validation hook and that will do the trick (preventing add to cart action and displaying a custom notice when needed), and using a custom utility function that will remove quantity field for your specific defined product ID:

add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_items_from_category', 10, 3 );
function limit_cart_items_from_category ( $passed, $product_id, $quantity ){
    // HERE define your product ID
    $targeted_product_id = 37;

    // Check quantity and display notice
    if( $quantity > 1 && $targeted_product_id == $product_id ){
        wc_add_notice( __('Only one item quantity allowed for this product', 'woocommerce' ), 'error' );
        return false;
    }

    // Loop through cart items checking if the product is already in cart
    foreach ( WC()->cart->get_cart() as $cart_item ){
        if( $targeted_product_id == $product_id && $cart_item['data']->get_id() == $targeted_product_id ) {
            wc_add_notice( __('This product is already in cart (only one item is allowed).', 'woocommerce' ), 'error' );
            return false;
        }
    }
    return $passed;
}

// Checking and removing quantity field for a specific product 
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
    // HERE define your product ID
    $targeted_product_id = 37;

    if( $targeted_product_id == $product->get_id() )
        $args['min_value'] = $args['max_value'] = $args['input_value'] = 1;

    return $args;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...