How to define where condition in resource model function for specific value? magento2
-
07-01-2021 - |
Question
How to define condition in resource model function , i have a function for sales reporting , here it is fetching records from defined columns as given below , i want to add condition here for status column to fetch only those status which is equals to **'closed' in my db row** , is that possible to define here ?
public function applyBaseFilters($collection)
{
$collection->getSelect()
->reset(\Zend_Db_Select::COLUMNS)
->columns([
'period' =>'DAYNAME(created_at)',
'total_orders' => 'COUNT(entity_id)',
'total_items' => 'SUM(total_item_count)',
'subtotal' => 'SUM(base_subtotal)',
'tax' => 'SUM(base_tax_amount)',
'status' => 'status',
'shipping' => 'SUM(base_shipping_amount)',
'discounts' => 'SUM(base_discount_amount)',
'total' => 'SUM(base_grand_total)',
'invoiced' => 'SUM(base_total_invoiced)',
'refunded' => 'SUM(base_total_refunded)',
'entity_id' => 'CONCAT(entity_id,\''.$this->createUniqueEntity().'\')'
])
;
}
File location is (\Magento\Sales\Model\ResourceModel\Order\Collection)
Solution
Try adding ->where("'status' = 'closed'")
as follows
$collection->getSelect()
->reset(\Zend_Db_Select::COLUMNS)
->where("'status' = 'closed'")
->columns([
'period' =>'DAYNAME(created_at)',
'total_orders' => 'COUNT(entity_id)',
'total_items' => 'SUM(total_item_count)',
'subtotal' => 'SUM(base_subtotal)',
'tax' => 'SUM(base_tax_amount)',
'status' => 'status',
'shipping' => 'SUM(base_shipping_amount)',
'discounts' => 'SUM(base_discount_amount)',
'total' => 'SUM(base_grand_total)',
'invoiced' => 'SUM(base_total_invoiced)',
'refunded' => 'SUM(base_total_refunded)',
'entity_id' => 'CONCAT(entity_id,\''.$this->createUniqueEntity().'\')'
])
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange