Reputation: 577
I have a external script i use to fetch the current quote products (as external cart for example) This works perfect.
In this code i use the following:
$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote();
$helper = $obj->get('\Magento\Checkout\Helper\Cart');
$quote = $helper->getQuote();
$quoteitems = $quote->getAllItems();
$cart= $helper->getCart();
foreach ($quoteitems as $item)
{
$_imagehelper = $obj->get('\Magento\Catalog\Helper\Image');
$product = $item->getProduct();
$img = $_imagehelper->init($product,'category_page_list',array('height' => '100' , 'width'=> '100'))->getUrl();
}
This results in: /pub/static/frontend/_view/nl_NL/Magento_Catalog/images/product/placeholder/.jpg
While in a layout block i use the following and that works perfect:
$helper = $this->helper('\Magento\Checkout\Helper\Cart');
$quote = $helper->getQuote();
$quoteitems = $quote->getAllItems();
$cart= $helper->getCart();
foreach ($quoteitems as $item)
{
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
$product = $item->getProduct();
$img = $_imagehelper->init($product,'category_page_list',array('height' => '100' , 'width'=> '100'))->getUrl();
}
Im out of ideas.
Upvotes: 2
Views: 5868
Reputation: 475
Use below code to get product image externally
use \Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
$mediaurl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$pid=1;
$_product = $objectManager->create('\Magento\Catalog\Model\Product')->load($pid);
$data=$_product->getData();
echo $imageurl=$_product->getImage();
//print_r($data);
?>
<img src="<?php echo $mediaurl.'catalog/product'.$imageurl;?>">
Upvotes: 0
Reputation: 3591
use the following code
// your product's id here
$pid = 7;
// set image width and height
$imagewidth = 500;
$imageheight = 500;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($pid);
$imageHelper = $objectManager->get('\Magento\Catalog\Helper\Image');
$image_url = $imageHelper->init($_product, 'product_page_image_small')->setImageFile($_product->getFile())->resize($imagewidth, $imageheight)->getUrl();
Upvotes: 4