1

I need to redirect the specific product page when i click the add to cart button in the category page. instead of add the product to the shopping 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.

enter image description here

2

you just need to override list.phtml file in your theme

app/design/frontend/Vendor/Theme/Magento_Catalog/templates/product/list.phtml

and replace the code below:

From

<?php $postParams = $block->getAddToCartPostParams($_product); ?>
<form data-role="tocart-form"
      data-product-sku="<?= $block->escapeHtml($_product->getSku()) ?>"
      action="<?= $block->escapeUrl($postParams['action']) ?>"
      method="post">
    <input type="hidden"
           name="product"
           value="<?=  $postParams['data']['product'] ?>">
    <input type="hidden" name="<?=  Action::PARAM_NAME_URL_ENCODED ?>"
           value="<?=  $postParams['data'][Action::PARAM_NAME_URL_ENCODED] ?>">
    <?= $block->getBlockHtml('formkey') ?>
    <button type="submit"
            title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"
            class="action tocart primary">
        <span><?= $block->escapeHtml(__('Add to Cart')) ?></span>
    </button>
</form>

To

<button title="<?= $block->escapeHtmlAttr(__('Add to Cart')) ?>"
        class="action primary">
    <a href="<?= $_product->getProductUrl() ?>"><span><?= $block->escapeHtml(__('Add to Cart')) ?></span></a>
</button>

Hope this will help you!

  • Hello Naresh, I have copied core list.phtml file code and replaced the code which you have shared. but still if i click the add to cart button, add the product to the shopping cart. instead of open the specific product. – Ramesh R Dec 11 '19 at 14:07
  • Code updated. please check with updated code and let me know it's working for you or not – Naresh Rupareliya Dec 12 '19 at 2:35
  • thank you naresh. it's working fine. – Ramesh R Dec 12 '19 at 7:35

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.