WooCommerce – Check if product is already in cart

Here is a snippet for WooCommerce that changes the “add to cart” button text when a product is already into the cart. It uses two filters, one to change the text on the product page, and one for the products list. Here are the results:[edit] code has been updated to be compatible with latest WooCommerce versions on Oct. 30th 2015

 

woocommerce-product-is-in-cart

woocommerce-product-is-in-cart-2

 

 

24 responses to “WooCommerce – Check if product is already in cart”

  1. siddanth

    Hi Remi,

    I love this code. Can you please tell me where to paste this code.
    I think I’ve to paste this code in functions.php in my theme folder. Am i correct ?

  2. tracer

    Hi Remi,

    This is great!

    Wondering if it’s possible to count the number of products from a specified category that have been added to the cart? I need to display a notice like “You have added # meals to the cart” but I don’t want to count the products from other categories. Any ideas?

    1. Hi, yes i’m pretty sure that’s doable but i never tried. I guess that using the $cart object will allow you to do it.

  3. heatherita

    Hi!
    Hi! I have used this code with woocommerce 2.0.20 and it was GREAT, worked just fine. I updated yesterday to woocommerce 2.1 and it is no longer working…
    Do you know why this might be?? Is there anything in the code I should modify?
    Thanks!

    1. hey, i haven’t tested it with WC 2.1, i’ll give it a try later

      1. heatherita

        Ok- thank you! :)

  4. justincone

    Thank you so much for this code.

    In my case, I’m using WP_Query to display a selection of products on my homepage. To generate the Add to Cart button, I’m using do_shortcode in my template. It works, but not with the code above.

    I modified the first filter to use woocommerce_loop_add_to_cart_link (instead of single_add_to_cart_text), and while that did indeed alter the text of the Add to Cart button, it rendered as *just* text, i.e. not a button (not even a link).

    Do you have any advice on how to proceed?

    Thank you!

  5. beat84

    Do you think is possible to disable Add button if a product of the same category is already in the cart?

  6. gemmawild

    Hi Remi

    I’ve copied and pasted the code exactly how given into functions.php and it works great on the single product page but seemingly not on any other page, i.e. the shop or category pages. I’m not using any other plugins that would edit the default Woocommerce markup or text.

    Is there an update to this code we need to use instead since Woocommerce 2.1 has been released?

    Thank you =)

  7. Gabor Javorszky

    In WooCoomerce 2.1.7 the name of the filters changed. They are now
    woocommerce_product_single_add_to_cart_text and woocommerce_product_add_to_cart_text. If the code stopped working, you might need to change the names of the filters.

  8. iwbyte

    Thanks for the code snippets here and elsewhere – really saved me a lot of work!

    Question: I noticed that the ‘already in cart’ text won’t show up if you are using the [add_to_cart id=””] shortcode. I didn’t see any specific filter for the shortcodes so I’m wondering if its even possible to customize the resulting HTML/css that comes out of a shortcode?

    Any thoughts?
    Thanks again!

  9. alemarengo

    Hi Remi!
    Thanks for this useful snippet!
    Probably one should know that it works perfectly only if used for single products, not for variable ones. In fact, if one would like to put more than one variation in the cart, the button looks like saying something misleading…
    What do you think about?
    Bye!

    1. alemarengo

      Hi!
      Remi, can you help us with this? Variation should be treated in a different way otherwise information to the customer is misleading in a way. What do you think about? Hope you can reply to this. :)

  10. darrentucs

    Hi There,

    Great snippet!

    Is there anyway we can get this to work with ajax?

    Thanks again!

  11. shoper

    Hi here, I’m just start using woo commerce and wordpress. Where i can put that snippet? Thank in advance!

  12. Saran

    Hi Remi,

    Useful code snippet. Working fine. Is there any possibility to change the Already in cart button to unclickable or disabled?

    Thanks,
    Saran

  13. rbrt_bene

    Hi Remi,

    I am looking for a solution for the following scenario: restrict the checkout if a product of a certain category is not in the cart. I have looked for days for a solution or a plugin with no success.

    Any ideas?
    Robert

  14. team

    Hello Remi,

    This is a wonderful post. I want some changes with this snippet as per my requirements.
    I want change the ADD TO CART button text to ADDED when the item is in cart.
    otherwise check the product tag, if the product tag is “buynow”, the ADD TO CART button text will be BUY NOW, else it will be ADD TO CART. I used if ( has_term( ‘buynow’, ‘product_tag’, $_product->id ) ). but I am going with some logic error. Can you please help me

    Here is my code:

    /**
    * Change the add to cart text on single product pages
    */
    add_filter(‘woocommerce_product_single_add_to_cart_text’, ‘woo_custom_cart_button_text’);
    function woo_custom_cart_button_text() {
    global $woocommerce;
    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values[‘data’];

    if( get_the_ID() == $_product->id ) {
    return __(‘Added’, ‘woocommerce’);
    }
    }

    if ( has_term( ‘buynow’, ‘product_tag’, $_product->id ) ) :
    return __( ‘Buy Now’, ‘woocommerce’ );
    else:
    return __( ‘Add to Cart’, ‘woocommerce’ );
    endif;
    }
    /**
    * Change the add to cart text on product archives
    */

    add_filter( ‘woocommerce_product_add_to_cart_text’, ‘woo_archive_custom_cart_button_text’ );
    function woo_archive_custom_cart_button_text() {
    global $woocommerce;
    foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values[‘data’];

    if( get_the_ID() == $_product->id ) {

    return __(‘Added’, ‘woocommerce’);
    }
    }

    if ( has_term( ‘buynow’, ‘product_tag’, $_product->id ) ) :
    return __( ‘Buy Now’, ‘woocommerce’ );
    else:
    return __( ‘Add to Cart’, ‘woocommerce’ );
    endif;
    }

  15. prabhjas

    Hello Remi,
    Everything works like a charm. But could you help me with adding ajax to this code. Because I don’t want to load a full page.
    Please provide some code for that.
    Thanks.

  16. Nick

    Hi Remi,

    Thanks for your post. I get a similar problem that is related to the one you talk about. As I sell digital products, I cannot uncheck the “Enable this to only allow one of this item to be bought in a single order” option because I don’t want customers to buy 2 times the same digital product and having to refund them.

    The problem occurs when a customer do not checkout right now, go back to the site, and then change his mind and click another time on the “buy” button. As I sell digital product on landing pages, I skip the cart to simplify the process, but in these situations the customers get the “You cannot add the X product in your cart” instead of going back to the checkout page for that single digital product.

    Is there a way to simply “re-add” the single digital product (with the option “allow one of this item to be bought in a single order” activated) and get the customer to the cart instead of giving the “You cannot add the X product in your cart” message?

    Thanks!

  17. alexanderbiscajin

    To hide add to cart from product pages. You can use this code in woocommerce.php (located wp-content/plugins/woocommerce):

    `function WpBlog() {

    remove_action( ‘woocommerce_after_shop_loop_item’, ‘woocommerce_template_loop_add_to_cart’);

    remove_action( ‘woocommerce_single_product_summary’, ‘woocommerce_template_single_add_to_cart’);

    return WooCommerce::instance();

    }`

    And for the specific product pages you can hide or disable add to cart button by adding the code in functions.php

    `add_filter(‘woocommerce_is_purchasable’, ‘wpblog_specific_product’);

    function wpblog_specific_product($purchaseable_product_wpblog, $product)
    {
    return ($product->id == specific_product_id (512) ? false : $purchaseable_product_wpblog);

    }`

    For reference you can see https://www.wpblog.com/add-to-cart-button-in-woocommerce-store/

Leave a Reply