while add to compare on product listing page I want to add a popup for success message instead of Magento 2 default success message.
Help would be appreciated.
comparelistingmagento2success-message
while add to compare on product listing page I want to add a popup for success message instead of Magento 2 default success message.
Help would be appreciated.
Copy catalog-add-to-cart.js from core '\module-catalog\view\frontend\web\js/catalog-add-to-cart.js and place into your theme folder \Package/theme/Magento_Catalog/web/js/catalog-add-to-cart.js
Now find ajaxSubmit function and add below code after self.enableAddToCartButton(form);
var popup = $('<div class="add-to-cart-dialog"/>').html($('.page-title span').text() + '<span> has been added to cart.</span>').modal({ //get product name from product view page only
modalClass: 'add-to-cart-success-popup'
});
popup.modal('openModal');
Reference for more modal options: https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/widgets/widget_modal.html
Submit the form via ajax
Use this script in your phtml:
<script type="text/javascript">
function callAjax(){
jQuery.ajax({
type : "POST",
url : "<?php echo Mage::getUrl('module/ctrlr/action'); ?>",
data : jQuery('#test_sample_form').serialize(),
dataType: "json",
complete: function (data) {
callpopup();
document.getElementById("test_sample_form").reset();
},
});
}
function callpopup(){
jQuery.fancybox(
'<p>Content!!!!!</p></br>',
{
padding:15,
closeBtn:true
}
);
}
</script>
Best Answer
I have just finished a module for adding compare products via Ajax. The logic of module just followed the Magento core logic:
vendor/magento/module-catalog/Controller/Product/Compare/Add.php
vendor/magento/module-catalog/view/frontend/web/js/catalog-add-to-cart.js
1) Declare our route.xml
app/code/Vendor/CatalogProduct/etc/frontend/routes.xml
2) We need to override the Block:
\Magento\Catalog\Block\Product\ProductList\Item\AddTo\Compare
, because we need to build the Ajax url for each product. And, after adding product successfully, we need to reload compare product data (stored in browser storage).app/code/Vendor/CatalogProduct/etc/frontend/di.xml
app/code/Vendor/CatalogProduct/etc/frontend/sections.xml
3) Before building the Ajax controller logic and Js script, we need to custom the compare product layout.
app/code/Vendor/CatalogProduct/view/frontend/layout/catalog_category_view.xml
Change the link to form data:
app/code/Vendor/CatalogProduct/view/frontend/templates/compare/list/addto/compare.phtml
Need to add the Js script to template:
app/code/Vendor/CatalogProduct/view/frontend/templates/compare/list/js.phtml
Blocks:
Vendor\CatalogProduct\Block\Compare\Js
should extend from\Magento\Framework\View\Element\Template
.Vendor\CatalogProduct\Block\ProductList\Item\AddTo\Compare
will extend fromMagento\Catalog\Block\Product\ProductList\Item\AddTo\Compare
.4) Js script and Ajax controller Logic: followed the Magento core logic.
app/code/Vendor/CatalogProduct/view/frontend/web/js/add-to-compare.js
app/code/Vendor/CatalogProduct/Controller/Compare/AjaxAdd.php
Remember to define
registration.php
andmodule.xml
.Now, on the product listing page, we can use Ajax for compare product function. However, this module only works on Product listing page, we need to improve it, it should work on Product detail page.