Iterating through a model in magento

2019-08-18 01:49发布

问题:

I am having trouble getting the following function to work:

Mage::getModel('sales/order');

I attempted this, however it returned no results:

$res=Mage::getModel('sales/order');
$orderId=$res->getOrderId();

echo '<pre>';
print_r($orderId);

回答1:

I guess by res you mean resource so I'll make a leap of logic and assume you mean to query a resource collection.

echo '<pre>';
$resource = Mage::getModel('sales/order')->getCollection();
foreach ($resource as $order) {
    echo $order->getId(), "\n";
}
echo '</pre>';


回答2:

$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{
 $name[] = $item->getName();
 $unitPrice[]=$item->getPrice();
 $sku[]=$item->getSku();
 $ids[]=$item->getProductId();
 $qty[]=$item->getQtyToInvoice();
}


标签: magento