WooCommerce – Check if product is already in cart

Remi | October 1, 2013 | in WooCommerce

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

 

 

The author

About the author Remi: Hey! My name is Rémi Corson, i'm a WordPress Expert Developer, i create high level themes and plugins, and i provide free stuff every week on this website!

20 Comments

  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?

  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!

  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. 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 =)

  6. 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.

  7. 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!

  8. 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. :)

  9. 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

  10. 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

  11. 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;
    }

Leave a reply