magento - 获取购买过该产品的用户(MAGENTO)

标签 magento

magento 是否可以根据用户购买的产品来过滤用户? 例如。

如何获取从类别 B 购买过产品 A 的所有用户

mysql 查询类似

从表用户、表产品中选择用户......其中用户购买了产品 A 。

请提供一些想法,我需要完成这项工作。 谢谢

最佳答案

如果您想要实际查询,您可能可以做一些简单的事情(添加额外的联接以从 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 模型,这应该适合您:

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

虽然它非常丑陋(实例化多个模型,在幕后执行一堆查询),但您可能可以编写自己的模型来使其更漂亮。

关于magento - 获取购买过该产品的用户(MAGENTO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9510786/

相关文章:

php - magento 安装错误 "PHP extension "curl“必须加载”

magento - 通过local.xml文件更改 block 的顺序

php - Magento 自定义模块如何在 config.xml 中存储变量

xml - Magento 从 CMS 页面中删除 "Miscellaneous Scripts"

php - 如何从外部站点将带有 curl 的 POST 数据发送到 magento Controller 功能?

Magento如何为所有 Controller 定义布局句柄

php - Magento 1.9 Helper 找不到错误

php - 自动获取 Magento 中自定义属性的选定下拉值

database - 通过 SQL 脚本在 Magento 中创建邮件模板?

magento - 如何在 Magento 中编辑产品页面模板