【问题标题】:magento filter order by product id and customer idmagento 按产品 ID 和客户 ID 过滤订单
【发布时间】:2016-04-28 13:16:02
【问题描述】:

如何获取特定客户订购的包含特定产品的所有 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 列的表!
  • 是的,我知道。但是我怎样才能将这两个条件结合起来呢?

标签: php magento magento-1.9


【解决方案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; 

请在数据库中使用上述过滤器检查可用数据。

【讨论】:

  • 比你的回答。它没有工作.. 我得到一个完整性约束违规:异常 'PDOException' 并带有消息 'SQLSTATE[23000]:完整性约束违规:1052 列 'created_at' in where 子句不明确'
  • 请删除 created_at 并尝试一次,因为上面的脚本在这里工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-20
  • 1970-01-01
相关资源
最近更新 更多