根据客户数量将产品列表添加到愿望清单中,我需要根据产品ID知道哪个客户将特定产品添加到了愿望清单中。
如何获取客户详细信息或基于产品ID至少获取客户ID?
答案 0 :(得分:0)
为我工作的代码如下:
首先,我要从wishlist_item表中获取集合,并使用我的产品ID进行过滤。
$wishlistItemCollection = $objectManager->get('Magento\Wishlist\Model\ResourceModel\Item\Collection')->addFieldToFilter('product_id', ['eq' => $productId]);
然后使用通用字段名称wishlist_id加入wishlist_item和wishlist表,并将customer_id放入wishlistItemCollection
$joinConditions = 'main_table.wishlist_id = wishlist.wishlist_id';
$wishlistItemCollection->getSelect('*')->join(
['wishlist'],
$joinConditions,
[]
)->columns("wishlist.customer_id");
使用customer_id,我们可以获得以下所有客户详细信息:
foreach($wishlistItemCollection as $wishlistData){
$customerId = $wishlistData->getCustomerId();
$customer = $this->_customer->load($customerId);
$customerData = $customer->getData();
$customerName = $customer->getFirstname();
$customerEmail = $customer->getEmail();
}