0

I have tried to achieve this using answer from the question here Magento 2 : Add to cart button change to go to cart after adding product

However the button text is not changed immediately as the code is from phtml file and not js file.

I wish to achieve this using js(catalog-add-to-cart.js). To check if product is added to cart, I need to get cart items in this file. How do I get current user cart items here? Once the text is changed to Go to Cart, I also want to change the button link to cart url.

0

override catalog-add-to-cart.js

vendor/magento/module-catalog/view/frontend/web/js/catalog-add-to-cart.js 

in your theme

app/design/frontend/Magento/test/Magento_Catalog/web/js/catalog-add-to-cart.js 

line around 211 change

$t('Add to Cart'); to $t('Go to Cart');

setTimeout(function () {
                var addToCartButtonTextDefault = self.options.addToCartButtonTextDefault || $t('Add to Cart'); //change here

                addToCartButton.removeClass(self.options.addToCartButtonDisabledClass);
                addToCartButton.find('span').text(addToCartButtonTextDefault);
                addToCartButton.attr('title', addToCartButtonTextDefault);
            }, 1000);

after run command via ssh :

rm -rf pub/static/*;rm -rf var/view_preprocessed/*;rm -rf var/cache/*;rm -rf var/log/*;rm -rf var/di/*;rm -rf var/page_cache/*;rm -rf var/tmp/*;rm -rf var/generated/*;rm -rf generated/*;

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy -f

php bin/magento cache:clean

php bin/magento cache:flush
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.