添加到购物车的Ajax不会将产品添加到购物车

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了添加到购物车的Ajax不会将产品添加到购物车相关的知识,希望对你有一定的参考价值。

* Ajax添加到购物车不会在弹出的onclick操作结帐/继续时将产品更新到购物车,然后重定向到URL http://hostip.com/checkout/cart/ *

我用过这个javascript

    <script type="text/javascript">
    function setAjaxData(data,iframe){
            if(data.status == 'ERROR'){
                alert(data.message);
            }else{
                if(jQuery('.block-cart')){
                    jQuery('.block-cart').replaceWith(data.sidebar);
                }
    //            if(jQuery('.header .links')){
    //                jQuery('.header .links').replaceWith(data.toplink);
    //            }
                if(jQuery('.cart-top-container')){
                    //jQuery('.cart-top-container').empty();
                    jQuery('.cart-top-container').replaceWith(data.toplink);
                }
                var targetUrl='<?php echo Mage::getUrl('onestepcheckout') ?>';
                jQuery( "#dialog-confirm" ).dialog({
                    resizable: false,
                    //height:140,
                    modal: true,
                    buttons: {
                        "Checkout": function() {
                            window.location.href = targetUrl;
                            //jQuery(this).dialog( "close" );
                        },
                        "Continue Shopping": function() {
                            jQuery(this).dialog( "close" );
                        }
                    }
                });
            }
        }
        function setLocationAjax(url,id){
            url += 'isAjax/1';
            url = url.replace("checkout/cart","ajax/index");
            jQuery('#ajax_loader'+id).show();
            try {
                jQuery.ajax( {
                    url : url,
                    dataType : 'json',
                    success : function(data) {
                        jQuery('#ajax_loader'+id).hide();
                        setAjaxData(data,false);          
                    }
                });
            } catch (e) {
            }
        }
    </script>




*The form to submit is*

<form action="<?php echo $this->getSubmitUrl($p) ?>" method="post" id="product_addtocart_form"<?php if ($p->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
                        <div class="home-prouduct-details">
                            <?php $pid = $p->getId(); ?>
                            <?php $prod = Mage::getModel('catalog/product')->load($pid);  ?>
                            <?php $html= $prod->getShortDescription(); ?>
                            <?php                          
                               $str = $html;
                               $matches = array();
                               preg_match_all('#<div[^>]*>(.*?)</div>#', $str, $matches);
//                               print_r($matches[1][0]);
                            ?>
                            <a class="prod-name" href="<?php echo $prod->getProductUrl(); ?>"><?php echo $prod->getName(); ?></a>
                            <div class="prod-img">
                                <a href="<?php echo $prod->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($prod, 'small_image'), null, true) ?>" class="product-image">
                                    <img src="<?php echo $this->helper('catalog/image')->init($prod, 'small_image')->resize(285,172); ?>" width="285" height="175" alt="<?php echo $this->stripTags($this->getImageLabel($prod, 'small_image'), null, true) ?>" />
                                </a>
                            </div>
                            <div class="home-price-button">
                                <?php $almost_gone_qty=$this->htmlEscape($prod->getData('almost_gone_qty')); ?>
                                <?php $qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($prod)->getQty(); ?>
                                <?php if (!$this->hasOptions() && strtotime($prod->getDealEndDate())>= Mage::getModel('core/date')->timestamp(time())):?>
                                    <div style="clear:both;"></div>
                                    <div class="add-to-box">
                                        <?php if($prod->isSaleable() && $qtyStock > $almost_gone_qty){ ?>
                                            <p class="availability out-of-stock">
                                                <a onclick="setLocationAjax('<?php echo Mage::helper('checkout/cart')->getAddUrl($prod); ?>',<?php echo $prod->getId();?>)" title="Add To Cart">
                                                    <img src="<?php echo $this->getSkinUrl('images/steamy.png'); ?>" title="Add To Cart" alt="Add To Cart" width="285" />
                                                </a>
                                            </p>
                                            <?php } else if($qtyStock <= $almost_gone_qty && $qtyStock!=0){?>
                                                <p class="availability out-of-stock">
                                                    <a onclick="setLocationAjax('<?php echo Mage::helper('checkout/cart')->getAddUrl($prod); ?>',<?php echo $prod->getId();?>)" title="Almost Gone">
                                                        <img src="<?php echo $this->getSkinUrl('images/almostgone.png'); ?>" title="Almost Gone" alt="Almost Gone" width="285" />
                                                    </a>
                                                </p>
                                            <?php } else{ ?>
                                                <p class="availability out-of-stock">
                                                    <a href="<?php echo Mage::getBaseUrl(); ?>sold-out" title="Sold Out">
                                                        <img src="<?php echo $this->getSkinUrl('images/soldout.png'); ?>" width="285" /><?php //echo $this->__('Out of stock') ?>
                                                    </a>
                                                </p>
                                            <?php } ?>
                                            <span id='ajax_loader<?php echo $prod->getId()?>' style='text-align: center; display:none'><img src='<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif')?>'/></span>
                                    </div>                                    
                                <?php else : ?>
                                    <p class="deal-has-ended">
                                        <a href="<?php echo Mage::getBaseUrl(); ?>sold-out" title="Sold Out">
                                            <img src="<?php echo $this->getSkinUrl('images/deal_ended.png'); ?>" width="285" />
                                        </a>
                                    </p>                                
                                <?php endif; ?>
                                    <?php echo $this->getChildHtml('addto') ?>
                                    <?php //echo $this->getChildHtml('extra_buttons') ?>
                            </div>
                            <div class="home-price">
                                <div class="home-prices">
                                    <?php 
                                        echo Mage::helper('core')->currency($prod->getPrice());
                                    ?>    
                                </div>
                                <?php if($matches[1][1] !=""){ ?>
                                    <div class="home-save">
                                        <?php
                                            $savings = $matches[1][1];
                                            $savings = str_ireplace('Rs.', '&#8377;', $savings);
                                            $savings = str_ireplace('Rs', '&#8377;', $savings);
                                            echo $savings;
                                        ?>
                                        <?php //print_r($matches[1][1]); ?>
                                        <div class="home-saver">
                                            savings
                                        </div>
                                    </div>
                                <?php } ?>
                                <div class="deal-endtime"> 
                                    <?php 
                                                    echo "<div id='cdcontainer_".$prod->getId()."' class='cdcontainer'></div>";
                                                    $dealEndDate = $prod->getDealEndDate();
                                                    $dealEndDateParts = explode(" ", $dealEndDate);
                                                    $targetDateParts = explode("-", $dealEndDateParts[0]);
                                                    $year = $targetDateParts[0];
                                                    $month = $targetDateParts[1];
                                                    $date = $targetDateParts[2];

                                                    $month_array = array(
                                                            "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
                                                    );
                                                    $month = $month_array[$month-1];

                                                    $targetDate = "$month $date, $year ".$dealEndDateParts[1];
                                                    $timer_text = Mage::getStoreConfig('homedeals/basic_settings/cd_display_text');
                                                    $timer_end_text = Mage::getStoreConfig('homedeals/basic_settings/cd_end_text');     
                                                    //echo $timer_end_text;
                                                    //echo date("Y-m-d H:i:s"); echo "<br/>";
                                                    //echo $dealEndDate;
                                                    if($dealEndDate <= date("Y-m-d H:i:s")){
                                                    ?>
                                                    <script type="text/javascript">
                                                            var launchdate_<?php echo $prod->getId(); ?>=new cdLocalTime("cdcontainer_<?php echo $_product->getId(); ?>", "server-php", 0, "", "", "<?php echo '<span><h1 style=color:red;>Deal has ended - 00:00:00</h1></span>' ?><?php //echo $timer_text; ?>", "<?php echo $timer_end_text; ?>", "<?php echo $prod->getId(); ?>");
                                                            launchdate_<?php echo $prod->getId(); ?>.displaycountdown("hours", formatresults2);
                                                    </script>
                                                    <?php   }else { ?>
                                                    <script type="text/javascript">
                                                            var launchdate_<?php echo $prod->getId(); ?>=new cdLocalTime("cdcontainer_<?php echo $prod->getId(); ?>", "server-php", 0, "<?php echo $targetDate?>", "<?php print date("F d, Y H:i:s", Mage::getModel('core/date')->timestamp(time()))?>", "<?php echo $timer_text; ?>", "<?php echo $timer_end_text; ?>", "<?php echo $prod->getId(); ?>");

                                                            launchdate_<?php echo $prod->getId(); ?>.displaycountdown("hours", formatresults2);
                                                    </script>
                                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                        <div id="dialog-confirm" style="display:none;">
                            <?php echo $prod->getName();?> added to your cart Successfully.
                        </div>
                        </form>
答案

请使用magento原型将产品添加到购物车。

这是参考模块。

http://www.excellencemagentoblog.com/magento-add-product-to-cart-ajax

另一答案
  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){
另一答案

您是否已发送附加图像等数据。

另一答案

您可以通过ajax按照以下教程链接添加到购物车功能。

教程链接:

http://blog.onlinebizsoft.com/magento-how-to-add-jqueryajax-add-to-cart/ http://www.ifuelinteractive.com/adding-to-the-cart-with-a-jquery-ajax-call-in-magento/

以上是关于添加到购物车的Ajax不会将产品添加到购物车的主要内容,如果未能解决你的问题,请参考以下文章

将产品添加到购物车 jQuery/Ajax/Symfony/Twig

我想在购物车中添加产品而不用 AJAX 和 Django 刷新页面

WooCommerce 产品循环中添加 Ajax 选项的复选框添加到购物车

用于 WooCommerce 3 中产品变化的 Ajax 添加到购物车按钮

AJAX 添加到购物车按钮不适用于自定义查询循环产品 woocommerce

ajax 上的 JS 警报添加到购物车以获取 Woocommerce 中的特定产品类别计数