Magento 2 Wishlist is a very beneficial feature that having this feature is a must for every eCommerce website.
However, some store owners are still confused about Magento 2 Wishlist and how to use it.
So in this article, we will explain how wishlists work and show you how to add products to the wishlist programmatically.
Let’s go!
What Is A Wishlist?
Table of Contents
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.
A wishlist signifies a customer’s interest in a product without an immediate intent to purchase it.
Other than improving the customer’s experience, wishlists also provide a deep, strategic value to online retailers.
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.
For store owners, wishlists give them insight into what customers’ want. From there, you can easily identify trends and optimize your business tactics.
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.
How To Add Products To Wishlist In Magento 2 Programmatically?
First of all, you need to create 2 parameters: product object and customer id.
- Product Object: Can be obtained by Product SKU or Id. For example, use this method to obtain the product from SKU: Magento\Catalog\Api\ProductRepositoryInterface::get($sku)
- Customer Id: On frontend customer ID can be obtained for the currently logged customer using this method: Magento\Customer\Model\Session::getId()
To save the product, load the customer’s wish list then add the product via the Wishlist model.
Use the following code:
<?php
class AddProductToWishlist
{
protected $_wishlistFactory;
protected $_wishlistResource;
public function __construct(
Magento\Wishlist\Model\WishlistFactory $wishlistFactory,
Magento\Wishlist\Model\ResourceModel\Wishlist $wishlistResource
) {
$this->_wishlistFactory = $wishlistFactory;
$this->_wishlistResource = $wishlistResource;
}
/**
* @param $product
* @param $customerId
*
* @throws \Magento\Framework\Exception\AlreadyExistsException
* @throws \Magento\Framework\Exception\LocalizedException
*/
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);
}
}
Below is the result. The product has been added successfully to the user’s wish list in the frontend.
Best Solution To Add Multiple Products To Magento Wishlist
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.
Thus, hereby we want to introduce you to the best solution to add multiple products to Magento wishlist:
Magento 2 Import Export Wishlist Item by BSS
This is the perfect extension to import products to wishlist in bulk via a CSV file.
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.
This module also enables validating data in the CSV file before importing products to wishlists.
Moreover, it also allows export items of the wishlist into a CSV file.
Conclusion
In this article, we have guided you step by step on how to add products to the wishlist programmatically in Magento 2.
We hope this blog is helpful and good luck to you!
LEARN MORE: Add products to category automatically using Magento 2 Dynamic Category!
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.
CONTACT NOW to let us know your problems. We are willing to support you every time.