Problema che sostituisce il file .phtml: modulo catalogo Magento 2


8

Problema: tentativo di sovrascrivere il file phtml di Magento predefinito, sto ottenendo un errore

Devo ottenere questi: (Impaginazione a piè di pagina, Sorter)

inserisci qui la descrizione dell'immagine

I miei moduli personalizzati:

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

In app \ design \ frontend \ Vendor \ tag

inserisci qui la descrizione dell'immagine

CODICE AGGIORNATO:

RIMOSSA la cartella seguente

Nella cartella 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

Questo è ciò che ottengo pagina vuota senza risultati nella prima pagina:

inserisci qui la descrizione dell'immagine

Aggiunto il codice citato nel file Block:

Venditore \ Module \ Blocco \ prodotto \ 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;
}
}

Per phtml :

\ App \ progettazione \ frontend \ Venditore \ tag \ Magento_Catalog \ templates \ prodotto \ 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
    ....

Questo è quello che ottengo se clicco sul link Categoria in frontend.

inserisci qui la descrizione dell'immagine

In Magento_CatalogSearch :

templates\result.phtml

In Magento_Theme :

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

ADESSO l'errore risultato.phtml sembra essere scomparso, ma i miei prodotti non vengono ancora visualizzati in frontend con l'impaginazione

In

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

NOTA: Nel mio modulo personalizzato, ho rimosso la cartella di visualizzazione che aveva modelli e file di layout.

Problema :

Devo ottenere il cercapersone (impaginazione), Sorter ecc dai file phtml sopra.

1) Ma i miei prodotti non sono resi da result.phtml (Ma il mio var / log ha gli ID prodotto) ... frontend non visualizzato

2) Non appare alcuna paginazione o selezionatrice


fai riferimento a questa risposta per risolvere il tuo primo e terzo problema magento.stackexchange.com/a/86190/36463 . In questo link di risposta segui la risposta "Mage2.PRO". kandy ans non funziona per me
— Bilal Usean,

Dal link sopra, dovrei dare come menzionato da Mage2.pro nell'app / desin / <file modello nascosto>? In tal caso ho provato lo stesso, ma l'impaginazione non appare (ho mantenuto il cercapersone phtml e la barra degli strumenti.phtml ) e la pagina del catalogo sta arrivando come mostrato nello screenshot qui sopra ... Pls suggerisce
— Sushivam,

Risposte:


5

Sembra che tu debba impostare il cercapersone e l'ordinamento nel tuo file phtml personalizzato.

Ho seguito i passaggi seguenti per aggiungere l'impaginazione e l'ordinamento.

Nel tuo file di blocco aggiungi sotto il codice per aggiungere l'impaginazione:

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

Definisci anche sotto la proprietà:

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

Nel file phtml aggiungi sotto il codice prima e dopo l'elenco:

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

I file phtml sono gli stessi del core incollato nella suddetta cartella! e ho aggiunto $ _defaultToolbarBlock = 'Magento \ Catalog \ Block \ Product \ ProductList \ Toolbar' protetto; nel mio ListProduct.php personalizzato
— Sushivam,

Tutti i miei file phtml personalizzati sembrano essere chiamati, ma non le mie categorie e la raccolta di prodotti nella ricerca non vengono caricati!
— Sushivam,

per l'impaginazione non è necessario sovrascrivere alcun file phtml. e inoltre sembra che tu non abbia impostato la raccolta nel tuo costrutto. e non sono sicuro, ma prova il codice della barra degli strumenti nella funzione _prepareLayout invece di _beforeToHtml
— Jaimin Parikh

Potete per favore darmi un codice di esempio per ottenere l'impaginazione (senza sovrascrivere phtml) nel mio file di blocco? Voglio dire, ho bisogno di quali sono le esatte funzioni che devo mantenere, sono confuso!
— Sushivam,

Ho commentato -beforeToHtml () e ho aggiunto getToolbarBlock () in preparazioneLayout () e ho anche impostato la raccolta in construct (), ottengo main.CRITICAL: Stato illegale [] [] in system.log
— Sushivam
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.