Hi,
A list of products was added to wishlist by the number of customers and i need to know that which customer was added a particular product to the wishlist, based on the product id.
I need to get the customer details or atleast a customer id based on the product id.
Please suggest any solution.
Thanks,
KiranL.
Solved! Go to Solution.
Every wishlist_item table consists of the item with the wishlist id. you have to load the collection of the wishlist_item and get the id then use the id to load wishlist table. It will return the wishlist details with customer_id. you can get the customer details from the customer_id.
Thanks!!!
Every wishlist_item table consists of the item with the wishlist id. you have to load the collection of the wishlist_item and get the id then use the id to load wishlist table. It will return the wishlist details with customer_id. you can get the customer details from the customer_id.
Thanks!!!
Can you please provide the related code to get the wishlist details with customer id in helper data.php.
I am trying to get the wishlist collection but getting an empty array.
You need to left join wishlist with wishlist_item table in the database to get Customer id from a product id.
Wishlist table contains customer id and wishlist_item table contains product id.
Your query looks like,
SELECT wi.`wishlist_id`,w.customer_id FROM `wishlist_item` as wi left join `wishlist` as w on wi.wishlist_id = w.wishlist_id WHERE wi.`product_id` = 438 group by `wishlist_id`
Check the link for direct SQL query, Direct sql query
Now you get a result of wishlist id and customer id based on customer id you can get load the details of Customer.
You can load the customer details by customer id using https://www.rakeshjesadiya.com/how-to-get-customer-data-by-customer-id-magento-2/
The worked code for me:
First iam getting the collection from the wishlist_item table and filter with my product id.
$wishlistItemCollection = $objectManager->get('Magento\Wishlist\Model\ResourceModel\Item\Collection')->addFieldToFilter('product_id', ['eq' => $productId]);
Then joined the wishlist_item and wishlist table using the common field name wishlist_id and gets the customer_id into the wishlistItemCollection.
$joinConditions = 'main_table.wishlist_id = wishlist.wishlist_id'; $wishlistItemCollection->getSelect('*')->join( ['wishlist'], $joinConditions, [] )->columns("wishlist.customer_id");
Using the customer_id we can get all the customer details as below:
foreach($wishlistItemCollection as $wishlistData){ $customerId = $wishlistData->getCustomerId(); $customer = $this->_customer->load($customerId); $customerData = $customer->getData(); $customerName = $customer->getFirstname(); $customerEmail = $customer->getEmail(); }
Thanks....!