Answers:
use \Magento\Store\Model\StoreManager $storeManager
$this->storeManager = $storeManager;
$path =
catalog/placeholder/thumbnail_placeholder OR
catalog/placeholder/swatch_image_placeholder OR
catalog/placeholder/small_image_placeholder OR
catalog/placeholder/image_placeholder OR
public function getConfig($config_path)
{
return $this->storeManager->getStore()->getConfig($config_path);
}
<img src = $mediaUrl.'catalog/product/placeholder/'.$this->getConfig($path) />
$mediaUrl = $this ->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA );
在Magento 2.2上:
获取帮助块(phtml)
$imageHelper = $this->helper(\Magento\Catalog\Helper\Image::class);
获取全球帮助者
$imageHelper = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Helper\Image::class);
获取占位符图像网址。用户作为参数:“图像”,“ smal_image”,“ swatch_image”或“缩略图”。
$placeholderImageUrl = $imageHelper->getDefaultPlaceholderUrl('image');
如果您检查商店设置,则会发现“产品图片”占位符的选项
Stores > Configuration > Catalog > Catalog > Product Image Placeholders
您可以在块中调用获取值,然后在模板文件中调用。
<?php
protected $_scopeConfig;
public function __construct
(
---
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
---
){
---
$this->_scopeConfig = $scopeConfig;
---
}
public function getPlaceholderImage(){
return $this->_scopeConfig->getValue('catalog/placeholder/image_placeholder'); // Base Image
$this->_scopeConfig->getValue('catalog/placeholder/small_image_placeholder'); // Small Image
$this->_scopeConfig->getValue('catalog/placeholder/swatch_image_placeholder'); // Swatch Image
$this->_scopeConfig->getValue('catalog/placeholder/thumbnail_placeholder'); // Thumbnail Image
}
在您的模板文件调用中
$block->getPlaceholderImage();
我们应该看一下Magento Helper类 \Magento\Catalog\Helper\Image
例如 :
<?php
namespace Vendor\Module\Helper;
use Magento\Catalog\Helper\ImageFactory as HelperFactory;
use Magento\Framework\View\Asset\Repository;
class Image
{
/**
* @var HelperFactory
*/
protected $helperFactory;
/**
* @var Repository
*/
protected $assetRepos;
/**
* Image constructor.
* @param HelperFactory $helperFactory
* @param Repository $repository
*/
public function __construct(
HelperFactory $helperFactory,
Repository $repository
) {
$this->assetRepos = $repository;
$this->helperFactory = $helperFactory;
}
/**
* Get image url
*
* @param $product
* @param $imageId
* @return mixed
*/
public function getImageUrl($product, $imageId = null)
{
/** @var \Magento\Catalog\Helper\Image $helper */
if ($imageId == null) {
$imageId = 'cart_page_product_thumbnail';
}
$helper = $this->helperFactory->create()
->init($product, $imageId);
return $helper->getUrl();
}
/**
* Get small place holder image
*
* @return string
*/
public function getPlaceHolderImage()
{
/** @var \Magento\Catalog\Helper\Image $helper */
$helper = $this->helperFactory->create();
return $this->assetRepos->getUrl($helper->getPlaceholder('small_image'));
}
}
如果您要在任何模板文件中获取它。您将需要Magento的图像助手。\Magento\Catalog\Helper\Image::class
您可以在任何.phtml文件的顶部获得这样的实例
$imageHelper = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Helper\Image::class);
并像这样获取占位符图像URL的路径
$imageHelper->getDefaultPlaceholderUrl('small_image')
$imageHelper->getDefaultPlaceholderUrl('image')