Product details in magento

2019-09-06 21:01发布

问题:

I have this code in my magento app

<?php
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId(); 
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);  

foreach ($order_details->getAllItems() as $item) {  

//here i know we can get item name as $item->getName(), sku as $item->getSku()
//but how do we get the following details
//1.category name,2.store,3.tax,city,country,state   

<?php }  ?>

I know by simply print_r($order_details->getAllItems()) will get the array list.but im in no situation to do this

回答1:

You need to load the full product model to access that data.

The collection you are loading is not of products, it's a collection of Order Items, which has minimal data associated with it. You need to load the full product model, by getting the product if from the Order Item.

try this:

<?php
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId(); 
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);  
?>
<?php foreach ($order_details->getAllItems() as $item): ?> 
    <?php $product = Mage::getModel('catalog/product')->load($item->getProductId()) ?>
    <?php echo $product->getName() ?>
    <?php // now you have the full product model/data loaded ?>
<?php endforeach  ?>


回答2:

This items are not as same as products, they don't have all the properties. You can either do a Mage::getModel('catalog/product')->load($item->getProductId()); fast way to test something, but this adds an extra query to mysql for every item in the loop. Or I recommend to edit the sales xml -> Mage > Sales > etc > config.xml (in a local module). You should add to the nodes sales_convert_quote_item or sales_convert_order_item the attributes you want to be copied from product to item. In your case it would be the sales_convert_order_item node since you are dealing with an order.



标签: php magento