Magento 2: Скрыть расширенные фрагменты обзора по умолчанию
В Magento 2 я хочу скрыть расширенные фрагменты по умолчанию. Я уже понял, как удалить фрагменты с ценами и продуктами, прочитав этот блог: http://blog.mageworx.com/2016/09/extending-rich-snippets-functionality-in-magento-2/
Но если у вас включены отзывы в Magento, он автоматически создаст расширенные фрагменты для рейтинга и отзывов. Кто-нибудь знает, как удалить структурированные данные рейтинга и отзывов?
1 answers
Я нашел решение, чтобы скрыть расширенные фрагменты обзора. В файле Vendor/Module/view/frontend/layout/catalog_product_view.xml, Я добавил следующий код:
<!--
Replace the default review template file with a custom review template file
-->
<referenceContainer name="content">
<referenceBlock name="product.info.product_additional_data">
<action method="setTemplate" ifconfig="sytem_config/product/enable_product_structured_data">
<argument name="template" xsi:type="string">Vendor_Module::product/view/list.phtml</argument>
</action>
</referenceBlock>
</referenceContainer>
Таким образом, на основе конфигурации "enable_product_structured_data", которую я добавил в system.xml , файл list.phtml будет перезаписан поставщиком файла/Модулем/представлением/интерфейсом/шаблоном/продуктом/представлением/списком.phtml. Этот файл совпадает с собственным файлом list.phtml Magento (расположен в модуле-обзор), но без всех атрибуты расширенных фрагментов (itemscope, itemprop и itemtype).
Чтобы переопределить фрагменты с высоким рейтингом, мне пришлось создать перезапись. Мой di.xml содержит следующий код:
<preference for="Magento\Review\Block\Product\ReviewRenderer"
type="Vendor\Module\Rewrite\Magento\Review\Block\Product\ReviewRenderer"/>
Мой файл перезаписи (Vendor/Module/Rewrite/Magento/Review/Block/Product/ReviewRenderer.php ) содержит следующий код:
<?php
namespace Vendor\Module\Rewrite\Magento\Review\Block\Product;
use Vendor\Module\Helper\Config;
/**
* Class ReviewRenderer
* @package Vendor\Module\Rewrite\Magento\Review\Block\Product
*/
class ReviewRenderer extends \Magento\Review\Block\Product\ReviewRenderer
{
/**
* @var \Vendor\Module\Helper\Config
*/
protected $configHelper;
/**
* ReviewRenderer constructor.
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Review\Model\ReviewFactory $reviewFactory
* @param \Vendor\Module\Helper\Config $configHelper
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Review\Model\ReviewFactory $reviewFactory,
\Vendor\Module\Helper\Config $configHelper,
array $data = []
)
{
$this->configHelper = $configHelper;
parent::__construct($context, $reviewFactory, $data);
}
/**
* @param \Magento\Catalog\Model\Product $product
* @param string $templateType
* @param bool $displayIfNoReviews
* @return string
*/
public function getReviewsSummaryHtml(
\Magento\Catalog\Model\Product $product,
$templateType = \Magento\Review\Block\Product\ReviewRenderer::DEFAULT_VIEW,
$displayIfNoReviews = false
)
{
if (!$product->getRatingSummary() && !$displayIfNoReviews) {
return '';
}
// pick template among available
if (empty($this->_availableTemplates[$templateType])) {
$templateType = self::DEFAULT_VIEW;
}
if ($this->configHelper->get(Config::ENABLE_PRODUCT_STRUCTURED_DATA) &&
($templateType == self::FULL_VIEW || $templateType == self::DEFAULT_VIEW)) {
$this->setTemplate('Vendor_Module::helper/summary_custom.phtml');
} else {
$this->setTemplate($this->_availableTemplates[$templateType]);
}
$this->setDisplayIfEmpty($displayIfNoReviews);
if (!$product->getRatingSummary()) {
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
}
$this->setProduct($product);
return $this->toHtml();
}
}
Затем я создал файл Поставщик/Модуль/представление/интерфейс/шаблоны/помощник/summary_custom.phtml. Это также совпадает с собственным summary.pthml от Magento (также находится в модуль-обзор), но опять же без атрибутов расширенных фрагментов.
Таким образом, я смог удалить расширенные фрагменты Magento по умолчанию и добавить свои собственные структурированные данные.