获得该产品的用户(MAGENTO)

时间:2012-03-01 04:51:39

标签: magento

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

如何让所有从B类购买产品A的用户

这样的mysql查询

SELECT用户从表用户,表产品......用户购买产品A。

请提出一些想法,我需要做这项工作。 谢谢

3 个答案:

答案 0 :(得分:13)

如果你想要一个实际的查询,你可以做一些简单的事情(添加额外的连接以从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());
}

虽然它很丑陋(实例化多个模型,在封面下执行一堆查询),你可能可以编写自己的模型来使它变得更漂亮。

答案 1 :(得分:0)

您必须根据订单查询。如果你想通过SQL查询来完成它,你必须通过下表:

  • sales_flat_quote,sales_flat_order_item以获取客户与产品之间的链接
  • catalog_category_product以获取类别和产品之间的链接
  • catalog_product_entity以获取sku功能的产品ID
  • ...
祝你好运

答案 2 :(得分:0)

尝试这些模型

$orders = Mage::getModel('sales/order')->addAttributeToSelect('*')->getCollection();

$order_items = Mage::getResourceModel('sales/order_item_collection')
->addAttributeToSelect('sku')
->addAttributeToSelect('created_at')
->addAttributeToSelect('order_id')
->addAttributeToFilter('order_id', array('in' => $orders_ids))->load();