我在代码块中使用以下代码来提取所有订单,
public function getOrders()
{
if (!($customerId = ObjectManager::getInstance()
->create('Magento\Customer\Model\SessionFactory')->create()
->getCustomer()->getId())) {
return false;
}
if (!$this->orders) {
$this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
'*'
)->addFieldToFilter(
'status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
)->setOrder(
'created_at',
'desc'
);
}
return $this->orders;
}
如何添加过滤器以通过付款方式提取所有订单?
我尝试使用
addFieldToFilter('method','ccsave');
但这似乎不起作用。
回答:
您不能直接通过付款方式过滤订单收集。
为此,您应该使用订单集合对数据库表进行mysql连接。sales_order_payment
if (!$this->orders) {
$pMethod = 'ccsave';
$this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
'*'
)->addFieldToFilter(
'status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
);
/* join with payment table */
$this->orders->getSelect()
->join(
["sop" => "sales_order_payment"],
'main_table.entity_id = sop.parent_id',
array('method')
)
->where('sop.method = ?',$pMethod );
$this->orders->setOrder(
'created_at',
'desc'
);
}