cancel
Showing results for 
Search instead for 
Did you mean: 

How to add product_super_link in product collection

SOLVED

How to add product_super_link in product collection

Hello,

 

I want to filter product collection by super_attribute from configurable but I cannot find a solution for it.

 

I've tried 

 

$items->getSelect()->joinLeft(['link_table' => 'catalog_product_super_link'], 'link_table.product_id = e.entity_id', ['product_id', 'parent_id']);
$items->getSelect()->group('link_table.parent_id');

but with no success.

 

What I have is this: I have created an custom API to apply filters and all the filters are working ok, except the filter for super_attribute from configurable. I have attribute "size" as a super_attribute and I want to filter by size = 35 but is returning empty. When I remove the filter 

 

$item->addFieldToFilter('visibility', 4)

then is returning results, but not the expected ones as it is providing simple and configurable ones. I could filter the simple from this, but before loading the collection, I am doing a count for collection in order to provide also the number of pages needed base on pagination rulles.

 

Can someone help me with this?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to add product_super_link in product collection

Hello @capoan 

 

Kindly refer below code for get collection by size:

 public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory = $productCollectionFactory;    
        parent::__construct($context, $data);
    }
    
    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('size',  array('eq' => 35));
        return $collection;
    }

It may help you!

Thank you

Problem solved? Click Accept as Solution!

View solution in original post

4 REPLIES 4

Re: How to add product_super_link in product collection

Hello @capoan 

 

Kindly refer below code for get collection by size:

 public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory = $productCollectionFactory;    
        parent::__construct($context, $data);
    }
    
    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('size',  array('eq' => 35));
        return $collection;
    }

It may help you!

Thank you

Problem solved? Click Accept as Solution!

Re: How to add product_super_link in product collection

Hello @Bhanu Periwal 

 

This will not work as configurable product does not have this attribute. Only the simple products from configurable will have this attribute as part of configuration. I need to find a way to get the size attribute from simple products and collection to provide configurable products as part of the list.

Re: How to add product_super_link in product collection

Hello @capoan 

 

The code I have shared with you, will get all products which has size specific value either its child or belongs to any configurable product.

Thanks

Problem solved? Click Accept as Solution!

Re: How to add product_super_link in product collection

Hello @Bhanu Periwal 

 

Indeed is working. I had a concatenation for multiple filters and this was the reason that was not working. Thank you very much for this.

 

Question extra: Is it possible to add in this filtering also the qty > 1 (not saleable attribute, but qty attribute) for simple products? Ex: I want to get all configurable with size 34 were simple product with size 34 has qty > 1?