覆盖.phtml文件的问题:Magento 2目录模块


8

问题:尝试覆盖默认的Magento phtml文件,即时消息出错

我需要获取这些信息:( 在页脚,分页器中分页)

在此处输入图片说明

我的自定义模块:

1) Vendor/TglsSearch  --> Override from Magento CatalogSearch
2) Vendor/Tglcatalog ---> from Magento Catalo

应用程序\设计\前端\供应商\标签中

在此处输入图片说明

更新的代码:

删除以下文件夹

Magento_Catalog文件夹中

templates\product\list\toolbar.phtml
templates\product\list\toolbar\amount.phtml
templates\product\list\toolbar\limiter.phtml
templates\product\list\toolbar\sorter.phtml
templates\product\list\toolbar\viewmode.phtml

这是我在首页上没有结果的空白页:

在此处输入图片说明

在块文件中添加了上述代码:

供应商\模块\块\产品\ ListProduct.php

class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
{
    public function __construct(
    /*passing all Constructors parameters to the parent class */
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Magento\Catalog\Model\Layer\Resolver $layerResolver,
     CategoryRepositoryInterface $categoryRepository,
    \Magento\Framework\Url\Helper\Data $urlHelper,

    \Vendor\Module\Helper\Data $tglssearchHelper,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $catalogResourceModelProductCollectionFactory,
    \Magento\Catalog\Model\Config $catalogConfig,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Catalog\Model\ProductFactory $productFactory,      //Pagination
    array $data = []
) {
    $this->tglssearchHelper = $tglssearchHelper;
    $this->catalogResourceModelProductCollectionFactory = $catalogResourceModelProductCollectionFactory;
    $this->catalogConfig = $catalogConfig;
    $this->storeManager = $storeManager;

    $this->_productFactory = $productFactory;  //Pagination
    $this->urlHelper = $urlHelper;

      parent::__construct(
        $context,
        $postDataHelper,
        $layerResolver,
        $categoryRepository,
        $urlHelper,
        $data
    );
    //updated code
     $collection= $this->_getProductCollection();
    $this->setCollection($collection);

}

public function _prepareLayout(){
   parent::_prepareLayout();
    if ($this->_getProductCollection()) { 

        // create pager block for collection 

        $toolbar = $this->getToolbarBlock();  

        $pager = $this->getLayout()->createBlock(
                        'Magento\Theme\Block\Html\Pager', 'list.pager'
                )->setCollection(
                $this->_getProductCollection() // assign collection to pager
        );  
        $toolbar->setChild('product_list_toolbar_pager', $pager); // set pager block in layout
        // called prepare sortable parameters
        $collection = $this->_getProductCollection();  

        // use sortable parameters
        $orders = $this->getAvailableOrders();  

        if ($orders) {
            $toolbar->setAvailableOrders($orders);
        }
        $sort = $this->getSortBy();
        if ($sort) {
            $toolbar->setDefaultOrder($sort);
        }
        $dir = $this->getDefaultDirection();
        if ($dir) {
            $toolbar->setDefaultDirection($dir);
        }
        $modes = $this->getModes();
        if ($modes) {
            $toolbar->setModes($modes);
        }
        $toolbar->setCollection($collection);

        $this->setChild('toolbar', $toolbar);
        $this->_getProductCollection()->load();
   }
    return $this;
}

protected function _getProductCollection()
{
      $tagalys = $this->tglssearchHelper->getSearchData();

        if($tagalys == false) {
            return parent::_getProductCollection();
        } else {

        $searchResult = $tagalys;

        if(empty($searchResult)) {
            return parent::_getProductCollection();
        }

        $collection = $this->_productCollection = $this->catalogResourceModelProductCollectionFactory->create()
             ->addAttributeToSelect($this->catalogConfig->getProductAttributes())
             ->setStore($this->storeManager->getStore())
             ->addFieldToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
             ->addAttributeToFilter( 'entity_id', array( 'in' => $searchResult['results'] ) );

        $orderString = array('CASE e.entity_id');
        foreach($searchResult['results'] as $i => $productId) {
            $orderString[] = 'WHEN '.$productId.' THEN '.$i;
        }
        $orderString[] = 'END';
        $orderString = implode(' ', $orderString);

    $collection->getSelect()->order(new \Zend_Db_Expr($orderString));

        return $this->_productCollection;

    }
}


public function getToolbarBlock()
{
    $blockName = $this->getToolbarBlockName();
    if ($blockName) {
        $block = $this->getLayout()->getBlock($blockName);
        if ($block) {
            return $block;
        }
    }
    $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
    return $block;
}

public function getMode()                       //Pagination
{
    return $this->getChildBlock('toolbar')->getCurrentMode();
}

public function getToolbarHtml()        //Pagination
{
    return $this->getChildHtml('toolbar');
}

protected function getPriceRender()
{
    return $this->getLayout()->getBlock('product.price.render.default');
}

protected function _getConfig()
{
    return $this->_catalogConfig;
}
}

对于phtml

\ app \ design \ frontend \ Vendor \ tag \ Magento_Catalog \ templates \ product \ list.phtml

<?php
   $_productCollection = $block->getLoadedProductCollection();
   $_helper = $this->helper('Magento\Catalog\Helper\Output');
?>
 <?php if (!$_productCollection->count()): ?>
<div class="message info empty"><div><?php /* @escapeNotVerified */ echo __('We can\'t find products matching the selection.') ?></div></div>
 <?php else: ?>
   <?php echo $block->getToolbarHtml() ?>        //Have called here
     ....
     ....
    <?php echo($iterator == count($_productCollection)+1) ? '</li>' : '' ?>
        <?php endforeach; ?>
    </ol>
  </div>
  <?php echo $block->getToolbarHtml() ?>    //Have called here
    ....

如果我在前端单击“类别”链接,这就是我得到的。

在此处输入图片说明

在Magento_CatalogSearch中

templates\result.phtml

在Magento_Theme中

templates\html\pager.phtml
layout\default.html
web\images\my_logo.png

现在result.phtml错误似乎消失了,但是我的产品仍然无法通过分页在前端呈现

Admin->Content->Design configuration-> Enabled my Custom Theme.

注意: 在我的自定义模块中,我删除了包含模板和布局文件的view文件夹。

问题

我必须从上述phtml文件中获取传呼机(分页),排序器等。

1)但是我的产品不是从result.phtml渲染的(但是我的var / log有产品ID)...前端不显示

2)没有出现分页或分类器


请参考此答案以解决您的第一个和第三个问题magento.stackexchange.com/a/86190/36463。在此答案链接中,遵循“ Mage2.PRO”答案。康提ANS不适合我
比拉尔Usean

从上面的链接中,我是否应该按照app / desin / <overidden template file>中的Mage2.pro的要求给出? ),目录页面如上图截图所示...请建议
Sushivam,2016年

Answers:


5

看来您必须在自定义phtml文件中设置传呼机和排序。

我遵循以下步骤pr添加分页和排序。

在您的阻止文件中,添加以下代码以添加分页:

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    array $data = []
) {
    $this->_productFactory = $productFactory;
    $this->urlHelper = $urlHelper;
    parent::__construct($context, $data);

    // Get your custom collection here

    $collection = $this->getCustomCollection();

    $this->setCollection($collection);
}

public function _prepareLayout()
{

    parent::_prepareLayout();
    if ($this->getCollection()) {

        // create pager block for collection 

        $toolbar = $this->getToolbarBlock();

        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager', 'list.pager'
        )->setCollection(
            $this->getCollection() // assign collection to pager
        );
        $toolbar->setChild('product_list_toolbar_pager', $pager); // set pager block in layout
        // called prepare sortable parameters
        $collection = $this->getCollection();

        // use sortable parameters
        $orders = $this->getAvailableOrders();

        if ($orders) {
            $toolbar->setAvailableOrders($orders);
        }
        $sort = $this->getSortBy();
        if ($sort) {
            $toolbar->setDefaultOrder($sort);
        }
        $dir = $this->getDefaultDirection();
        if ($dir) {
            $toolbar->setDefaultDirection($dir);
        }
        $modes = $this->getModes();
        if ($modes) {
            $toolbar->setModes($modes);
        }
        $toolbar->setCollection($collection);

        $this->setChild('toolbar', $toolbar);
        $this->getCollection()->load();
    }

    return $this;
}

public function getToolbarHtml()
{
    return $this->getChildHtml('toolbar');
}

public function getToolbarBlock()
{
    $blockName = $this->getToolbarBlockName();
    if ($blockName) {
        $block = $this->getLayout()->getBlock($blockName);
        if ($block) {
            return $block;
        }
    }
    $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));

    return $block;
}

public function getMode()
{
    return $this->getChildBlock('toolbar')->getCurrentMode();
}

还定义以下属性:

protected $_defaultToolbarBlock = 'Magento\Catalog\Block\Product\ProductList\Toolbar';

在phtml文件中,在列表前后添加以下代码:

<?php echo $block->getToolbarHtml() ?>

phtml文件与上述文件夹中粘贴的核心文件相同!并且我添加了受保护的$ _defaultToolbarBlock ='Magento \ Catalog \ Block \ Product \ ProductList \ Toolbar'; 在我的自定义ListProduct.php中
Sushivam '16

我所有的自定义phtml文件似乎都被调用了,但是不是我的类别和搜索中的产品收藏未加载!
Sushivam '16

对于分页,您不需要覆盖任何phtml文件。而且似乎您尚未在构造中设置collection。而且我不确定,但是请尝试使用_prepareLayout函数而不是_beforeToHtml的工具栏代码
Jaimin Parikh

您能否给我一个示例代码,以在我的块文件中获取分页(不覆盖phtml)?我的意思是我需要保留的确切功能是什么,我很困惑!
Sushivam '16

我注释掉了-beforeToHtml(),并在prepareLayout()中添加了getToolbarBlock(),还在construct()中设置了集合,我得到了main.CRITICAL:system.log中的非法状态[] []
Sushivam,2016年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.