Magento – How to get the price of all simple products assigned to configurable products in magento 2

configurable-productmagento2pricesimple-product

I am trying to display prices of all the simple products which are assigned to the configurable products. For that I am using:

<?php 
    $attributes = $configurable->getConfigurableAttributesAsArray($_product);
    foreach($attributes as $attrId => $attribute){
        $att_value = $attribute['values'];
        echo "<pre>"; print_r($att_value); echo "</pre>";
    }
?>

After this is I am not getting price of the simple products, although I got the variations of the configurable product. But I want to display the price of the simple products. How can I do this?

Best Answer

You can try below code to get data of simple products assigned to configurable products.

    $configurableOptions = $_product->getConfigurableAttributesData();

    foreach ($configurableOptions['matrix'] as $option) {
       echo $option['price'];
    }

You can observe the reference file for more details.

\vendor\magento\magento2-base\dev\tests\functional\tests\app\Magento\ConfigurableProduct\Test\Constraint\AssertConfigurableProductPage.php
Related Topic