簡體   English   中英

magento按產品ID和客戶ID篩選訂單

[英]magento filter order by product id and customer id

如何獲得包含特定產品的特定客戶訂購的所有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中的錯誤:消息消息為'SQLSTATE [42S22]的異常'PDOException':找不到列:1054'where子句'中的未知列'customer_id'

我正在使用magent 1.9.2

謝謝您的幫助。

請檢查以下代碼以獲得期望的結果:

$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; 

請使用數據庫中的上述過濾器檢查可用數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM