Magento 2 Tutorial: How To Add Product To Wishlist Programmatically?

So in this article, we will explain how wishlists work and show you how to add products to the wishlist programmatically.
BSS Commerce is one of the leading Magento extension providers and web development services in the world. With experienced and certified Magento developers, we commit to bringing high-quality products and services to optimize your business effectively. Furthermore, we offer FREE Installation – FREE 1-year Support and FREE Lifetime Update for every Magento extension.
<?php class AddProductToWishlist
{
protected $_wishlistFactory;
protected $_wishlistResource; public function __construct(
MagentoWishlistModelWishlistFactory $wishlistFactory,
MagentoWishlistModelResourceModelWishlist $wishlistResource
) {
$this->_wishlistFactory = $wishlistFactory;
$this->_wishlistResource = $wishlistResource;
} /**
* @param $product
* @param $customerId
*
* @throws MagentoFrameworkExceptionAlreadyExistsException
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function saveProductToWishlist($product, $customerId)
{
//load wishlist by customer id
$wishlist = $this->_wishlistFactory->create()->loadByCustomerId($customerId, true); //add product for wishlist
$wishlist->addNewItem($product); //save wishlist
$this->_wishlistResource->save($wishlist);
}
}

For store owners, wishlists give them insight into what customers’ want. From there, you can easily identify trends and optimize your business tactics.

magento-2-add-to-wishlist-programmatically

What Is A Wishlist?

Moreover, it also allows export items of the wishlist into a CSV file.
In this article, we have guided you step by step on how to add products to the wishlist programmatically in Magento 2.
A wishlist signifies a customer’s interest in a product without an immediate intent to purchase it.
Let’s go!
This module also enables validating data in the CSV file before importing products to wishlists.
Writing is a part of my life and I’m living for it.

How To Add Products To Wishlist In Magento 2 Programmatically?

We hope this blog is helpful and good luck to you!

  • Product Object: Can be obtained by Product SKU or Id. For example, use this method to obtain the product from SKU: MagentoCatalogApiProductRepositoryInterface::get($sku)
  • Customer Id: On frontend customer ID can be obtained for the currently logged customer using this method: MagentoCustomerModelSession::getId()

Magento 2 Import Export Wishlist Item by BSS
magento-2-export-import-wishlist-item-bss
Wish list is a default Magento feature that allows registered customers to create their own personalized collections of products they want to buy in the future.

magento-2-add-to-wishlist-programmatically
Use the following code:

Best Solution To Add Multiple Products To Magento Wishlist

You just need to prepare a standard CSV file with all necessary information (products SKU, quantity, users’ email, store views, etc.). And it allows you to easily start importing the CSV.
To save the product, load the customer’s wish list then add the product via the Wishlist model.

magento-2-export-import-wishlist-item-bss
Table of Contents

Thus, hereby we want to introduce you to the best solution to add multiple products to Magento wishlist:
For customers, a wishlist gives them the opportunity to save items for later if they can’t purchase it right at that moment and find those items quickly whenever they return to the store.
This is the perfect extension to import products to wishlist in bulk via a CSV file.
Although you can add products to customers’ wishlists using code, you can only do it one by one, and it’s a very time-consuming process. Not to mention manual work can lead to many mistakes.
However, some store owners are still confused about Magento 2 Wishlist and how to use it.

Conclusion

First of all, you need to create 2 parameters: product object and customer id.
Retailers can also analyze the data to see what items people save for later and what items they actually buy, thus identifying the issues that harm the sales: Lack of information? Wrong pricing strategy? Etc.
Other than improving the customer’s experience, wishlists also provide a deep, strategic value to online retailers. 
Magento 2 Wishlist is a very beneficial feature that having this feature is a must for every eCommerce website.


CONTACT NOW to let us know your problems. We are willing to support you every time.