1

How can i programatically get special price product collection?

2

You can get products with a special price greater then 0 using product repository with search criteria:

/**
 * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
 * @param \Magento\Framework\Api\SearchCriteriaInterface $criteria
 * @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
 * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
 */
public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Framework\Api\SearchCriteriaInterface $criteria,
    \Magento\Framework\Api\Search\FilterGroup $filterGroup,
    \Magento\Framework\Api\FilterBuilder $filterBuilder
) {
    $this->productRepository = $productRepository;
    $this->searchCriteria = $criteria;
    $this->filterGroup = $filterGroup;
    $this->filterBuilder = $filterBuilder;
}

/**
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 */
public function getProductData()
{

    $this->filterGroup->setFilters([
        $this->filterBuilder
            ->setField('special_price')
            ->setConditionType('gt')
            ->setValue(0)
            ->create(),
    ]);

    $this->searchCriteria->setFilterGroups([$this->filterGroup]);
    $products = $this->productRepository->getList($this->searchCriteria);
    $productItems = $products->getItems();

    return $productItems;
}

Result should be like this:

result

UPDATE:

If you want to filter selected items by visibility & status - just add corresponding filters:

/**
 * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
 * @param \Magento\Framework\Api\SearchCriteriaInterface $criteria
 * @param \Magento\Framework\Api\Search\FilterGroup $filterGroup
 * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
 * @param \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus
 * @param \Magento\Catalog\Model\Product\Visibility $productVisibility
 */
public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Framework\Api\SearchCriteriaInterface $criteria,
    \Magento\Framework\Api\Search\FilterGroup $filterGroup,
    \Magento\Framework\Api\FilterBuilder $filterBuilder,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility
) {
    $this->productRepository = $productRepository;
    $this->searchCriteria = $criteria;
    $this->filterGroup = $filterGroup;
    $this->filterBuilder = $filterBuilder;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;
}

/**
 * @return \Magento\Cms\Model\Block|null
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 */
public function getProductData()
{

    $this->filterGroup->setFilters([
        $this->filterBuilder
            ->setField('special_price')
            ->setConditionType('gt')
            ->setValue(0)
            ->create(),
        $this->filterBuilder
            ->setField('status')
            ->setConditionType('in')
            ->setValue($this->productStatus->getVisibleStatusIds())
            ->create(),
        $this->filterBuilder
            ->setField('visibility')
            ->setConditionType('in')
            ->setValue($this->productVisibility->getVisibleInSiteIds())
            ->create(),
    ]);

    $this->searchCriteria->setFilterGroups([$this->filterGroup]);
    $products = $this->productRepository->getList($this->searchCriteria);
    $productItems = $products->getItems();

    return $productItems;
}
  • In the above code i am getting all the products with special price, Is there anyway to get the products that are currently active in special price.. – srg Aug 9 '17 at 7:28
  • @srg Yes, just modify your search criteria. You can find an example here – Siarhey Uchukhlebau Aug 9 '17 at 7:39
  • I have tried but the result is same...i am getting all special products ..can you please help me to apply search criteria.. – srg Aug 9 '17 at 9:11
  • @srg Please, see updated answer – Siarhey Uchukhlebau Aug 9 '17 at 9:30
  • i have tried in the same way that you have mentionde above.but the product with special price date is ended are also still listing in this. in this i think i want to check the current date, how can i check the end date and current date to display products. – srg Aug 9 '17 at 10:21

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.