magento通过复选框将多个产品从心愿单添加到购物车

时间:2012-11-23 04:26:17

标签: javascript magento cart

场景:想要通过复选框将多个产品从愿望清单添加到购物车,以便将所选产品及其数量转移到购物车,并从中删除所选项目心愿表。
提到了很多博客 我尝试通过(应用)添加相关产品javascript(逻辑)来实现它。
但仍然没有得到。

[更新]

这是复选框列(时间是硬编码)

<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:

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

提前致谢

1 个答案:

答案 0 :(得分:1)

单击(将所有内容添加到购物车)后,表单操作将转至/ wishlist / update部分。 添加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! ! !