-1

I noticed that I can access the value of the variable $order outside of its scope.

public function dryRunAction()
{     
    $allCustomersBefore = Mage::getModel('customer/customer')->getCollection()
                                                       ->addAttributeToSelect('*')
                                                       ->addFieldToFilter('customer_activated', '1')
                                                       ->addFieldToFilter('group_id', array('6'));
    

    foreach($allCustomersBefore as $customer) {

        $orders = Mage::getResourceModel('sales/order_collection')
            ->addAttributeToSelect('*')
            ->addFieldToFilter('customer_id', $customer->getId());

        $atLeastOnePendingOrder = false;
        foreach($orders as $order) {
                    
            if ($order->getStatus() == 'pending') {
                $atLeastOnePendingOrder = true;
                break;
            }
        }


        if ($atLeastOnePendingOrder) {
            echo $customer->getName() . " already made an order (Order Status: ". $order->getStatus() . ").<br>";        
        }
    }
}

The output is Mr Sample Customer already made an order (Order Status: pending).

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Black
  • 18,150
  • 39
  • 158
  • 271
  • 1
    because the code is synchronous it takes the last iteration of the definition inside the loop ... – treyBake Apr 03 '19 at 09:07

1 Answers1

3

The foreach loop is a block level, not a function level.

Variables declared in function are not available outside.

But, variables outside block are always available.

Their values should be the latest value from iteration.

Reference:

Pupil
  • 23,834
  • 6
  • 44
  • 66