cancel
Showing results for 
Search instead for 
Did you mean: 

Get Customer Details by Wishlist Product Id

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Get Customer Details by Wishlist Product Id

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.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Get Customer Details by Wishlist Product Id

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!!!

View solution in original post

5 REPLIES 5

Re: Get Customer Details by Wishlist Product Id

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!!!

Re: Get Customer Details by Wishlist Product Id

Hi @Kavitha Mano

 

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.

 

Re: Get Customer Details by Wishlist Product Id

Hi @Kavitha Mano

 

Thanks for your suggestion...It worked for me...

Re: Get Customer Details by Wishlist Product Id

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/

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Get Customer Details by Wishlist Product Id

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....!