1

This was asked at least three times between Magent.StackExchange and StackOverFlow, but the provided solutions are just not working for me. What im trying to do is get the image of each of the items in the orders, I tried using 'image_url' => Mage::helper('catalog/image')->init($item->getProduct(), 'thumbnail') as explained in the other answers but im always getting an empty object. I made sure the product have a thumbnail assigned from the magento admin. Everything else is working just fine for the item details, just can't get the image or thumbnail url.

protected function _retrieveCollection() {

    // get the customer
    $login_hash = $this->getRequest()->getParam('login_hash');
    $customer = Mage::getModel('customer/customer')
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('login_hash',$login_hash)
                ->load()
                ->getFirstItem();       


    $orders = Mage::getResourceModel('sales/order_collection')
        ->addFieldToSelect('*')
        ->addFieldToFilter('customer_id', $customer->getId() )
        ->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
        ->setOrder('created_at', 'desc');


    $collection = new Varien_Data_Collection(); 


    foreach ($orders as $order) {   

        $items = $order->getAllVisibleItems();
        $items_arr = [];

        foreach ($items as $item) {
            //Mage::Log(print_r($item, true));
            $items_arr[] = array(
                    'id' =>  $item->getItemID(),
                    'name' => $item->getName(),
                    'desc' => $item->getDescription(),
                    'price' => $item->getPrice(),
                    'quantity' => $item->getQtyOrdered(),
                    'original_price' => $item->getOriginalPrice(),
                    'product_type' => $item->getProductType(),
                    'image_url' =>  Mage::helper('catalog/image')->init($item->getProduct(), 'thumbnail')
                );


        }

        $order_arr = array(
                'id' => $order->getRealOrderId(),
                'date_created' => $order->getCreatedAt(),
                'total' => $order->getGrandTotal(),
                'tax_amount' => $order->getTaxAmount(),
                'shipping_amount' => $order->getShippingAmount(),
                'subtotal' => $order->getSubtotal(),
                'shipping_address_id' => $order->getShippingAddressID(),
                'billing_address_id' => $order->getBillingAddressID(),
                'total_item_count' => $order->getTotalItemCount(),
                'items' => $items_arr
            );

        $varienObject = new Varien_Object();
        $varienObject->setData($order_arr);
        $collection->addItem($varienObject);            

    }


    return $collection;

}
1

Magento 2 get Product Image By ID in Order, please follow the code after:

 - $_item = $block->getItem();

 - $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

 - $_product = $objectManager->get('Magento\Catalog\Model\Product')->load($_item->getProductId());

 - $imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');

 - $image_url = $imageHelper->init($_product, 'product_page_image_small')->setImageFile($_product->getFile())->resize(100, 100)->getUrl();

Please view this link. Hope to help you !

1

try this

foreach ($items as $item) {
    $_product=$item->getProduct();
    //Mage::Log(print_r($item, true));
    $items_arr[] = array(
        'id' =>  $item->getItemID(),
        'name' => $item->getName(),
        'desc' => $item->getDescription(),
        'price' => $item->getPrice(),
        'quantity' => $item->getQtyOrdered(),
        'original_price' => $item->getOriginalPrice(),
        'product_type' => $item->getProductType(),
        'image_url' =>  Mage::helper('catalog/image')->init($_product, 'thumbnail')
    );
}
  • actually i know now where the problem is, your solution and min both works, the problem is that the magento api filters the url from the returned json for some reason, im checking this out now – Yehia A.Salam Jan 30 '16 at 19:27
  • What about the load overhead when you load the product model each time? – Rinto George Jun 19 '18 at 8:19
  • @RintoGeorge you can also use $item->getProduct() – Qaisar Satti Jun 19 '18 at 9:03
  • @QaisarSatti I have tried but it's giving empty string – Rinto George Jun 19 '18 at 10:48

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.