Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento:通过编程将产品添加到愿望列表中_Magento - Fatal编程技术网

Magento:通过编程将产品添加到愿望列表中

Magento:通过编程将产品添加到愿望列表中,magento,Magento,我在我的控制器中添加了以下代码,以编程方式将产品添加到wishlist。但是,每当ajax请求到达控制器时,它就会替换我以前从wishlist添加的产品,并将新产品添加到wishlist中 谁能帮我一下吗 if (Mage::getSingleton('customer/session')->isLoggedIn()) { // Load the customer's data $customer = Mage::getSingleton('

我在我的控制器中添加了以下代码,以编程方式将产品添加到wishlist。但是,每当ajax请求到达控制器时,它就会替换我以前从wishlist添加的产品,并将新产品添加到wishlist中

谁能帮我一下吗

if (Mage::getSingleton('customer/session')->isLoggedIn()) {
            // Load the customer's data
            $customer = Mage::getSingleton('customer/session')->getCustomer();

            //echo $customer->getName(); // Full Name
            $customerId = $customer->getId(); // First Name


            $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customerId, true);
            $product = Mage::getModel('catalog/product')->load($productId);
            $result = $wishlist->addNewItem($product);
            $wishlist->save();
            echo "added to wishlist";
        }
您可以在下面尝试:

  $product = Mage::getModel('catalog/product')->load($productId);
        if (!$product->getId() || !$product->isVisibleInCatalog()) {
            //$this->__('Cannot specify product.');


        }

        try {
            $requestParams = $this->getRequest()->getParams();

            $buyRequest = new Varien_Object($requestParams=array());

            $result = $wishlist->addNewItem($product, $buyRequest);
            if (is_string($result)) {
                Mage::throwException($result);
            }

            $wishlist->save();

            Mage::dispatchEvent(
                'wishlist_add_product',
                array(
                    'wishlist' => $wishlist,
                    'product' => $product,
                    'item' => $result
                )
            );



            Mage::helper('wishlist')->calculate();

            $message = $this->__('%1$s has been added to your wishlist. Click <a href="%2$s">here</a> to continue shopping.',
                $product->getName(), Mage::helper('core')->escapeUrl($referer));

        } catch (Mage_Core_Exception $e) {
           echo $this->__('An error occurred while adding item to wishlist: %s', $e->getMessage());
        }
        catch (Exception $e) {
            echo ($this->__('An error occurred while adding item to wishlist.'));
        }