3

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?

  • By using Configurable Product Id. Load all simple products. In loop getPrice function stackoverflow.com/questions/34740555/… – Ankit Shah Oct 25 '16 at 7:28
  • @AnkitShah Can you please help me to do that..? I tried alot, But not working... <?php $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollection->create()->addAttributeToSelect('*')->load( $configurable_pro_id); ?> But Again its showing all the simple products. – Yudi Oct 27 '16 at 11:54
2

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
  • This is working. Thanks...But I am getting all the simple products instead current category's configurable products->simple products...How to get these by category? – Yudi Oct 25 '16 at 11:09
  • As per the question, you need to use/send configurable product into $_product to get the sample products price, those are assigned to configurable product. As i mention please have a look at reference file to get more detailed information. – Krishna ijjada Oct 25 '16 at 11:13

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.