I want to add button in PDP page which would redirect to the desired location. But it does not capture the size and other customize attributes.
Can anybody help me?
How to add a custom button to Magento 2 product page
View/frontend/templates/catalog/product/view/addtocart.phtml
<div class="actions">
<button type="submit"
title="<?= /* @escapeNotVerified */ $buttonTitle ?>"
class="action default tocart"
id="product-addtocart-button">
<span><?= /* @escapeNotVerified */ $buttonTitle ?></span>
</button>
<?= $block->getChildHtml('', true) ?>
<?php /*if($cusdesignLink):?>
<a href="<?php echo $cusdesignLink; ?>"><?php echo __('Customize Your Design'); ?></a>
<?php endif */ ?>
<a href="<?php echo $this->getUrl($cusdesignLink); ?>" title="<?php /* @escapeNotVerified */ echo __($buttonTitle) ?>" id="custom" target="_blank" class="action primary">
<span><?php /* @escapeNotVerified */ echo __($custombuttonTitle) ?></span>
</a>
</div>
Creation of button Method 2 where it works as Add to cart and does not replace Form action View/frontend/web/js/custom.js
define([
'jquery'
], function ($) {
"use strict";
return function (config, element) {
$(element).click(function () {
var form = $(config.form);
var baseUrl = form.attr('action'),
buyNowUrl = baseUrl.replace('checkout/cart/add', 'custom/index/index');
form.attr('action', 'custom/index/index');
return false;
});
}
});
view/frontend/templates/custom.phtml
<?php $addtocartId = "Test"; ?>
<?php $buttonTitle = "Test"; ?>
<?php $formId = $addtocartId ? $addtocartId : "product_addtocart_form"; ?>
<div class="custom-button">
<button type="submit" title="<?php /* @escapeNotVerified */ echo __($buttonTitle) ?>" id="custom" class="action primary" data-mage-init='
{
"Maan_Custom/js/custom": {
"form": "#<?php echo $formId; ?>"
}
}
'>
<span><?php /* @escapeNotVerified */ echo __($buttonTitle) ?></span>
</button>
</div>
Actually I have not created button i have added link to display it as a button and have passed link via helper. Had got code for creating button but it does not replace form action instead it gets added to "Add to cart" section