Magento – Magento 2 : Add to cart button change to go to cart after adding product

addtocartajaxjquerymagento2template

How to do the following –

Clicking on "Add to Cart" button adds the product to cart then Button Changes to "Go to Cart" and clicking on that redirect to Shopping Cart

Please help me in doing this functionality.

Best Answer

Replace your add to cart button in

app\design\frontend\<vendor>\<theme>\Magento_Catalog\templates\product\view
addtocart.phtml

With

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart">
    <div class="fieldset">
        <?php if ($block->shouldRenderQuantity()): ?>
        <div class="field qty">
            <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 1 ?>"
                       title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                       class="input-text qty"
                       data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
        </div>
        <?php endif; ?>
        <div class="actions">
            <?php
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $quoteId = $objectManager-> create('Magento\Checkout\Model\Session')->getQuoteId(); 
            $cartData = $objectManager->create('Magento\Quote\Model\QuoteRepository')->get($quoteId)->getAllVisibleItems();
            $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 
            $items = $cart->getQuote()->getAllItems(); 
            $count=0;
            foreach($items as $item) {
                if($item->getProductId() == $_product->getId())
                {
                    $count++;
                }
            }

                if($count != 0){?>
                    <button type="button"
                            title="<?php /* @escapeNotVerified */ echo __('Go to Cart') ?>"
                            class="action primary tocart"
                            id="product-addtocart-button"
                            onclick="window.location='<?php echo $block->getUrl('checkout/cart');?>'">
                        <span><?php /* @escapeNotVerified */ echo __('Go to Cart') ?></span>
                    </button>
            <?php }else{ ?>
                    <button type="submit"
                            title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
                            class="action primary tocart"
                            id="product-addtocart-button">
                        <span><?php /* @escapeNotVerified */ echo $buttonTitle ?></span>
                    </button>
            <?php } ?>
            <?= $block->getChildHtml('', true) ?>
        </div>
    </div>
</div>
<?php endif; ?>
<?php if ($block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/product/view/validation": {
                "radioCheckboxClosest": ".nested"
            }
        }
    }
</script>
<?php else : ?>
<script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/js/validate-product": {}
        }
    }
</script>
<?php endif; ?>
Related Topic