How to set List Mode(backend configuration) programmatically for category page magento 2?
-
25-02-2021 - |
题
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.
解决方案
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;
}
其他提示
Try to avoid using preference if possible as getMode() is a public function you can use a plugin instead.