{"id":2891,"date":"2021-03-03T21:01:41","date_gmt":"2021-03-03T21:01:41","guid":{"rendered":"https:\/\/scriptfeeds.com\/blog\/?p=2891"},"modified":"2023-03-27T14:21:03","modified_gmt":"2023-03-27T14:21:03","slug":"how-to-change-add-to-cart-text-if-a-product-is-in-cart-on-woocommerce","status":"publish","type":"post","link":"https:\/\/scriptfeeds.com\/blog\/wordpress\/how-to-change-add-to-cart-text-if-a-product-is-in-cart-on-woocommerce\/","title":{"rendered":"2 Easy ways to change add to cart text if a product is in cart on WooCommerce"},"content":{"rendered":"\n<p>This Article will guide you to change add to cart text if a product is in cart on <a href=\"https:\/\/wordpress.org\/plugins\/woocommerce\/\" target=\"_blank\" rel=\"noopener\">WooCommerce<\/a>. We designed and developed lots of WooCommerce stores for our customers, so everyday we face lots of customization demand and challenges from clients. <\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#code-to-change-add-to-cart-text\">Code to change add to cart text<\/a><ul><\/ul><\/li><li><a href=\"#where-to-paste-the-code-to-to-change-add-to-cart-text\">Where to paste the code to to change add to cart text?<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>Recently one of client asked if we can change the text of button <strong>&#8220;ADD TO CART&#8221;<\/strong> dynamically after the product is added in cart? At first sight, it seems bit tricky but when we had a look on WooCommerce codex, things became clear. <\/p>\n\n\n\n<h2 class=\"has-medium-font-size wp-block-heading\" id=\"code-to-change-add-to-cart-text\"><strong>Code to change add to cart text<\/strong><\/h2>\n\n\n\n<h3 class=\"has-medium-font-size wp-block-heading\" id=\"use-code-below-which-you-can-use-to-change-add-to-cart-text-of-button-dynamically-once-product-added-to-cart\"><strong>Use code below which you can use to change &#8220;ADD TO CART&#8221; text of button dynamically once product added to cart. <\/strong><\/h3>\n\n\n\n<p>Here our text changes to <strong>&#8220;ADDED TO CART&#8221;<\/strong>. You can define your own text on last section of the code. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Conditional function that checks if a product is in cart and return the correct button text\nfunction change_button_text( $product_id, $button_text ) {\n    foreach( WC()-&gt;cart-&gt;get_cart() as $item ) {\n        if( $product_id === $item&#91;'product_id'] ) {\n            return __('Already in Cart. Add again?', 'woocommerce');\n        }\n    }\n    return $button_text;\n}\n\n\/\/ Archive pages: For simple products (ajax add to cart button)\nadd_filter( 'woocommerce_product_add_to_cart_text', 'change_ajax_add_to_cart_button_text', 10, 2 );\nfunction change_ajax_add_to_cart_button_text( $button_text, $product ) {\n    if ( $product-&gt;is_type('simple') ) {\n        $button_text = change_button_text( $product-&gt;get_id(), $button_text );\n    }\n    return $button_text;\n}\n\n\/\/ Single product pages: Simple and external products\nadd_filter( 'woocommerce_product_single_add_to_cart_text', 'change_single_add_to_cart_button_text', 10, 2 );\nfunction change_single_add_to_cart_button_text( $button_text, $product ) {\n    if (  ! $product-&gt;is_type('variable') ) {\n        $button_text = change_button_text( $product-&gt;get_id(), $button_text );\n    }\n    return $button_text;\n}\n\n\/\/ Single product pages: Variable product and its variations\nadd_action( 'woocommerce_after_variations_form', 'action_after_variations_form_callback' );\nfunction action_after_variations_form_callback() {\n    global $product;\n\n    \/\/ Get the product variation Ids for the variable product\n    $children_ids = $product-&gt;get_visible_children();\n\n    $ids_in_cart  = &#91;];\n\n    \/\/ Loop through cart items\n    foreach( WC()-&gt;cart-&gt;get_cart() as $item ) {\n        if( in_array( $item&#91;'variation_id'], $children_ids ) ) {\n            $ids_in_cart&#91;] = $item&#91;'variation_id'];\n        }\n    }\n    ?&gt;\n    &lt;script type=\"text\/javascript\"&gt;\n    jQuery(function($){\n        var b = 'button.single_add_to_cart_button',\n            t = '&lt;?php echo $product-&gt;single_add_to_cart_text(); ?&gt;';\n\n        $('form.variations_form').on('show_variation hide_variation found_variation', function(){\n            $.each(&lt;?php echo json_encode($ids_in_cart); ?&gt;, function(j, v){\n                var i = $('input&#91;name=\"variation_id\"]').val();\n                if(v == i &amp;&amp; i != 0 ) {\n                    $(b).html('&lt;?php _e('Added to cart', 'woocommerce'); ?&gt;');\n                    return false;\n                } else {\n                    $(b).html(t);\n                }\n            });\n        });\n    });\n    &lt;\/script&gt;\n    <\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"800\" height=\"526\" src=\"https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2022\/02\/added-to-cart-scriptfeeds.jpg\" alt=\"change add to cart text\" class=\"wp-image-3357\" title=\"How to change add to cart text\" srcset=\"https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2022\/02\/added-to-cart-scriptfeeds.jpg 800w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2022\/02\/added-to-cart-scriptfeeds-399x262.jpg 399w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2022\/02\/added-to-cart-scriptfeeds-600x395.jpg 600w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2022\/02\/added-to-cart-scriptfeeds-245x161.jpg 245w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2022\/02\/added-to-cart-scriptfeeds-123x81.jpg 123w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2022\/02\/added-to-cart-scriptfeeds-768x505.jpg 768w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2022\/02\/added-to-cart-scriptfeeds-750x493.jpg 750w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2022\/02\/added-to-cart-scriptfeeds-300x197.jpg 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><figcaption class=\"wp-element-caption\">Button changes to &#8220;ADDED TO CART&#8221; text when product added<\/figcaption><\/figure>\n\n\n\n<h2 class=\"has-medium-font-size wp-block-heading\" id=\"where-to-paste-the-code-to-to-change-add-to-cart-text\"><strong>Where to paste the code to to change add to cart text?<\/strong><\/h2>\n\n\n\n<p>Well the code will go in functions.php file of your child theme. If you don&#8217;t have child theme, then create one child theme and add code in functions.php file. If you have any question, just comment below or connect with our<a href=\"https:\/\/scriptfeeds.com\/blog\/hire-us\/\" data-type=\"page\" data-id=\"3537\"> Developer here<\/a><\/p>\n\n\n\n<p>HAPPY CODING \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This Article will guide you to change add to cart text if a product is in cart on WooCommerce. We designed and developed lots of WooCommerce stores for our customers, so everyday we face lots of customization demand and challenges from clients. Recently one of client asked if we can change the text of button [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3393,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":{"subtitle":"","format":"standard","video":"","gallery":"","source_name":"","source_url":"","via_name":"","via_url":"","override_template":"0","override":[{"template":"1","single_blog_custom":"","parallax":"1","fullscreen":"1","layout":"right-sidebar","sidebar":"default-sidebar","second_sidebar":"default-sidebar","sticky_sidebar":"1","share_position":"top","share_float_style":"share-monocrhome","show_share_counter":"1","show_view_counter":"1","show_featured":"1","show_post_meta":"1","show_post_author":"1","show_post_author_image":"1","show_post_date":"1","post_date_format":"default","post_date_format_custom":"Y\/m\/d","show_post_category":"1","show_post_reading_time":"0","post_reading_time_wpm":"300","show_zoom_button":"0","zoom_button_out_step":"2","zoom_button_in_step":"3","show_post_tag":"1","show_prev_next_post":"1","show_popup_post":"1","number_popup_post":"1","show_author_box":"0","show_post_related":"0","show_inline_post_related":"0"}],"override_image_size":"0","image_override":[{"single_post_thumbnail_size":"crop-500","single_post_gallery_size":"crop-500"}],"trending_post":"0","trending_post_position":"meta","trending_post_label":"Trending","sponsored_post":"0","sponsored_post_label":"Sponsored by","sponsored_post_name":"","sponsored_post_url":"","sponsored_post_logo_enable":"0","sponsored_post_logo":"","sponsored_post_desc":"","disable_ad":"0"},"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[75,86],"tags":[216,105],"class_list":["post-2891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","category-woocommerce","tag-change-add-to-cart-button-to-added-to-cart","tag-change-add-to-cart-text"],"_links":{"self":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/posts\/2891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/comments?post=2891"}],"version-history":[{"count":0,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/posts\/2891\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/media\/3393"}],"wp:attachment":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/media?parent=2891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/categories?post=2891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/tags?post=2891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}