获取已购买此产品的用户 (MAGENTO) [英] Get users who has bought this product (MAGENTO)
问题描述
magento 是否可以根据用户购买的产品过滤用户?例如.
Is it possible in magento to filter user based on the products they have bought? For eg.
如何获取所有购买过 B 类产品 A 的用户
How can I get all the users who have bought product A from category B
mysql 查询如
SELECT users from table users, table products ..... WHERE user has purchase product A .
SELECT users From table users, table products ..... WHERE user has purchased product A .
请给出一些想法,我需要完成这项工作.谢谢
Please give some ideas, I needed to make this work. Thanks
推荐答案
如果你想要一个实际的查询,你可能可以做一些简单的事情(添加额外的连接以从 EAV 获取客户信息):
If you want an actual query, you can probably do something as simple as (add additional joins to get customer information from EAV):
SELECT DISTINCT o.customer_id FROM sales_flat_order_item i
INNER JOIN sales_flat_order o ON o.entity_id = i.order_id
WHERE o.customer_id IS NOT NULL
AND i.sku = 'some-product-sku'
使用 Magento 模型,这应该适合您:
Using Magento models, this should work for you:
<?php
require_once 'app/Mage.php';
/*
* Initialize Magento. Older versions may require Mage::app() instead.
*/
Mage::init();
/**
* Get all unique order IDs for items with a particular SKU.
*/
$orderItems = Mage::getResourceModel('sales/order_item_collection')
->addFieldToFilter('sku', 'some-product-sku')
->toArray(array('order_id'));
$orderIds = array_unique(array_map(
function($orderItem) {
return $orderItem['order_id'];
},
$orderItems['items']
));
/**
* Now get all unique customers from the orders of these items.
*/
$orderCollection = Mage::getResourceModel('sales/order_collection')
->addFieldToFilter('entity_id', array('in' => $orderIds))
->addFieldToFilter('customer_id', array('neq' => 'NULL'));
$orderCollection->getSelect()->group('customer_id');
/**
* Now get a customer collection for those customers.
*/
$customerCollection = Mage::getModel('customer/customer')->getCollection()
->addFieldToFilter('entity_id', array('in' => $orderCollection->getColumnValues('customer_id')));
/**
* Traverse the customers like any other collection.
*/
foreach ($customerCollection as $customer) {
var_dump($customer->getData());
}
虽然它非常丑陋(实例化多个模型,在幕后执行一堆查询),但您可能可以编写自己的模型来使其更漂亮.
It's pretty ugly though (instantiates multiple models, executes a bunch of queries under the covers), you could probably write your own model to make this -a lot- prettier.
这篇关于获取已购买此产品的用户 (MAGENTO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!