WooCommerce

Woocommerce change Add to cart text if already at Cart

My previous article was about how to change add to cart button text. But what if you want to change add to cart text if product is already in cart. For instance, when a user adds a product to their cart, the ‘add to cart’ button text will automatically rename to ‘Already in Cart. Add again?’. In this article, I will show you how to change the add to cart button text if product is already in cart on Woocommerce, and we will do it without any plugin rather we will use a code or snippet.

The best part is that the code or snippet which I am going to share with you in this article will work for both simple and variable products (even with external products).

wpamit rename add to cart if already at cart in woocommerce without plugin 2

It’s no secret that the way people relate to a product is going to change depending on whether they’re looking at it to buy or they’ve already bought it. That’s why so many websites have different sets of buttons and messages for logged-in users and for users who aren’t logged in.

This might be obvious, but it’s important to remember: that your customers are going to have different needs and expectations based on where they are in their experience with you.

That’s why we’re going to show you how to change the way your products are treated on your site once they’ve been added to the cart. You’ll learn how (and why) to change the text that appears on the add-to-cart button when a product has already been added to cart, which indicates that users can no longer add it.

Why should change add to cart text if already at cart in woocommerce?

Because it makes sense. When a customer adds a product to their shopping cart, you want them to know that it’s there. So you can use the standard WooCommerce add-to-cart text, and when the product has been added to the cart, replace the text with something like “Already in cart. Add again?”

What if the user wants to add that particular product again to the cart, will this code work in that scenario?

Yes, this code will work in that condition too. The user needs to simply click on the ‘Already in Cart Add again?’ button, and the product will again be added to the cart.

best woocommerce theme 2023

Steps to modify add to cart text if product already at cart

Just like my all articles, again I have same recommendations for you.

Take a full backup of your website or blog.
Be careful before editing the functions.php file. If you don’t want to edit functions.php directly, use the code snippet plugin.
We recommend you use a child theme before any customization on your website. You can use a plugin to create child theme

1). Login to WordPress Admin Dashboard. Go to ‘Appearance’ then ‘Theme File Editor’. Then click on ‘Theme Functions’. This is the functions.php file of your theme.

access theme file editor
Go to Appearance then click Theme File Editor
how to change wordpress login page logo 4
Click on Theme Functions

2). Add below code to the Theme Functions (functions.php) or use this code snippet plugin.

/**
 * change add to cart text if already in cart
 * by http://wpamit.com
*/
// Checks if a product is in cart and return the correct button text
function wpamit_change_button_text( $product_id, $button_text ) {
    foreach( WC()->cart->get_cart() as $item ) {
        if( $product_id === $item['product_id'] ) {
            return __('Already in Cart. Add again?', 'woocommerce');
        }
    }
    return $button_text;
}

// Archive pages or shop pages: For simple products (ajax add to cart button)
add_filter( 'woocommerce_product_add_to_cart_text', 'wpamit_change_ajax_add_to_cart_button_text', 10, 2 );
function wpamit_change_ajax_add_to_cart_button_text( $button_text, $product ) {
    if ( $product->is_type('simple') ) {
        $button_text = wpamit_change_button_text( $product->get_id(), $button_text );
    }
    return $button_text;
}

// Single product pages: Simple and external products
add_filter( 'woocommerce_product_single_add_to_cart_text', 'wpamit_change_single_add_to_cart_button_text', 10, 2 );
function wpamit_change_single_add_to_cart_button_text( $button_text, $product ) {
    if (  ! $product->is_type('variable') ) {
        $button_text = wpamit_change_button_text( $product->get_id(), $button_text );
    }
    return $button_text;
}

// Single product pages: Variable product and its variations
add_action( 'woocommerce_after_variations_form', 'wpamit_action_after_variations_form_callback' );
function wpamit_action_after_variations_form_callback() {
    global $product;

    // Get the produc variation Ids for the variable product
    $children_ids = $product->get_visible_children();

    $ids_in_cart  = [];

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $item ) {
        if( in_array( $item['variation_id'], $children_ids ) ) {
            $ids_in_cart[] = $item['variation_id'];
        }
    }
    ?>
    <script type="text/javascript">
    jQuery(function($){
        var b = 'button.single_add_to_cart_button',
            t = '<?php echo $product->single_add_to_cart_text(); ?>';

        $('form.variations_form').on('show_variation hide_variation found_variation', function(){
            $.each(<?php echo json_encode($ids_in_cart); ?>, function(j, v){
                var i = $('input[name="variation_id"]').val();
                if(v == i && i != 0 ) {
                    $(b).html('<?php _e('Already in Cart. Add again?', 'woocommerce'); ?>');
                    return false;
                } else {
                    $(b).html(t);
                }
            });
        });
    });
    </script>
    <?php
}

Changes you can make in this code: You can replace the text ‘Already in Cart. Add again?‘ as per your requirements. You just need to change text in line 9 and line 59 in the above code (appears two times).

Hope this article helped you understand how to change add to cart text if product is already in cart without using any plugin. If you need any assistance, please comment below. I will be happy to help you.

Leave a Reply

Your email address will not be published. Required fields are marked *