1

How to get child product image thumbnail in sales order view in Back-end ?

my Magento is 2.1.6

This code working but i need to get order current image not configurable image all the time if i created order from the backend and select the configurable product and choosing another color instead of default.

 $_item = $block->getItem();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getProduct()->getId());
$thitem = $_item->getProduct()->getThumbnail();

Also How to get image url by sku if possible?

1

instead of use $_item->getProduct()->getId()

You should use sku $_item->getSku();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Catalog\Model\ProductFactory'->create());
$product = $this->productFactory->create();
$product->load($product->getIdBySku($_item->getSku()));
$thitem = $product->getThumbnail();

$fullImageUrl =$block->getUrl('pub/media/catalog').'product'.$product->getThumbnail()

Suggestion:

Use of direct product object is not good idea

Also suggest to inject \Magento\Catalog\Model\ProductFactory to your block class

  • Tried using $product = $this->productFactory->create(); $product->load($product->getIdBySku($sku)); $product->getThumbnailUrl() to display product thumbnail image, but its not working. getThumbnail does not give full image path to display – Slimshadddyyy Jan 31 '18 at 7:25
1

My solution without using ObjectManager.

When you have configurable product in order, then actually 2 items are added (configurable and simple). For configurable products we have to iterate over all (not only visible) items and replace our product when items parent id is equal to current item id.


use Magento\ConfigurableProduct\Model\Product\Type\Configurable;

/** @var  $_item \Magento\Sales\Model\Order\Item */
$_item = $block->getItem();
$productForThumbnail = $_item->getProduct();

if ($_item->getProductType() === Configurable::TYPE_CODE){
    foreach ($block->getOrder()->getAllItems() as $orderItem){
        if ($orderItem->getParentItemId() == $_item->getId()){
            $productForThumbnail = $orderItem->getProduct();
            break;
        }
    }
}

Then we can easily add image:

$imageUrl = $this->helper('Magento\Catalog\Helper\Image')->init($productForThumbnail,'cart_page_product_thumbnail')->getUrl();
0

If image not present then use placeholder.

$images= $imgurl.$product->getImage();

$ch = curl_init($images);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($retcode==200) {
    $product_images = $images;
}
else
{
    $product_images ="STORE_URL/pub/static/frontend/TemplateMonster/Spiceli/en_US/Magento_Catalog/images/product/placeholder/image.jpg";
}

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.