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?
Solved! Go to Solution.
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
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
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.
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
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?