In this tutorial, we will show you how to change Add to cart button’s text in Magento 2, from the regular “Add to cart” to “Find out more”. (Notes: you can change it to any phrase that you want). Moreover, when the customers click on the button, they will be redirected to a different URL on the website.
Contents
Guide To Change Add To Cart Button Text & Link
1st Method
Modifying your code as below and check in the file …template/catalog/product/view/addtocart.phtml
Change from:
<?php $_product = $this->getProduct(); ?> <?php $buttonTitle = $this->__('Add to Cart'); ?> <?php if($_product->isSaleable()): ?> <div class="add-to-cart"> <?php if(!$_product->isGrouped()): ?> <label for="qty"><?php echo $this->__('Qty:') ?></label> <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" /> <?php endif; ?> <button type="button" title="<?php echo $buttonTitle ?>" id="product-addtocart-button" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button> <?php echo $this->getChildHtml('', true, true) ?> </div> <?php endif; ?>
To:
<?php $_product = $this->getProduct(); ?> <?php $buttonTitle = $this->__('Find Out More'); ?> <?php $externalURL = 'your different URL'; ?> <?php if($_product->isSaleable()): ?> <div class="add-to-cart"> <?php if(!$_product->isGrouped()): ?> <label for="qty"><?php echo $this->__('Qty:') ?></label> <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" /> <?php endif; ?> <button type="button" title="<?php echo $this->__('Find Out More') ?>" class="button btn-cart" onclick="setLocation('<?php echo $externalURL ?>')"> <span> <span><?php echo $this->__('Find Out More') ?></span> </span> </button> <?php echo $this->getChildHtml('', true, true) ?> </div> <?php endif; ?>
2nd Method
Implementing these following steps:
Create the attribute ‘find_out_more’ with yes or no and then create an attribute to get the link ‘get_link’ if choose yes for the first one.
Now, open the file addtocart.phtml like in the 1st method and use the following code:
<?php $_product = $this->getProduct(); ?> <?php $buttonTitle = $this->__('Find Out More'); ?> <?php if($_product->isSaleable()): ?> <div class="add-to-cart"> <?php if(!$_product->isGrouped()): ?> <label for="qty"><?php echo $this->__('Qty:') ?></label> <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" /> <?php endif; ?> <?php if($_product->getData('find_out_more')==1){?> <button type="button" title="<?php echo $this->_('') ?>" class="button btn-cart" onclick="<?php echo $_product->getData('get_link'); ?>"><span><span><?php echo $buttonTitle ?></span></span></button> <?php }else { ?> <button type="button" title="<?php echo $this->_('Add to Cart') ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $this->_('Add to Cart') ?></span></span></button> <?php } ?> <?php echo $this->getChildHtml('', true, true) ?> </div> <?php endif; ?>
Wrapping Up
We have shown you how to customize/ change Add to cart button in Magento. If you have any problems when following these instructions, leave a comment and we’re glad to help!