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();
  • can you share the code that you are using? – Shoaib Munir Apr 18 at 5:31
  • 1
    Updated my question – Shiwani Apr 18 at 5:41
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();
  • $productImage = $imageBlock->getImage($product, $imageType); $product is productid right? – Shiwani Apr 18 at 6:18
  • no, the object of product that you have loaded – Shoaib Munir Apr 18 at 6:20
  • Need to get on the behalf of productids – Shiwani Apr 18 at 6:22
  • Updated my answer – Shoaib Munir Apr 18 at 6:26
  • 1
    Thanks for the reply. This is working in observer. But it's not working in crontab. – Shiwani Apr 18 at 8:09
1

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();

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.