1

I need to create featured page / category where all featured products are grouped.

By default I create an hidden (from the menu) category and I add products both to featured category and with featured flag.

I was wondering if there is nicer way to do this. Is there already a page, by default, that includes all featured products? Or is there a way to automate adding a product to a category if it gets featured?

Any other suggestions are welcome.

1

You can assign product to featured category if it gets featured using observer. You can easily use catalog_product_save_after event.

I am assuming that you already know how to create a module in M2.

Put this events.xml in below path

app\code\YOUR_NAMESPACE\YOURMODULE\etc\adminhtml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="test_name" instance="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>

And put your Productsaveafter.php in below path

app\code\YOUR_NAMESPACE\YOURMODULE\Observer\

<?php

namespace YOURNAMESPACE\YOURMODULENAME\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();  // you will get product object
        $_isFeatured = $_product->getIsFeatured();
        if($_isFeatured == 1){
            //your further code here to assign this product to featured category
        }
    }   
}

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.