0

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;
    }
  • First of all, please check it $this->_getProductCollection(); return null or not. I think it return null. – Rohan Hapani Jul 4 at 11:49
  • @RohanHapani it is displaying data when i print it using $collection->getData() – Ansar Husain Jul 4 at 11:52
  • Oh..!! Then, MuhammadHasam answer is right. It should be working. – Rohan Hapani Jul 4 at 11:55
  • @RohanHapani i have tried MuhammadHasam answer its not working – Ansar Husain Jul 4 at 11:57
0

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

  • its not working – Ansar Husain Jul 4 at 11:40
  • Are you sure, you are overriding getLoadedProductCollection() function rightly? – Muhammad Hasham Jul 4 at 11:41
  • I am even adding this directly in core file yet for sure – Ansar Husain Jul 4 at 11:42
0

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

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