0

Following is the code to get product ID from attribute value in Magento 2.2.

for ($i = 0; $i < sizeof($post['prod_attr']); $i++) {    
     $attribute = $this->eavConfig->getAttribute('catalog_product', 'attribute1');
            $options = $attribute->getSource()->getAllOptions();

            $attributeAttr = $this->eavConfig->getAttribute('catalog_product', 'attribute2');
            $optionsAttr = $attributeAttr->getSource()->getAllOptions();

            $productModel = $this->productFactory->create();

            foreach ($options as $option) {
                if ($option['label'] == $post['prod_attr'][$i][0]) {
                    $this->productCollection->addAttributeToFilter(
                            array(
                        array('attribute' => 'attribute1', 'eq' => $option['value']),
                            ), '', 'left'
                    );
                }
            }

            foreach ($optionsAttr as $optionAttr) {
                if ($optionAttr['label'] == $post['prod_attr'][$i][1]) {
                    $this->productCollection->addAttributeToFilter(
                            array(
                        array('attribute' => 'attribute2', 'eq' => $optionAttr['value']),
                            ), '', 'left'
                    );
                }
            }

            $this->productCollection->addAttributeToFilter(
                    'status', ['in' => $this->productStatus->getVisibleStatusIds()]
            );
            $this->productCollection->setVisibility($this->productVisibility->getVisibleInSiteIds());

            foreach ($this->productCollection as $product) {
                $productId = $product->getId();

                $product = $productModel->load($productId);

                $productType = $product->getTypeID();
                if ($productType == self::PRODUCT_TYPE_SIMPLE) {
                    $params = array('qty' => $qty);

                    try {
                        $this->cart->addProduct($product, $params);
                        $this->cart->save();
                        $this->messageManager->addSuccess(__("You added " . $product->getName() . " to your shopping cart."));
                    } catch (LocalizedException $e) {
                        $this->messageManager->addError($e->getMessage());
                    }
                }
            }
}

Issue I am facing here is every time I get same product id when I execute this code in for loop.

Please let me know what I am missing here.

  • Can you describe that, at which place you put this code. – Dhiren Vasoya Nov 22 '17 at 10:35
  • @Dhiren Code is in my observer where I get an array of attributes, I run this code through for loop but for every different attribute I get first product ID as I have mentioned. – DEEP JOSHI Nov 22 '17 at 10:51
  • can you specify your entire code with for loop which you have done in observer, So it is easy to understand. – Dhiren Vasoya Nov 22 '17 at 10:59
  • @DhirenVasoya I have updated code. Please have a look. – DEEP JOSHI Nov 22 '17 at 11:02
  • in your above code some places you have used static "attribute1" and "attribute2", I think you need pass current attribute for which code is execute. – Dhiren Vasoya Nov 22 '17 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.