2

I need to change the template for subcategories(like able to assign any category) and list.phtml is fine. But need to change template for random categories.

Any one have solution for this?

  • What do you mean by template? Do you need different layout or different theme? – Sukumar Gorai Jun 25 '19 at 8:18
  • different layout – Shomita Jun 25 '19 at 8:55
  • @SukumarGorai Do you have solution for this? – Shomita Jun 25 '19 at 11:30
  • If you want to use different list.phtml then you can try the solution of HoangHieu. If any issue let me know here. – Sukumar Gorai Jun 25 '19 at 12:26
3
  • Go to Manage category in Admin
  • In Design tab you will find Theme dropdown
  • Select your theme from there and this will apply to that category
| improve this answer | |
2

I have three options for you if you are a developer.

Option 1. You can use xml layout such as named catalog_category_view_id_[Cat Id].xml to change list.phtml template.

Option 2. Add new category attribute using setup upgrade

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'custom_product_list_template',
        [
            'type' => 'varchar',
            'label' => 'Category custom template',
            'input' => 'select',
            'sort_order' => 600,
            'source' => 'Namespace\CategoryCustom\Model\Category\Attribute\Source\Templates',
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => null,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group' => 'Design',
            'backend' => ''
        ]
    );

Namespace\CategoryCustom\Model\Category\Attribute\Source\Templates extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource

public function getAllOptions()
{
    if ($this->_options === null) {
        $this->_options = [
            ['label' => __('None'), 'value' => ''],
            ['label' => __('Custom Template'), 'value' => 'category_custom_template_file'],
        ];
    }
    return $this->_options;
}

Namespace/CategoryCustom/view/adminhtml/ui_component/category_form.xml

<fieldset name="display_settings">
    <field name="custom_product_list_template">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Namespace\CategoryCustom\Model\Category\Attribute\Source\Templates</item>
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">210</item>
                <item name="dataType" xsi:type="string">string</item>
                <item name="formElement" xsi:type="string">select</item>
                <item name="label" translate="true" xsi:type="string">Ground Custom Template</item>
            </item>
        </argument>
    </field>
</fieldset>

Add event observer to add layout update to events.xml

<event name="layout_load_before">
        <observer name="add_product_list_template" instance="Namespace\CategoryCustom\Observer\ProcessProduclistTemplate"/>
</event>

Observer class \Namespace\CategoryCustom\Observer\ProcessProduclistTemplate

public function execute(\Magento\Framework\Event\Observer $observer)
{
    if($observer->getFullActionName() == 'catalog_category_view'){
        $currentCategory = $this->_coreRegistry->registry('current_category');
        if(is_object($currentCategory) && $currentCategory->getId()){
            $handle = $currentCategory->getData('custom_product_list_template') ?: '';
            if($handle){
                $observer->getLayout()->getUpdate()->addHandle($handle);
            }
        }
    }
    return $this;
}

After that custom you layout in category_custom_template_file.xml

Option 3. Create new static block -> assign it to category and change the config of the category from (Display CMS and Product -> CMS).

| improve this answer | |
  • I don't want to add catalog_category_view_id_[Cat Id].xml Id wise. – Shomita Jun 25 '19 at 9:40
  • Let use option 2 and 3 – HoangHieu Jun 25 '19 at 9:45

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.