We are using Magento 2.1.7 and building some API endpoints to serve our need.
Following is how we get Products list and send them as json from API call.
<?php
namespace Vendor\Customer\Model;
use Magento\Framework\Api\Filter;
class ProductRepository
{
protected $_productCollection, $productRepository, $searchCriteriaBuilder;
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
)
{
$this->productRepository = $productRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
/**
* @return \Magento\Catalog\Api\Data\ProductSearchResultsInterface
*/
public function getProductCollection()
{
$searchCriteria = $this->searchCriteriaBuilder->create();
$this->_productCollection = $this->productRepository->getList($searchCriteria);
return $this->_productCollection;
}
}
We need to know if we can add some search criteria to getProductCollection() method using Filter to get only those products associated to some active catalog price rule with discounted price.
Or if we have any other method to get Products with Catalog Price Rule and discounted Price, guide me in that direction.
Best Answer
When you create a catalog price rule, Magento creates some entries in
catalogrule_product
table. You can see which products are affected by the price rules created.Unfortunately
ProductRepository
does not allow you to filter by catalog price rule. However, you can modify this class by extending it.The only thing you need to add is the following line.
Let's say create
MyProductRepository
as follows:Be careful, you cannot use
\Magento\Catalog\Api\ProductRepositoryInterface
anymore. You have to call your implementation.