简体   繁体   English

为magento中的所有产品创建一个JS函数,以便使用Ajax方法添加到购物车

[英]Create a single JS function for all products in magento for add to cart using Ajax method

I have created a instant add to cart buttons successfully in Magento. 我已经在Magento中成功创建了一个即时添加到购物车按钮。 I have done it using Forms and Java script ie each product displayed on the page has its own form and its won JavaScript function created on the form accordingly. 我已经使用Forms和Java脚本完成了该操作,即页面上显示的每个产品都有其自己的表单,并且相应地在表单上创建了自己的JavaScript函数。

So, If I display 60 products in one page, it leads to 60forms and 60 js functions. 因此,如果我在一页中显示60个产品,它将导致60forms和60 js函数。 Is there anyway where I can use a single JS for all the forms/products? 无论如何,我可以对所有表单/产品使用单个JS吗? ...I have tried and looked everywhere but unable to get a solution. ...我尝试过并四处查看,但无法找到解决方案。

Here is the Form and JS: 这是Form和JS:

 <form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form<?php echo $this->htmlEscape($_product->getId())?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>


   <button1  title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cartsd" onclick="productAddToCartForm<?php echo $this->htmlEscape($_product->getId())?>.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
    <?php echo $this->getChildHtml('', true, true) ?>
    </form>

JS : JS:

 var productAddToCartForm<?php echo $this->htmlEscape($_product->getId())?> = new VarienForm('product_addtocart_form<?php echo $this->htmlEscape($_product->getId())?>');

     productAddToCartForm<?php echo $this->htmlEscape($_product->getId())?>.submit = function(button) {

         if (this.validator.validate()) {
             var form = this.form;
             var e = null;
             var oldLabel;

             try {
                 this.form.request({
                     parameters: { adeptaajax:'true' },
                     onSuccess: function(transport){
                         var json = transport.responseText.evalJSON();
                         var displayString = json.message;
                         if(json.success) {
                             jQuery("#topCartContent").load("page/ #element > *");
                             jQuery("#cartHeader1").load("page/ #element2 > *");
                         }
                         alert(displayString);
                     },
                     onFailure: function(transport){

                         alert("<?php echo $this->__('There has been a problem adding your product. Please try again later.'); ?>");
                     }
                 });
             } catch (e) {
             }
             if (e) {
                 throw e;
             }
         }
     }.bind(productAddToCartForm<?php echo $this->htmlEscape($_product->getId())?>);
 </script>

As you dont want to fire 60 requests and there is no checkout cart controller action which is able to add more than one product, you propably have to write your own addMultipleAction in a class extending Mage_Checkout_CartController . 由于您不希望触发60个请求,并且没有可以添加多个产品的结帐购物车控制器操作,因此您可能必须在扩展Mage_Checkout_CartController的类中编写自己的Mage_Checkout_CartController

There is a blog entry i found via a 2 second google search which seems to do exactly this: 我通过2秒钟的Google搜索发现了一个博客条目,该条目似乎正是这样:

http://www.danneh.org/2010/09/adding-multiple-products-to-the-cart-simultaneously-in-magento/ http://www.danneh.org/2010/09/adding-multiple-products-to-the-cart-simultaneously-in-magento/

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

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