Magento 2.1 Attributes – Get Simple Product by Attribute IDs

attributesconfigurable-productmagento-2.1magento2

I have been searching and cannot find the answer to this!

I have the following data being sent to my controller:

{
  "93":"52",
  "142":"173",
  "product":"1799"
}

The product key is the configurable product ID. The 2 numeric keys are attribute_id: atrribute_value_id.

How do I go about loading the simple product based on these details?

Using version 2.1.5

Update

This is actually the same (minus a couple of irrelevant details) as what Magento uses to add a configurable product to the cart from the category listing page. So what I am asking is definitely possible!

I have tried tracing the steps that Magento goes through from /checkout/cart/add but I am not seeing HOW Magento does this.

Another Update as there is no activity on this question

I have got the following code working to get the attribute codes and child IDs from the configurable parent. Can anyone come up with a way to filter these children by the attributes?

$attr = $this->eavAttribute->get('catalog_product', 93);
$attributeCodes[] = $attr->getAttributeCode();

$attr = $this->eavAttribute->get('catalog_product', 142);
$attributeCodes[] = $attr->getAttributeCode();
$product = $this->productRepository->getById($productId, false, $storeId

if ($product->getTypeId() == 'configurable') {
    $ids = $this->configurable->getChildrenIds($productId);
}

PS

I hate to complain but my questions on here hardly ever get any attention at all and this is no different. I am starting to lose hope in magento.stackexchange as it seems like nobody comes on here to answer questions. I know there are Magento 2 gurus out there, but where are you?

Best Answer

Thanks for the help on this question guys! With all the attention this question has grabbed, I figured I should answer for anyone else who is stuck. #Sarcasm.

The following method exists: \Magento\ConfigurableProduct\Model\Product\Type\Configurable::getProductByAttributes()

It is implemented as follows:

$childProduct = $this->configurable->getProductByAttributes(
    $data, $product
);

This returns a Magento product object of type \Magento\Catalog\Model\Product.

There is no way I am the only person on this site who knows this. I have been developing with Magento for nearly 2 years and the lack of support form the Magento community is excruciating!

This site exists to help people out. Please guys, lets do that.

Related Topic