Sự cố ghi đè tệp .phtml: Mô-đun Magento 2


8

Sự cố : Đang cố gắng ghi đè tệp phtml Magento mặc định, tôi đang gặp lỗi

Tôi cần có được những thứ này: (Phân trang ở chân trang, Trình sắp xếp)

nhập mô tả hình ảnh ở đây

Mô-đun tùy chỉnh của tôi:

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

Trong ứng dụng \ design \ frontend \ Vendor \ tag

nhập mô tả hình ảnh ở đây

MÃ CẬP NHẬT:

Đã xóa thư mục bên dưới

Trong thư mục 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

Đây là những gì tôi nhận được trang trống mà không có kết quả ở trang trước:

nhập mô tả hình ảnh ở đây

Đã thêm mã được đề cập trong tệp Chặn:

Nhà cung cấp \ Module \ Block \ Product \ ListSub.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;
}
}

Đối với phtml :

\ app \ design \ frontend \ Vendor \ tag \ Magento_Catalog \ samples \ sản phẩm \ 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
    ....

Đây là những gì tôi nhận được nếu tôi nhấp vào liên kết Danh mục ở lối vào.

nhập mô tả hình ảnh ở đây

Trong Magento_CatalogSearch :

templates\result.phtml

Trong Magento_Theme :

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

NGAY BÂY GIỜ, lỗi result.phtml dường như không còn nữa, nhưng các sản phẩm của tôi vẫn không hiển thị ở phía trước với phân trang

Trong

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

LƯU Ý: Trong mô-đun tùy chỉnh của tôi, tôi đã xóa thư mục xem có các tệp mẫu và bố cục.

Vấn đề :

Tôi phải lấy máy nhắn tin (phân trang), Trình sắp xếp, v.v. từ các tệp phtml ở trên.

1) Nhưng các sản phẩm của tôi không được hiển thị từ result.phtml (Nhưng var / log của tôi có id sản phẩm) ... frontend không hiển thị

2) Không có phân trang hoặc phân loại xuất hiện


tham khảo câu trả lời này để giải quyết vấn đề thứ 1 và thứ 3 của bạn magento.stackexchange.com/a/86190/36463 . Trong câu trả lời này, hãy làm theo câu trả lời "Mage2.PRO". Kandy ans không làm việc cho tôi
Bilal Usean

Từ liên kết trên, tôi có nên đưa ra như Mage2.pro đã đề cập trong ứng dụng / desin / <tệp mẫu bị che khuất>? Nếu vậy tôi đã thử tương tự, nhưng Pagination không xuất hiện (tôi đã giữ pager phtml và thanh công cụ. ) và trang danh mục đang đến như thể hiện trong ảnh chụp màn hình ở trên ... Xin đề nghị
Sushivam

Câu trả lời:


5

Có vẻ như bạn phải đặt máy nhắn tin và sắp xếp trong tệp phtml tùy chỉnh của bạn.

Tôi đã làm theo các bước dưới đây pr thêm phân trang và sắp xếp.

Trong tệp khối của bạn, thêm mã bên dưới để thêm phân trang:

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

Cũng xác định thuộc tính dưới đây:

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

Trong tệp phtml, thêm mã bên dưới trước và sau khi liệt kê:

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

Các tập tin phtml giống như trong lõi được dán trong thư mục được đề cập ở trên! và tôi đã thêm $ _defaultToolbarBlock = 'Magento \ Catalog \ Block \ Product \ ProductList \ Thanh công cụ'; trong danh sách tùy chỉnh của tôi Productt.php
Sushivam

Tất cả các tệp phtml tùy chỉnh của tôi dường như được gọi, nhưng y không phải là danh mục và bộ sưu tập sản phẩm của tôi trong tìm kiếm không tải!
Sushivam

để phân trang, bạn không cần ghi đè lên bất kỳ tệp phtml nào. và cũng có vẻ như bạn chưa đặt bộ sưu tập trong cấu trúc của mình. và tôi không chắc nhưng hãy thử mã thanh công cụ của bạn trong hàm _prepareLayout thay vì _b BeforeToHtml
Jaimin Parikh

Bạn có thể vui lòng cho tôi một mã ví dụ để lấy số trang (không ghi đè phtml) trong tệp khối của tôi không? ý tôi là tôi cần những chức năng chính xác mà tôi phải giữ, tôi bối rối!
Sushivam

Tôi nhận xét ra -beforeToHtml () và thêm vào getToolbarBlock () trong prepareLayout () một cũng thiết lập các bộ sưu tập trong xây dựng (), tôi nhận được main.CRITICAL: tình trạng bất hợp pháp [] [] trong system.log
Sushivam
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.