How to do the following –
Clicking on "Add to Cart" button adds the product to cart then Button Changes to "Go to Cart" and clicking on that redirect to Shopping Cart
Please help me in doing this functionality.
addtocartajaxjquerymagento2template
How to do the following –
Clicking on "Add to Cart" button adds the product to cart then Button Changes to "Go to Cart" and clicking on that redirect to Shopping Cart
Please help me in doing this functionality.
There is an option on the add to cart from to submit submit_route_data
which will override the standard add to cart url used in Magento.
public function getSubmitUrl($product, $additional = array())
{
$submitRouteData = $this->getData('submit_route_data');
if ($submitRouteData) {
$route = $submitRouteData['route'];
$params = isset($submitRouteData['params']) ? $submitRouteData['params'] : array();
$submitUrl = $this->getUrl($route, array_merge($params, $additional));
} else {
$submitUrl = $this->getAddToCartUrl($product, $additional);
}
return $submitUrl;
}
I have not used this myself but this should work at least for internal urls. An example can be found at app/code/core/Mage/Checkout/Block/Cart/Item/Configure.php
in the function _prepareLayout
.
For external urls you may need to work with the event controller_action_(pre|post)dispatch_{{fullActionName}}
here you should be able to catch the pre dispatch for the add to cart action and then simply redirect the user when the product being added matches a certain product.
You need to create new extension for this follow the following steps
1.Addtocart/Newbutton/composer.json
{
"name": "addtocart/newbutton",
"description": "Addtocart Newbutton",
"require": {
"php": "~5.5.0|~5.6.0",
"magento/module-store": "0.74.0-beta4",
"magento/module-theme": "0.74.0-beta4",
"magento/module-widget": "0.74.0-beta4",
"magento/module-backend": "0.74.0-beta4",
"magento/module-catalog": "0.74.0-beta4",
"magento/module-email": "0.74.0-beta4",
"magento/module-ui": "0.74.0-beta4",
"magento/module-variable": "0.74.0-beta4",
"magento/module-media-storage": "0.74.0-beta4",
"magento/framework": "0.74.0-beta4",
"magento/magento-composer-installer": "*"
},
"type": "magento2-module",
"version": "0.74.0-beta4",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"extra": {
"map": [
[
"*",
"Addtocart/Newbutton"
]
]
}
}
2.Addtocart/Newbutton/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Addtocart_Newbutton',
__DIR__
);
3.Addtocart/Newbutton/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Addtocart_Newbutton" setup_version="1.0.0"></module>
</config>
4.Addtocart/Newbutton/view/frontend/layout/catalog_product_view.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<body>
<referenceBlock name="product.info.form.options" htmlClass="css_class" htmlTag="div">
<block class="Magento\Catalog\Block\Product\View"
name="product.addtocart.info" as="product_addtocart_info"
template="Addtocart_Newbutton::product/view/addtocart.phtml"
before="product.options.wrapper"
>
</block>
</referenceBlock>
</body>
</page>
5.Addtocart/Newbutton/view/frontend/templates/product/view/addtocart.phtml
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/** @var $block \Magento\Catalog\Block\Product\View */
?>
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart" style="border-bottom: 1px #ebebeb solid; margin-bottom: 15px; width: 100%;">
<div class="fieldset" style="margin: 0 0 36px;">
<?php if ($block->shouldRenderQuantity()): ?>
<div class="no-display"><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 class="qty-changer">
<a href="javascript:void(0)" class="qty-inc"><i class="porto-icon-up-dir"></i></a>
<a href="javascript:void(0)" class="qty-dec"><i class="porto-icon-down-dir"></i></a>
</div>
</div></div>
<?php endif; ?>
<div class="actions">
<button type="submit"
title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
class="action primary tocart"
id="product-addtocart-button">
<span><?php /* @escapeNotVerified */ echo $buttonTitle ?></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; ?>
Best Answer
Replace your add to cart button in
With