1

I am trying to get the product based on the 3 rules. 1st rule -> the product should have been created within a month 2nd rule -> it should be belonging to a particular category 3rd rule ->it should not have any special price

My Code:

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('created_at', ['gteq' => $previousMonthDate]);
$collection->addCategoryFilter($category);

This is returning empty array any suggestions on applying filter?

1 Answer 1

2

You check and try the below example code. Check all your inputs in getting the collection date should be like as described for example. Also, Category should be category object.

$objectManager    = $bootstrap->getObjectManager();
$pc = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

// Category
$cat = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
$categoryId = 3;
$category = $cat->create()->load($categoryId);


$collection = $pc->create();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('created_at', ['gteq' => '2020-06-01 00:00:00']);
$collection->addCategoryFilter($category);

$items = $collection->getItems();
foreach ($items as $item) {
    print_r($item->getData());  
}

you can also refer below links for more information. -

For Special Price. - https://meetanshi.com/blog/check-if-product-has-a-special-price-in-magento-2/

Thank you

1
  • Thank you it worked.
    – sandesh s
    Jun 8, 2020 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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