0

I have the following code, if there are no orders older then $days_ago, I want that the foreach loop won't be executed. I don't know how to do this.

$orders = Mage::getModel('sales/order')->getCollection()
     ->addAttributeToSelect('*')
     ->addAttributeToFilter('created_at', array('lt' => $days_ago));

     foreach ($orders as $order):
        if(!$this->_shouldBeAnonymised($order)):
            continue;
     endif;

        $this->_anonymiseSale($order);
     endforeach;

Hope someone can help me!

1

except getSize() you can use count as well,

like below

$orders = Mage::getModel('sales/order')->getCollection()
     ->addAttributeToSelect('*')
     ->addAttributeToFilter('created_at', array('lt' => $days_ago));

if($orders->count() > 0):
    foreach ($orders as $order):
        if(!$this->_shouldBeAnonymised($order)):
            continue;
        endif;

        $this->_anonymiseSale($order);
    endforeach;
endif;
0

Try this:

$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('created_at', array('lt' => $days_ago));
    if ($orders->getSize()) {
        foreach ($orders as $order):
            if(!$this->_shouldBeAnonymised($order)):
                continue;
            endif;

            $this->_anonymiseSale($order);
        endforeach;
    }

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.