magento按产品ID和客户ID排序

时间:2016-04-28 13:16:02

标签: php magento magento-1.9

如何获得包含特定产品的特定客户订购的所有magento订单? 我尝试了以下方法:

        $customer_id = Mage::getSingleton('customer/session')->getCustomerId();
        $customer_eamil = Mage::getSingleton('customer/session')->getCustomerEmail();


        $event = $observer->getEvent();
        $product = $event->getProduct();
        $product->original_price = $product->getPrice();
        $productID = $product->getId();

        $fromDate = date('Y-m-d H:i:s', strtotime(date('Y-01-01')));
        $toDate = date('Y-m-d H:i:s');

        $orders = Mage::getResourceModel('sales/order_item_collection')
            ->addFieldToSelect('*')
            ->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate))
            ->addAttributeToFilter('product_id', array('eq' => $productID))
            ->addAttributeToFilter('customer_id', $customer_id)
        ;

但我得到的是我的magento error.log中的一个错误: 异常' PDOException'消息' SQLSTATE [42S22]:未找到列:1054未知列' customer_id'在' where子句''

我正在使用magent 1.9.2

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

请检查以下代码以获得所需结果:

$customer_id = Mage::getSingleton('customer/session')->getCustomerId();
$customer_eamil = Mage::getSingleton('customer/session')->getCustomerEmail();

$event = $observer->getEvent();
$product = $event->getProduct();
$product->original_price = $product->getPrice();
$productID = $product->getId();

$fromDate = date('Y-m-d H:i:s', strtotime(date('Y-01-01')));
$toDate = date('Y-m-d H:i:s');

$orders = Mage::getResourceModel('sales/order_item_collection')
        ->addFieldToSelect('*')
        ->addAttributeToFilter('created_at', array('from' => $fromDate, 'to' => $toDate))
        ->addAttributeToFilter('product_id', array('eq' => $productID));

$orders->getSelect()->join(array('sales_order' => Mage::getSingleton('core/resource')->getTableName('sales/order')), 'main_table.order_id = sales_order.entity_id and customer_id=' . $customer_id, array('sales_order.entity_id'));

echo "<pre>";
print_r($orders->getData());
exit; 

请使用DB中的上述过滤器检查可用数据。