2012-11-23 44 views
1

場景:
希望通過複選框多個產品添加到購物車從心願,因此,有他們的數量以及所選擇的產品轉移到購物車和所選擇的項目是從心願表中刪除。
推薦了很多博客。
我試圖通過(應用)添加相關產品javascript(邏輯)來實現它。
但仍然沒有得到。
的Magento通過複選框多種產品添加到購物車從心願

[更新]

這是複選框列(暫且硬編碼)

<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox7" name="related_products[]" value="7"> 

新增的JavaScript這一部分:

<script type="text/javascript"> 
    //<![CDATA[ 
    $$('.related-checkbox').each(function(elem){ 
     Event.observe(elem, 'click', addRelatedToProduct) 
    }); 

    var relatedProductsCheckFlag = false; 
    function selectAllRelated(txt){ 
     if (relatedProductsCheckFlag == false) { 
      $$('.related-checkbox').each(function(elem){ 
       elem.checked = true; 
      }); 
      relatedProductsCheckFlag = true; 
      txt.innerHTML="unselect all"; 
     } else { 
      $$('.related-checkbox').each(function(elem){ 
       elem.checked = false; 
      }); 
      relatedProductsCheckFlag = false; 
      txt.innerHTML="select all"; 
     } 
     addRelatedToProduct(); 
    } 

    function addRelatedToProduct(){ 
     var checkboxes = $$('.related-checkbox'); 
     var values = []; 
     for(var i=0;i<checkboxes.length;i++){ 
      if(checkboxes[i].checked) values.push(checkboxes[i].value); 
     } 
     if($('related-products-field')){ 
      $('related-products-field').value = values.join(','); 
     } 
    } 
    //]]> 
    </script> 

添加到購物車
<button type="button" title="Add to Cart" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span>Add to Cart</span></span></button>

和JavaScript這一部分:提前

+0

給我們一些代碼工作 –

+0

@AkhilSekharan任何想法如何實現? –

回答

1

在點擊

<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; 
       try { 
        this.form.submit(); 
       } catch (e) { 
       } 
       this.form.action = oldUrl; 
       if (e) { 
        throw e; 
       } 

       if (button && button != 'undefined') { 
        button.disabled = true; 
       } 
      } 
     }.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> 

感謝(全部添加到購物車)的形式採取行動去/心願/更新部分在這裏這一部分。 添加updatenew功能,並添加以下代碼

public function updatenewAction() 
    { 
      $post = $this->getRequest()->getPost(); 
      $product_id = $qtyarr=array(); 
      $product_id = $this->getRequest()->getPost('product123'); 
      $qtyarr = $this->getRequest()->getPost('qty'); 
      if($product_id) 
      { 
       $procol = Mage::getModel('catalog/product');     
       $cart = Mage::getModel('checkout/cart'); 
       foreach($product_id as $productid => $wishlistid) 
        { 
         $proid = $procol->load($productid); 
         $_product = $procol->load($productid); 
         $proname = $procol->getName(); 
         $quantity = $qtyarr[$wishlistid] ; 
         $cart->init(); 
         $cart->addProduct($productid,$quantity); 
         $cart->save(); 
         Mage::getSingleton('core/session')->addSuccess($proname.' moved from Wishlist'); 

         Mage::getModel('wishlist/item')->load($wishlistid)->delete(); 
        } 
        return $this->_redirectUrl(Mage::helper('checkout/cart')->getCartUrl()); 
      } 
     else 
     {   
      Mage::getSingleton('core/session')->addError('No Product Selected'); 
      $this->_redirect('wishlist'); 
     } 

    } 

在我們加入產品進入購物車程序,同時我們從願望清單中刪除它上面的代碼。
首先在default/template/wishlist/item/column/image.phtml

<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $product->getId()?>" name="product123[<?php echo $product->getId() ?>]" value="<?php echo $item->getId() ?>" /> 

您可以相應地調整它。
現在在複選框部分通過product_id和wishlist_id與產品數量(已有數量的字段在那裏)中的值。
ALA! ! !

+0

就像一個魅力。 。 。 –

相關問題