1

I am overriding below product collection function with sku filter but its giving all results in category page including that sku product.

    /**
     * Retrieve loaded category collection
     *
     * @return AbstractCollection
     */
    public function getLoadedProductCollection()
    {
          $collection = $this->_getProductCollection();
          $collection->addAttributeToFilter('sku', 'test');
          return $collection;
    }
4
  • First of all, please check it $this->_getProductCollection(); return null or not. I think it return null. Jul 4 '19 at 11:49
  • @RohanHapani it is displaying data when i print it using $collection->getData() Jul 4 '19 at 11:52
  • Oh..!! Then, MuhammadHasam answer is right. It should be working. Jul 4 '19 at 11:55
  • @RohanHapani i have tried MuhammadHasam answer its not working Jul 4 '19 at 11:57
1

Use this code, I think you have to dispatch after collection filter

public function getLoadedProductCollection()
{

    $collection = $this->_getProductCollection();
    $collection->addAttributeToFilter('sku', 'test');
    $this->_eventManager->dispatch(
        'catalog_block_product_list_collection',
        ['collection' => $collection]
    );

    return $collection;
}

I hope this will help, though I don't test my code

3
  • its not working Jul 4 '19 at 11:40
  • Are you sure, you are overriding getLoadedProductCollection() function rightly? Jul 4 '19 at 11:41
  • I am even adding this directly in core file yet for sure Jul 4 '19 at 11:42
1

Try below code ->addAttributeToFilter('sku', array('like' => '%24-MB01%'))

public function getLoadedProductCollection()
{
      $collection = $this->_getProductCollection()
                         ->addAttributeToSelect('*')
                         ->addAttributeToFilter('sku', 
                            array('like' => '%24-MB01%'))
                         ->load();
}
7
  • I have done same but not working Jul 4 '19 at 11:24
  • check updated answer.
    – Mohit Rane
    Jul 4 '19 at 11:27
  • Yes Mohit not working above code can you check your side ? Jul 4 '19 at 11:32
  • can you try like this ->addAttributeToFilter('sku', array('eq' => '24-MB01'));
    – Mohit Rane
    Jul 4 '19 at 11:37
  • No working Mohit Jul 4 '19 at 11:41

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.