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.

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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