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

时间:2017-06-07 20:05:06

标签: php jquery ajax magento

我想在div中添加一条消息,比如5秒,

"产品已添加到购物车"在屏幕上。

目前,当点击按钮时,产品只是添加到购物车,但没有显示消息,产品已添加到购物车,这对于某些客户来说不清楚是否添加了产品。

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>

更新:相关的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>

2 个答案:

答案 0 :(得分:0)

首先,当您将产品添加到购物车时,Magento会给出消息。 Magento有两种类型的设置。

  1. 当客户在购物车中添加产品时,它会重定向到购物车页面并显示您的产品已添加到购物车中的消息。
  2. 当客户将产品添加到购物车中时,它将保留在同一页面上并显示您的产品已添加到购物车中的消息。
  3. 因此,当客户将产品添加到您网站的购物车中时,您必须检查为什么不显示Magento的默认消息。

答案 1 :(得分:0)

要显示消息,请替换代码

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); 

                            } 
                        }

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); 

                            } 
                        }