简体   繁体   中英

How to Filter the magento abandoned cart report collection

I am using this code to get all the abandoned carts.

$storeIds = array(1);
    $collection = Mage::getResourceModel('reports/quote_collection');
    $collection->prepareForAbandonedReport($storeIds);
    $collection->load();

What I want is that to get the carts not older than some specific date, I have also tried code below to achieve it, but its not working.

$collection->addFieldToFilter(array("main_table.created_at"=>Mage::getModel('core/date')->date('Y-m-d H:i:s', strtotime('-1 week'))));

Another thing how can I addAttributeToselect() to get the required data, not all.If someone can also provide the answers with some other filter examples it will be very grateful.Thanks in advance

So after alot of searching I finally found the way to do it. The function prepareForAbandonedReport() is doing all the filteration steps, so I open the file containing this function, copied its code and used it in my code and altered it according to my needs. Here is the code I used,I hope this will help someone.

        $collection = Mage::getResourceModel('reports/quote_collection')
        ->addFieldToFilter('items_count', array('neq' => '0'))
        ->addFieldToFilter('main_table.is_active', '1')
        ->addFieldToFilter('main_table.created_at', array('gt' => date("Y-m-d H:i:s", strtotime('-2 month'))))
        ->addSubtotal($storeIds, null)
        ->addCustomerData(null)
        ->setOrder('updated_at')
        ->addFieldToFilter('store_id', array('in' => $storeIds));
    $collection->load();

Please use following code this may help -

$collection = Mage::getResourceModel('reports/quote_collection');
$collection->prepareForAbandonedReport();
$output = $collection->load()->toArray();

Since this answer help me a lot, i'll share my own solution (tested in Magento EE 1.13.1.0):

  1. Override Mage_Reports_Model_Resource_Quote_Collection
  2. Create _filterDate() method:

     public function _filterDate($key, $filter) { if (array_key_exists($key, $filter)) { $filterDate = $filter[$key]; if (array_key_exists('from', $filterDate)) { $fromRawDate = $filter[$key]['from']; $dateFrom = str_replace('/', '-', $fromRawDate); $dateStart = date('Ym-d', strtotime($dateFrom)) . ' 00:00:00'; } if (array_key_exists('to', $filterDate)) { $toRawDate = $filter[$key]['to']; $dateTo = str_replace('/', '-', $toRawDate); $dateEnd = date('Ym-d', strtotime($dateTo)) . ' 23:59:59'; } if ( ! empty($dateStart) && ! empty($dateEnd) ) { $this->addFieldToFilter('`main_table`.`' . $key . '`', array('from' => $dateStart, 'to' => $dateEnd)); } else if ( ! empty($dateStart) && empty($dateEnd) ) { $this->addFieldToFilter('`main_table`.`' . $key . '`', array('from' => $dateStart)); } else if ( ! empty($dateEnd) && empty($dateStart) ) { $this->addFieldToFilter('`main_table`.`' . $key . '`', array('to' => $dateEnd)); } } }
  3. Edit prepareForAbandonedReport() and use _filterDate() :

     public function prepareForAbandonedReport($storeIds, $filter = null) { self::_filterDate('created_at', $filter); // Used to filter created_at field self::_filterDate('updated_at', $filter); // Used to filter updated_at field $this->addFieldToFilter('items_count', array('neq' => '0')) ->addFieldToFilter('main_table.is_active', '1') ->addSubtotal($storeIds, $filter) ->addCustomerData($filter) ->setOrder('updated_at'); if (is_array($storeIds) && !empty($storeIds)) { $this->addFieldToFilter('store_id', array('in' => $storeIds)); } return $this; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM