2

I am getting placeholder images in crontab.

Like this:
/pub/static/version1554967977/crontab/_view/en_US/Magento_Catalog/images/product/placeholder/.jpg;
This is wrong image.

How to get product image in cron tab?

$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(500, 500)->getUrl();
2
  • can you share the code that you are using? Apr 18, 2019 at 5:31
  • 1
    Updated my question
    – Shomita
    Apr 18, 2019 at 5:41

2 Answers 2

3

This is my working code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();   
$product  = $objectManager->get('Magento\Catalog\Model\Product')->load($item->getProductId());
                            $store    = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
                            $imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
1

By using block factory you can create List block object, and by List block object you can get your image. Try code below

public function __construct(
    ...
    \Magento\Catalog\Model\Product $productModel,
    \Magento\Framework\View\Element\BlockFactory $blockFactory)
{
    $this->productModel = $productModel
    $this->_blockFactory = $blockFactory;
}

In your function, use this:

$imageBlock =  $this->_blockFactory->createBlock('Magento\Catalog\Block\Product\ListProduct');
$product = $this->productModel->load($item->getProductId();
$imageType = 'category_page_grid';
$productImage = $imageBlock->getImage($product, $imageType);
$imageUrl = $productImage->getImageUrl();
6
  • $productImage = $imageBlock->getImage($product, $imageType); $product is productid right?
    – Shomita
    Apr 18, 2019 at 6:18
  • no, the object of product that you have loaded Apr 18, 2019 at 6:20
  • Need to get on the behalf of productids
    – Shomita
    Apr 18, 2019 at 6:22
  • Updated my answer Apr 18, 2019 at 6:26
  • 1
    Thanks for the reply. This is working in observer. But it's not working in crontab.
    – Shomita
    Apr 18, 2019 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.