Question

Actually i want to set list/grid mode(List Mode) configuration values for category page programmatically not from magento admin backend.if anyone have any idea please guide me.

Était-ce utile?

La solution

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <preference for="Magento\Catalog\Block\Product\ListProduct" 
            type="Vendor\Module\Block\Rewrite\Product\ListProduct" />
</config>

ListProduct.php

<?php
    namespace Vendor\Module\Block\Rewrite\Product;
    use Magento\Framework\View\Element\Template;

    class ListProduct extends \Magento\Catalog\Block\Product\ListProduct{
        public function getMode()
        {

        }
    }

Check This File :-

vendor/magento/module-catalog/view/frontend/templates/product/list.phtml

Line No. 28 :-

if ($block->getMode() == 'grid') {
        $viewMode = 'grid';
        $image = 'category_page_grid';
        $showDescription = false;
        $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    } else {
        $viewMode = 'list';
        $image = 'category_page_list';
        $showDescription = true;
        $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::FULL_VIEW;
    }

Autres conseils

Try to avoid using preference if possible as getMode() is a public function you can use a plugin instead.

Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top