Magento 2:覆盖Magento \ Review \ Block \ Product \ View无效

时间:2017-08-07 14:42:36

标签: php magento magento2 magento2.0.2

我试图覆盖Magento 2中的产品视图审核功能块,但我无法让它工作。

应用/代码/ [包] /CombineReviews/etc/module.xml

namespace [Package]\CombineReviews\Block\Magento\Review\Product;

class View extends \Magento\Review\Block\Product\View {
/**
 * Review collection
 *
 * @var ReviewCollection
 */
protected $_reviewsCollection;

/**
 * Review resource model
 *
 * @var \Magento\Review\Model\ResourceModel\Review\CollectionFactory
 */
protected $_reviewsColFactory;

/**
 * @param \Magento\Catalog\Block\Product\Context $context
 * @param \Magento\Framework\Url\EncoderInterface $urlEncoder
 * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
 * @param \Magento\Framework\Stdlib\StringUtils $string
 * @param \Magento\Catalog\Helper\Product $productHelper
 * @param \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig
 * @param \Magento\Framework\Locale\FormatInterface $localeFormat
 * @param \Magento\Customer\Model\Session $customerSession
 * @param ProductRepositoryInterface $productRepository
 * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
 * @param \Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory
 * @param array $data
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
 */
public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Url\EncoderInterface $urlEncoder,
    \Magento\Framework\Json\EncoderInterface $jsonEncoder,
    \Magento\Framework\Stdlib\StringUtils $string,
    \Magento\Catalog\Helper\Product $productHelper,
    \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
    \Magento\Framework\Locale\FormatInterface $localeFormat,
    \Magento\Customer\Model\Session $customerSession,
    ProductRepositoryInterface $productRepository,
    \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
    \Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory,
    array $data = []
) {
    $this->_reviewsColFactory = $collectionFactory;
    parent::__construct(
        $context,
        $urlEncoder,
        $jsonEncoder,
        $string,
        $productHelper,
        $productTypeConfig,
        $localeFormat,
        $customerSession,
        $productRepository,
        $priceCurrency,
        $data
    );
}

/**
 * Render block HTML
 *
 * @return string
 */
protected function _toHtml()
{
    $this->getProduct()->setShortDescription(null);

    return parent::_toHtml();
}

/**
 * Replace review summary html with more detailed review summary
 * Reviews collection count will be jerked here
 *
 * @param \Magento\Catalog\Model\Product $product
 * @param bool $templateType
 * @param bool $displayIfNoReviews
 * @return string
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
public function getReviewsSummaryHtml(
    \Magento\Catalog\Model\Product $product,
    $templateType = false,
    $displayIfNoReviews = false
) {
    return $this->getLayout()->createBlock(
        'Magento\Review\Block\Rating\Entity\Detailed'
    )->setEntityId(
        $this->getProduct()->getId()
    )->toHtml() . $this->getLayout()->getBlock(
        'product_review_list.count'
    )->assign(
        'count',
        $this->getReviewsCollection()->getSize()
    )->toHtml();
}

/**
 * Get collection of reviews
 *
 * @return ReviewCollection
 */
public function getReviewsCollection()
{
    if (null === $this->_reviewsCollection) {
        $this->_reviewsCollection = $this->_reviewsColFactory->create()->addStoreFilter(
            $this->_storeManager->getStore()->getId()
        )->addStatusFilter(
            \Magento\Review\Model\Review::STATUS_APPROVED
        )->addEntityFilter(
            'product',
            3
            //$this->getProduct()->getId()
        )->setDateOrder();
    }
    return $this->_reviewsCollection;
}

/**
 * Force product view page behave like without options
 *
 * @return bool
 */
public function hasOptions()
{
    return false;
}
}

应用/代码/ [包] /CombineReviews/etc/di.xml

)->addEntityFilter(
    'product',
    3
    //$this->getProduct()->getId()

应用/代码/ [包] /CombineReviews/Block/Magento/Review/Product/View.php

select cust_id,date,
case 
when date = (select min(date) as min_date from customer where to_char(cust.date,'yyyy') = to_char(date,'yyyy')) 
then 'Y' else 'N' end as flag
from customer cust

在函数ReviewCollection中,我做了一个小修改:

Map.Entry

此修改适用于核心文件,但不适用于我的自定义模块。我也试图回归'测试'从这个功能,但我不能让它工作。

有人看到了这个问题吗?

1 个答案:

答案 0 :(得分:1)

您应该使用以下代码修改app/code/[Package]/CombineReviews/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="[Package]_CombineReviews" setup_version="1.0.0" schema_version="1.0.0">
        <sequence>
            <module name="Magento_Review" />
        </sequence>
    </module>
</config>

将文件路径从app / code / [Package] /CombineReviews/Block/Magento/Review/Product/View.php更改为app / code / [Package] /CombineReviews/Block/Product/View.php。

运行setup: upgradestatic-content: deploy命令并检查。希望这应该有效。