2015-09-09 3 views
0

Я добавил код в свой контроллер, чтобы программно добавить продукт в список пожеланий. Но всякий раз, когда запрос ajax переходит к контроллеру, он заменяет мой ранее добавленный продукт из списка пожеланий и добавляет новый в список пожеланий.Magento: Добавить продукт в список пожеланий программно

Может кто-нибудь помочь с этим.

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"; 
     } 

ответ

0

Вы можете попробовать ниже:

$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.')); 
     }