0

Hi i have recently created most viewed product sorting option for my store and its working fine but i want to add that option to be enable or disable from the backend here in sorting option where position, price and name is visible

enter image description here

0

Add InstallData.php for add attribute in category

VendoreName\ModuleName\Setup

InstallData.php

<?php
namespace VendoreName\ModuleName\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{


    protected $eavSetupFactory;

    public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '0.0.1', '<=')) {

            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Category::ENTITY,
                'is_enable_sorting',
                [
                    'type' => 'int',
                    'label' => 'Recently Created Most Viewed Product Sorting',
                    'input' => 'Boolean',
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                    'visible' => true,
                    'required' => false,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'group' => 'General Information',

                ]
            );
        }

        $setup->endSetup();
    }
}

overrite default category form and add our attribute

VendoreName/ModuleName/view/adminhtml/ui_component

category_form.xml

<?xml version="1.0" encoding="UTF-8"?>

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="display_settings">
        <field name="is_enable_sorting">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="formElement" xsi:type="string">checkbox</item>
                    <item name="label" xsi:type="string" translate="true">Recently Created Most Viewed Product Sorting</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                        <item name="valueMap" xsi:type="array">
                            <item name="true" xsi:type="string">1</item>
                            <item name="false" xsi:type="string">0</item>
                        </item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Now you can Filter category collection by this attribute

 protected $_categoryFactory;

 public function __construct(
       ..........................................
       ..........................................
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collecionFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        ..........................................
        ..........................................
    ) {
        ..........................................
        ..........................................
        $this->_categoryFactory = $collecionFactory;
        $this->_storeManager = $storeManager;
        ..........................................
        ..........................................
    }
    public function getSortingEnableCategory()
    {
        $category = $this->_categoryFactory->create();
        $category->addAttributeToFilter('is_enable_sorting', 1);
        $category->setStore($this->_storeManager->getStore());
        return $category;
    }

call in phtml file

$category = $block->getSortingEnableCategory();

enter image description here

I hope this Helps you.

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.