Question

Do you know how to Change text and Relink the "Add To Cart" button in the product list page (need to change code in the file list.phtml).

In my magento server, the list.phtml file is located in the theme directory. I'm using magento 2.2.6: /public_html/app/design/frontend/Hiddentechies/pixtron/Magento_Catalog/templates/product/list.phtml

I can change the button text from "Add To Cart" to "Find Out More", but can not relink it.

To relink the button, I created an atrribute 'get_link' for each product to store the external URL from admin's input, when customer click "Find Out More" button, it will direct user to the external URL's page in the web browser.

<button type="button"
    title="<?php echo $block->escapeHtml(__('Find Our More')); ?>"
    class="action tocart primary"
    onclick='window.location.href="<?php echo $_product->getData('get_link'); ?>"'>
    <span><?php /* @escapeNotVerified */ echo __('Find Our More') ?></span>
</button>

It's difficult because I don't know how to take the value in the 'get_link' atrribute in file list.phtml, it's not the same way to take the 'get_link' atrribute in file addtocart.phtml:

(/public_html/app/design/frontend/Hiddentechies/pixtron/Magento_Catalog/templates/product/view/addtocart.phtml)

and there is also a FOR loop to print all products in the same category, so that I don't know how to get the value of 'get_link' for each product:

<?php foreach ($_productCollection as $_product): ?>

I know it's not easy but hope you will take a time to help. Or just give me somewhere I can find solution for this.

Thank you a lot.

Was it helpful?

Solution

Change your button code at

public_html/app/design/frontend/Hiddentechies/pixtron/Magento_Catalog/templates/product/list.phtml

to:

<button type="button"
    title="<?php echo $block->escapeHtml(__('Find Our More')); ?>"
    class="action tocart primary"
    onclick='window.location.href="<?php echo $_product->getGetLink(); ?>"'>
    <span><?php /* @escapeNotVerified */ echo __('Find Our More') ?></span>
</button>

Above button is creating in a loop, So it will take value for each product.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top