Get product information of the latest orders in Ma

2020-08-01 07:14发布

问题:

By default, Magento got Order information such as shipping, address, but not much about product. I want to get the latest order information included product url, product thumbnail, etc. I try to join the order flat tables to get information. But I found there's only product name in the sales_flat_order_item table. So how can i get the product url and thumbnail?

I wrote a function in block to get some information about an order, newbie to magento, would you like to tell me if this is a good method to get data from magento, if not, so appriciate to tell your method.

    public function getOrderCollection() {
    $orders = Mage::getModel('sales/order')->getCollection();
    $orders->getSelect()
        ->join(
            array('addr' => Mage::getSingleton('core/resource')->getTableName('sales/order_address')),
            'main_table.entity_id = addr.parent_id',
            array('addr.*')
        )       
        ->join(
            array('itemz' => Mage::getSingleton('core/resource')->getTableName('sales/order_item')),
            'main_table.entity_id = itemz.order_id', 
            array('itemz.*')
        )
        ->join(
            array('product' => Mage::getSingleton('core/resource')->getTableName('catalog/product')),
            'itemz.product_id = product.entity_id',
            array('product.*')
        )
        ;
    $orders
        ->addAttributeToFilter('main_table.status', 'Processing')
        ->addAttributeToFilter('addr.address_type', 'shipping')
        ->addAttributeToSort('main_table.entity_id', 'asc')
        ;


    return $orders;
}

回答1:

What you probably want to be doing is using $order->getAllItems() or $order->getAllVisibleItems(). The difference between the two is that getAllItems() returns all items, including configurable/grouped products and their children, whereas getAllVisibleItems() returns parent products only.

From there you can foreach the collection and use $item->getProduct(), or more likely you'll need to load the product like this:

Mage::getModel('catalog/product')->load($item->getProductId());

From what I can tell you're looking for code like this:

$items = $order->getAllItems();
foreach ($items as $item) {
  $product = Mage::getModel('catalog/product')->load($item->getProductId());
  /* Use product methods here to get required data */
}


标签: magento