1

A list of products was added to wish-list by the number of customers and I need to know that which customer was added a particular product to the wish-list, based on the product id.

I need to get the customer details or at-least a customer id based on the product id.

3
  • 1
    This's your custom function or core magento. In module-wishlist, you can get customer id based on the product id by join 2 tables wishlist and wishlist_item
    – rocky
    Jan 5, 2019 at 7:18
  • Hi @AdityaShah the below example code wishlist->getItem doesn't work for me. Jan 6, 2019 at 5:36

2 Answers 2

1

Get data by Item ID

private $wishlist;

public function __construct(
    ...
    \Magento\Wishlist\Model\Wishlist $wishlist
) {
    $this->wishlist = $wishlist;
    ...
}

...
$item_id= 1;
$wishlistItem_collection = $this->wishlist->getItem($item_id);

This will return Item data collection including customer ID.

Wishlist Model

0

The worked code for me as below:

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();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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