0

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.

1

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;
    }
  • your approach is correct only, but i want to set $block->getMode() value i.e. grid/list programmatically. – Hitesh Balpande Apr 3 '19 at 15:13
  • vendor/magento/module-catalog/view/frontend/templates/product/list.phtml this file put in your theme – Rk Rathod Apr 3 '19 at 15:15
  • /app/design/frontend/thme/themename/Magento_Catalog/templates/product/view/ paste file in this location – Rk Rathod Apr 3 '19 at 15:17
  • i have idea related but i don't want to change from list.phtml,i am trying to set configuration values programmatically – Hitesh Balpande Apr 3 '19 at 15:17
  • which file you want to get value ? – Rk Rathod Apr 3 '19 at 15:19
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.