Can anyone help me out? I want to add new products to the particular customer's wishlist automatically.

  $customerId = 2;
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $productCollection = $objectManager- >create('Magento\Catalog\Model\ResourceModel\Product\Collection');

 $collection = $productCollection->addAttributeToSelect('*')
  ->addAttributeToSort('updated_at', 'DESC')

 foreach($collection as $product){

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId());

$wishList = $objectManager->get('\Magento\Wishlist\Model\WishlistFactory');

$wishlistAdd = $wishList->create()->loadByCustomerId($customerId, true);



I highly recommend to inject Magento\Catalog\Model\Product and Magento\Wishlist\Model\WishlistFactory in your constructor.

This is for your just quick snippet.

Hope it will help you.

  • while using this code in template file i am getting this error."Parse error: syntax error, unexpected '$objectManagerr' (T_VARIABLE) in C:\xampp\htdocs\magento223\app\code\Wishorder\Asorder\view\frontend\templates\asorder.phtml on line 7" ---Please help me out – SUBBULAKSHMI G Apr 13 '18 at 10:03
  • Update your question with your code, So that i can help you out. – anonymous Apr 13 '18 at 10:05
  • the code you provided that only i have added in custom module template..nothing new i added – SUBBULAKSHMI G Apr 13 '18 at 10:07
  • Please paste properly, :) You messed up with $objectManager. There's $objectmanagerr in your error. :) – anonymous Apr 13 '18 at 10:09
  • the same one i tried after only i tried with objectManagerr..that too i got error – SUBBULAKSHMI G Apr 13 '18 at 10:11

You have use Magento\Wishlist\Model\Wishlist class.

First you need to load a customer wishlist by calling function loadByCustomerId()

Then add a new item to wish list using method addNewItem


namespace {NameSpaceofClass};
class {Classname}{

    protected $_wishlistFactory;

    public function __construct(
         \Magento\Wishlist\Model\WishlistFactory $wishlistFactory
       $this->_wishlistFactory = $wishlistFactory

    public function addAnewProduct()
        $wishlist = $this->_wishlistFactory->create();

        $wishlist->loadByCustomerId(15); //15 is Customer Id

                $wishlist->addNewItem(25); // 25 Product id

Note that:

addNewItem has 3 parameters

  1. $product can Product id OR product object which should be instance \Magento\Catalog\Model\Product.
  2. $buyRequest , it should be instance

    \Magento\Framework\DataObject or array or string or null

. 3. $forciblySetQty boolean true/false forcefully set qty.

  • where I need to use this code..in block or controller? – SUBBULAKSHMI G Apr 13 '18 at 9:20
  • You can call this at any where you want to use – Amit Bera Apr 13 '18 at 9:51
  • while using this code in block file i didnt get anything in the frontend. and its not adding the product to wishlish – SUBBULAKSHMI G Apr 13 '18 at 10:02
  • You need to make your block class not cache . OR You should add code at controller – Amit Bera Apr 13 '18 at 10:04
  • while putting in controller file, i am getting the error like this.. – SUBBULAKSHMI G Apr 13 '18 at 11:33

This is the code I have used in my controller

     namespace Wishorder\Asorder\Controller\Index;

      use \Magento\Framework\App\Action\Context;
      use   \Magento\Framework\View\Result\PageFactory ;
      use   \Magento\Wishlist\Model\WishlistFactory ;

      class Index extends \Magento\Framework\App\Action\Action

        protected $resultPageFactory;
        protected $wishlistFactory;

        public function __construct(Context $context
        \Magento\Framework\View\Result\PageFactory $resultPageFactory, 
        \Magento\Wishlist\Model\WishlistFactory $wishlistFactory
          $this->resultPageFactory = $resultPageFactory;
          $this->wishlistFactory = $wishlistFactory;
public function execute()
    $wishlist = $this->wishlistFactory->create();

    $wishlist->loadByCustomerId(1); //15 is Customer Id
    $resultPage = $this->resultPageFactory->create();
            $wishlist->addNewItem(25); // 25 Product id
            $wishlist->save(); // 25 Product id     
    return $this->resultPageFactory->create();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.