2

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?

  • It's tricky because Magento stores values in _int, _varchar, _decimal, _datetime different different table. As per ur attribute type – Ankit Shah May 11 '17 at 2:27
  • @AnkitShah I know, however, this is the same info that Magento uses to add a product to the cart so it must be achievable. See my update. – Wildcard27 May 11 '17 at 2:57
3

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.

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.