How to add customfields in product listing criteria?

How to add customfields in product listing, search criteria?

I have done this by using the following lines of code.

public static function getSubscribedEvents(): array
    {
        return [
            ProductEvents::PRODUCT_LISTING_CRITERIA => ‘productListingCriteria’,
        ];
    }

    public function productListingCriteria(ProductListingCriteriaEvent $event)
    {

        $event->getCriteria()->addAssociation(‘customFields’);

        $event->getCriteria()->addFilter(
            new RangeFilter(‘customFields.product_scheduled_from’, [RangeFilter::LTE => date(DATE_ATOM)]),
            new RangeFilter(‘customFields.product_scheduled_to’, [RangeFilter::GTE => date(DATE_ATOM)])
        );
        
        return true;
    }

3 Likes