以编程方式创建Magento Multiple Wishlist

时间:2014-06-05 19:35:37

标签: magento merge admin

我知道如何以编程方式将产品添加到客户的心愿单中,但它只会添加到一个心愿单中。我已将多个心愿单选项设置为已启用,但我不知道如何创建新心愿单,而不是将产品合并到现有心愿单中。

public function submitQuote(Mage_Adminhtml_Model_Session_Quote $quote)
{
    $currentQuote = $quote->getQuote();

    $customer = $currentQuote->getCustomer();

    $items = $currentQuote->getAllVisibleItems();

    //$wishlist = Mage::helper('wishlist')->getWishlist();
    //Mage::register('wishlist', $wishlist);
    $wishlist = Mage::getModel('wishlist/wishlist');
    $curretDate = date('m/d/Y', time());
    $wishlist->setCustomerId($customer->getId());
    $wishlist->setName('Quote ' . $curretDate)
    ->setVisibility(false)
    ->generateSharingCode()
    ->save();

    foreach ($items as $item)
    {
        $productId = $item->getProductId();
        $product = Mage::getModel('catalog/product')->load($productId);
        $buyRequest = $item->getBuyRequest();

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

    //Mage::unregister('wishlist');
}

2 个答案:

答案 0 :(得分:0)

我以前遇到过这个问题。要将产品添加到客户的心愿单,您需要启动心愿单模型并调用addNewItem方法传递产品对象。我就是这样做的。

$customer = Mage::getModel('customer/customer');
$wishlist = Mage::getModel('wishlist/wishlist');
$product = Mage::getModel('catalog/product');

$customer_id = 1;
$product_id = 1;
$customer->load($customer_id);
$wishlist->loadByCustomer($customer_id);
$wishlist->addNewItem($product->load($product_id));

希望这有帮助!

编辑:

$customerid= "YOUR CUSTOMER ID "; // Modify This
$customer=Mage::getModel("customer/customer")->load($customerid);
$wishlist=Mage::getModel("wishlist/wishlist")->loadByCustomer($customer,true);

检查参考:app / code / core / Mage / Wishlist / Model / Wishlist.php

最新编辑:

我看到你的问题在这里:

$result = $wishlist->addNewItem($product, $buyRequest);

最终编辑:

然后创建另一个函数并添加要添加到的项目和心愿单(作为参数)。这应该将项目添加到您在调用函数时作为参数传递的心愿单。它是我能想到的唯一解决方案。希望这有帮助!

答案 1 :(得分:0)

我偶然发现了这个问题,因为我尝试做类似的事情 - 你的代码部分帮助我创建了一个新的愿望清单,而且我已经成功地做了其余的事情,所以我想我就这样做了。分享。

此代码将;

创建一个名为'引用dd / mm / YY h:m:s'

的新愿望清单

加载当前用户的心愿单集合,按最高ID排序

加载最新的心愿单

将所有篮子物品添加到其中

将用户重定向到新的愿望清单视图。

public function addToQuoteAction() {

            if(Mage::getSingleton('customer/session')->isLoggedIn()) {
            $customerData = Mage::getSingleton('customer/session')->getCustomer();
            $customerId = $customerData->getId();
            } else {
                Mage::getSingleton('core/session')->addError($this->__('Please login to use this feature'));
                return $this->_redirectUrl($this->_getRefererUrl());
            }

            // Create the wishlist
            $newWishlist = Mage::getModel('wishlist/wishlist');
            $curretDate = date('d/m/Y h:m:s', time());
            $newWishlist->setCustomerId($customerId);
            $newWishlist->setName('Quote ' . $curretDate)
                ->setVisibility(false)
                ->generateSharingCode()
                ->save();

            // Find the newly create list and load it
            $wishlistCollection = Mage::getModel('wishlist/wishlist')->getCollection()
                                    ->addFieldToFilter('customer_id',$customerId)->setOrder('wishlist_id');;

            $firstWish = $wishlistCollection->getFirstItem();

            $wishlist = Mage::getModel('wishlist/wishlist')->load($firstWish->getWishlistId());
            $cart = Mage::getModel('checkout/cart')->getQuote();

            //getAllItems
            foreach ($cart->getAllVisibleItems() as $item) {
                                    $productId  = $item->getProductId();
                $buyRequest = $item->getBuyRequest();
                $result = $wishlist->addNewItem($productId, $buyRequest);
                $wishlist->save();
            }
            Mage::getSingleton('core/session')->addSuccess($this->__('All items have been moved to your quote'));
            return $this->_redirectUrl(Mage::getBaseUrl().'wishlist/index/index/wishlist_id/'.$firstWish->getWishlistId().'/');
    }