2

I want to get all products by rating filter. here my code

$getProducts = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$getProducts->addAttributeToSelect('*');
$getProducts->addAttributeToFilter('visibility', 4);

So, how can I add here filter for rating so I need not to first get all product and then check all product with that rating issue.

thanks

2

2 Answers 2

4

You can try the below code for filter product collection by rating percent:

$collection = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection->addAttributeToSelect('*');

$collection->getSelect()->joinLeft(array('rova'=> 'rating_option_vote_aggregated'),'e.entity_id =rova.entity_pk_value', array("percent" => 'percent'))->group('e.entity_id');

$collection->getSelect()->where("rova.percent = 80");

foreach($collection as $product){
    //YOUR CODE
}

100 Percent = 5 Star Rating

90 Percent = 4.5 Star Rating

80 Percent = 4 Star Rating

and so on...

3
  • can you give me average counting percentage? Dec 27, 2019 at 9:34
  • You need to customise the query for that. Dec 27, 2019 at 9:37
  • Yes but for average how can I write query can you tell me sir. Dec 27, 2019 at 9:42
0

you have to override model from core like below.

then you have to extends in your local custom file. class ProductRepository extends \Magento\Catalog\Model\ProductRepository

Then you have to right below getlist function for custom rating code collection.

public function getList(SearchCriteriaInterface $searchCriteria) {

    $searchCriteriaData = $this->http->get('searchCriteria');
    

    if (!$searchCriteria) {
        return;
    }
    foreach (array_keys((array)$searchCriteriaData) as $key) {
        $value = &$searchCriteriaData[$key];
        unset($searchCriteriaData[$key]);
        $searchCriteriaData[SimpleDataObjectConverter::camelCaseToSnakeCase($key)] = $value;
    }

    

    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
    $collection = $this->collectionFactory->create();
    $this->extensionAttributesJoinProcessor->process($collection);

    $collection->addAttributeToSelect('*');
    $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
    $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
    $this->joinPositionField($collection, $searchCriteria);


    if(isset($searchCriteriaData['filter_groups'][1]['filters'][0]['field']) && $searchCriteriaData['filter_groups'][1]['filters'][0]['field'] == 'rat')
    {
        
        $collection->getSelect()->joinLeft(array('rova'=> 'rating_option_vote_aggregated'),'e.entity_id =rova.entity_pk_value',array("percent as rating"))
        ->where("rova.percent between ".$searchCriteriaData['filter_groups'][1]['filters'][0]['value']." and ".$searchCriteriaData['filter_groups'][1]['filters'][1]['value'])
        ->group('e.entity_id') ->GROUP("rova.percent");            
        $count = $collection->count();
         
    }else{
        $this->collectionProcessor->process($searchCriteria, $collection);      
    }
    

    $collection->load();

    $collection->addCategoryIds();
    $this->addExtensionAttributes($collection);
    $searchResult = $this->searchResultsFactory->create();
    $searchResult->setSearchCriteria($searchCriteria);
    $searchResult->setItems($collection->getItems());
    if(isset($searchCriteriaData['filter_groups'][1]['filters'][0]['field'] ) && $searchCriteriaData['filter_groups'][1]['filters'][0]['field'] == 'rat')
    {
        $searchResult->setTotalCount($count);    
    }else{
        $searchResult->setTotalCount($collection->getSize());    
    }
    

    foreach ($collection->getItems() as $product) {
        $this->cacheProduct(
            $this->getCacheKey(
                [
                    false,
                    $product->getStoreId()
                ]
            ),
            $product
        );
    }

    return $searchResult;
}

This is for rest API.

Your Answer

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

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