2

I have the following custom model:

class CompanyName_Orders_Model_FetchOrderInfo extends Mage_Core_Model_Abstract {
private function createDate($lookBeforeDateInDays = null){
    if(($lookBeforeDateInDays===null) || (!is_int($lookBeforeDateInDays))){ 
        // 'No date or invalid date supplied.
        return $lookBeforeDateInDays=date(now());
    }else{
        $varToTime = strtotime("-".$lookBeforeDateInDays." day");
        return $lookBeforeDateInDays=date("Y-m-d 00:00:00",$varToTime);
    }

}
public function unpaid($lookBackInDays = null) {
    echo $lookBeforeDateInDays=$this->createDate($lookBackInDays);
    echo '<br>';
    /* 
     * Get Orders where not cancelled or complete and have not captured any funds.
     */
    $missingOrders = Mage::getModel('sales/order')->getCollection()
            ->addFieldToFilter('status', array('nin' => array('canceled','complete')))
            ->addAttributeToFilter('base_total_paid', array('null'=>true))
            ->addAttributeToFilter('created_at',array('to'=>$lookBeforeDateInDays));
    echo $missingOrders->count().'<br>';
    echo $missingOrders->getSelect(); 
    return true;
}
}

Please excuse the bonus "echo's" in the model - they're for debugging.

I am attempting to replicated the same results as you'd see in Sales > Orders when Filter = Processing and Purchased On (To) = [date]. The createDate function take a numerical input, converts X days ago using strtotime or sets the date to now if no input is provided.

My Problem

The date is outputting correctly, but where $missingOrders->count() should be, lets say, 53 - it may be 35.

I've verified that it isn't because of the "status" filter so that leads me to believe that my server time differs from Magento's time. How can I sync the two so that the results line up? And, since this is in a model, shouldn't the timezone match up anyway?

2

I found the solution.

When querying dates that need to align with the Magento backend we need to pass the dates through:

$date=Mage::getSingleton('core/date');

I here is my updated solution for anyone who may find it beneficial. If you notice line containing:

$varToTime = strtotime($lookBeforeDateInDays." day ago 11:59:59PM");

You'll see that the date has been pushed to midnight. This is because in the order grid the "To" filter on the date looks through the date supplied (since we can't provide hours, minutes or seconds), so, I have forced my date to right at midnight so that it queries through the entire day.

Solution

private function createDate($lookBeforeDateInDays = null){
    $date=Mage::getSingleton('core/date');
    $formatStr = 'Y-m-d H:i:s';
    if(($lookBeforeDateInDays===null) || (!is_int($lookBeforeDateInDays))){ 
        // 'No date or invalid date supplied.
        return $lookBeforeDateInDays=$date->gmtDate($formatStr,'now');
    }else{
        if($lookBeforeDateInDays===0){
            return $lookBeforeDateInDays=$date->gmtDate($formatStr,'now');
        }
        $varToTime = strtotime($lookBeforeDateInDays." day ago 11:59:59PM");
        return $lookBeforeDateInDays=$date->gmtDate($formatStr,$varToTime);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.