简体   繁体   English

Magento:添加消息以添加到购物车Ajax按钮

[英]Magento: add message to add to cart Ajax button

I want to add a message in a div for say 5 seconds, 我想在div中添加一条消息,例如5秒钟,

"Product added to cart" on screen. 屏幕上显示“产品已添加到购物车”。

As presently, when clicking on the button the product is simply added to the cart, but no message is displayed the product is added to the cart, which for some customers is not clear, whether the product is added. 如当前,当单击按钮时,仅将产品添加到购物车,但未显示任何消息,表明该产品已添加到购物车,对于某些客户来说,不清楚是否添加了该产品。

The relevant Magento code for add to card is like this:

<div class="add-to-cart-buttons -field -field-actions -clearfix">
<button type="button" title="<?php echo $buttonTitle ?>" 
class="button btn-cart -button -button-wide add-to-bag -button-fancy"
onclick="productAddToCartForm.submit(this)"><span><span>
<?php echo $buttonTitle ?></span></span></button>
<?php echo $this->getChildHtml('', true, true) ?></div>

update: related Javascript 更新:相关的Javascript

<script type="text/javascript">
    //<![CDATA[
    var ajaxRequest = true;
        var productAddToCartForm = new VarienForm('product_addtocart_form');
        productAddToCartForm.submit = function(button, url) {
            if (this.validator.validate()) {
                var form = this.form;
                var oldUrl = form.action;

                if (url) {
                   form.action = url;
                }
               if(ajaxRequest) { 
                    new Ajax.Request('<?php echo $this->getUrl('sischeckout/cart/add'); ?>',{
                        method: "POST",
                        parameters:form.serialize(),
                        onSuccess: function(transport) {
                            try{ 

                           data = JSON.parse(transport.responseText); 

                                }catch(e){ data = {};
                            } 
                            if(!data.status) {
                                 alert(data.message); 

                            } 
                        }, 
                        onFailure: function(transport) {
                            alert('Invalid Request'); 
                        }
                    });
                } else {
                    var e = null;
                    try {
                        this.form.submit();
                    } catch (e) {
                    }

                    this.form.action = oldUrl;
                    if (e) {
                        throw e;
                    }

                    if (button && button != 'undefined') {
                        button.disabled = true;
                    }
                } 
            } else {
                    if(ajaxRequest) {
                        alert("Product options are required field.");
                    }
                }
        }.bind(productAddToCartForm);

        productAddToCartForm.submitLight = function(button, url){
            if(this.validator) {
                var nv = Validation.methods;
                delete Validation.methods['required-entry'];
                delete Validation.methods['validate-one-required'];
                delete Validation.methods['validate-one-required-by-name'];
                // Remove custom datetime validators
                for (var methodName in Validation.methods) {
                    if (methodName.match(/^validate-datetime-.*/i)) {
                        delete Validation.methods[methodName];
                    }
                }

                if (this.validator.validate()) {
                    if (url) {
                        this.form.action = url;
                    }
                    this.form.submit();
                }
                Object.extend(Validation.methods, nv);
            }
        }.bind(productAddToCartForm);
    //]]>
    </script>

First of all, Magento give message when you add products in to cart. 首先,Magento在将产品添加到购物车时会显示消息。 There are two types of settings are available in Magento. Magento有两种类型的设置。

  1. When customer add product in cart then it will redirect to cart page and display message that your product added in cart. 当客户在购物车中添加产品时,它将重定向到购物车页面并显示您的产品已添加到购物车中的消息。
  2. When customer add product the cart then it will stay on same page and display message that your product added in cart. 当客户将产品添加到购物车时,它将停留在同一页面上并显示您的产品已添加到购物车的消息。

So you have to check why Magento's default message is not display when customer add product in to cart in your site. 因此,当客户将产品添加到您网站上的购物车中时,您必须检查为什么未显示Magento的默认消息。

For showing message, Please Replace the code 要显示消息,请替换代码

onSuccess: function(transport) {
                            try{ 

                           data = JSON.parse(transport.responseText); 
                            jQuery(.cart .page-title).after("<ul class="messages"><li class="success-msg"><ul><li><span>Product was added to your shopping      cart.</span></li></ul></li></ul>");
                                }catch(e){ data = {};
                            } 
                            if(!data.status) {
                                 alert(data.message); 

                            } 
                        }

To

onSuccess: function(transport) {
                            try{ 

                           data = JSON.parse(transport.responseText); 
                           jQuery(".cart > .page-title").after("<ul class='messages'><li class='success-msg'><ul><li><span>Product was added to your shopping       cart.</span></li></ul></li></ul>");
                                }catch(e){ data = {};
                            } 
                            if(!data.status) {
                                 alert(data.message); 

                            } 
                        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM