Magento2从外部获取产品图片

时间:2016-05-12 06:30:35

标签: quotes cart magento2

我有一个用于获取当前报价产品的外部脚本(例如外部购物车)这很完美。

在此代码中,我使用以下内容:

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

导致:/pub/static/frontend/_view/nl_NL/Magento_Catalog/images/product/placeholder/.jpg

在布局块中,我使用以下内容并且完美无缺:

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

我没有想法。

2 个答案:

答案 0 :(得分:4)

使用以下代码

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

答案 1 :(得分:0)

使用以下代码从外部获取产品图片

    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;?>">