1

场景:
想要通过复选框将多个产品从愿望清单添加到购物车,以便将所选产品及其数量转移到购物车,并将所选项目从愿望清单表中删除。
参考了很多博客。
我试图通过(应用)添加相关产品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>

提前致谢

4

1 回答 1

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 以及 post 值中的产品数量(已经有 qty 字段)。
阿拉斯!!!

于 2012-12-03T07:54:31.520 回答