0

I want to redirect the specific product page when i click the add to cart button instead of add the product to cart in magento 2.3.2

If anyone know the answer please share the exact code and folder structure since i'm new to magento.

1 Answer 1

0

you need to overide addtocart.phtml file.

here is the code app\code\Vendor\Extension\view\frontend\layout\catalog_product_view.xml

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

    <body>

        <referenceBlock name="product.info.addtocart">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Extension::catalog/product/view/addtocart.phtml</argument>
            </action>
        </referenceBlock>

    </body>
</page>

Now, create phtml file.

app\code\Vendor\Extension\view\frontend\templates\catalog\product\view\addtocart.phtml

Put below code at addtocart.phtml

    <?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><?php /* @escapeNotVerified */ echo __('Qty') ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       maxlength="12"
                       value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
                       title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty"
                       data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
        </div>
        <?php endif; ?>
        <div class="actions">
                <button type="button" title="Titel" class="action primary tocart" onclick="setProductData('<?php echo $_product->getId(); ?>')"><span><?php /* @escapeNotVerified */ echo 'button text' ?></span></button>
            <?php echo $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>
    require([
        'jquery',
        'mage/mage',
        'Magento_Catalog/product/view/validation',
        'Magento_Catalog/js/catalog-add-to-cart'
    ], function ($) {
        'use strict';

        $('#product_addtocart_form').mage('validation', {
            radioCheckboxClosest: '.nested',
            submitHandler: function (form) {
                var widget = $(form).catalogAddToCart({
                    bindSubmit: false
                });

                widget.catalogAddToCart('submitForm', $(form));

                return false;
            }
        });
    });
</script>
<?php endif; ?>

it will work, Happy Coding :)

8
  • it's not working. i have created new module like Custom_Addtocart. and created the phtml file and xml file which you have mentioned the file path. after that i have clicked the add to cart button still its adding the product.
    – Ramesh R
    Dec 11, 2019 at 9:50
  • Have you created registration.php and etc/module.xml file? Dec 11, 2019 at 10:47
  • yes. i have created registration.php file and module.xml file.
    – Ramesh R
    Dec 11, 2019 at 10:48
  • xml and phtml file added app/code/Custom/addtocart/view/layout and /templates folder ? Dec 11, 2019 at 10:49
  • now i'm trying another way like paste the code under design/frontend/vendor/themename/Magento_Catalog following same file path which you're shared.
    – Ramesh R
    Dec 11, 2019 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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