使用magento 1.9中的ajax从视图页面更新购物车

时间:2016-03-01 07:25:01

标签: jquery ajax magento

添加到购物车在视图页面上正常工作。项目动态更新。但更新购物车不会更新标题购物车项目。刷新后项目会更新。

结帐/购物车/项目/配置/ updatecart.phtml

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Update Cart'); ?> 
<?php if($_product->isSaleable()): ?>
<div class="add-to-cart">
    <?php if (!$_product->isGrouped()): ?>
    <div class="qty-wrapper">
        <label for="qty"><?php echo $this->__('Qty:') ?></label>
        <input type="text" pattern="\d*" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
    </div>
    <?php endif; ?>
    <div class="add-to-cart-buttons">
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <span id='ajax_loader' style='display:none'><img src='<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif')?>'/></span>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
</div>

view.phtml

    <script type="text/javascript">
//<![CDATA[
    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;
        }
        var e = null;
        // Start of our new ajax code
        if (!url) {
            url = jQuery('#product_addtocart_form').attr('action');
        }
        url = url.replace("checkout/cart","ajax/index"); // New Code
        var data = jQuery('#product_addtocart_form').serialize();
        data += '&isAjax=1';
        jQuery('#ajax_loader').show();
            try {
                jQuery.ajax({
                  url: url,
                  dataType: 'json',
                  type : 'post',
                  data: data,
                  success: function(data){     
                      console.log(data);
                        jQuery('#ajax_loader').hide();
                        alert(data.message);
                        //console.log(url);
                        if(jQuery('#gd_total')){ 
                             jQuery("#gd_total").html( jQuery(data).find('#gd_total'));
                        }

                        if(jQuery('.count')){
                             var count=jQuery(data.toplink).find('.count').text();
                             jQuery(".count").html(count);
                              jQuery(".skip-cart").removeClass("no-count");
                        }      
                        if(jQuery('.minicart-wrapper')){ 
                           // jQuery('.header-minicart').replaceWith(data.toplink);
                           var cart=jQuery(data.toplink).find('.minicart-wrapper').html();
                             jQuery(".minicart-wrapper").html(cart);
                        }                             

                  }
            });
            }  catch (e) {
        }
        // End of our new ajax code
        this.form.action = oldUrl;
        if (e) {
            throw e;
        }
    }
}.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'];
            if (this.validator.validate()) {
                if (url) {
                    this.form.action = url;
                }
                this.form.submit();
            }
            Object.extend(Validation.methods, nv);
        }
    }.bind(productAddToCartForm);
//]]>
</script>

0 个答案:

没有答案