I am trying to pull configurable products option and price on product detail page on seprate section. on radio button. I've used below code to get detail of options but i can retrive price of options.

$blockObj = $block->getLayout()->createBlock('Magento\ConfigurableProduct\Block\Product\View\Type\Configurable');


$_attributes = $blockObj->decorateArray($blockObj->getAllowAttributes());

 foreach ($_attributes as $_attribute): ?>

    <?php foreach ($_attribute->getData('options') as $option):    ?>
        // option detail here
     <?php endforeach; ?>
 <?php endforeach; ?>

I've also try to get product id of options but i can get in above data. anyone have idea. how to get it? Note: I've used objectmanager for testing now. I'll use class to use once my code will be ready

  • Prices are associated with the child products of a configurable product, not options. You should just get the prices of the child products. – subroutines May 5 '17 at 0:17
  • @subroutines But there will be some way to get child product id from option id. right? I didn't find it. – Kul May 5 '17 at 8:39
up vote 13 down vote accepted
+100

This code tested on Magento 2.1.4.

All you need is in getConfigurableOptions call.

67 - is a test product ID. In my installation that is a configurable product id. Change it to your product id.

Copy paste it to testfile.php and run php config.php inside magento root:

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$repository = $obj->create('Magento\Catalog\Model\ProductRepository');
$product = $repository->getById('67');

$data = $product->getTypeInstance()->getConfigurableOptions($product);

$options = array();

foreach($data as $attr){
  foreach($attr as $p){
    $options[$p['sku']][$p['attribute_code']] = $p['option_title'];
  }
}


foreach($options as $sku =>$d){
  $pr = $repository->get($sku);
  foreach($d as $k => $v)
    echo $k.' - '.$v.' ';
  echo ' : '.$pr->getPrice()."\n";
}

Here is the output: enter image description here

  • Perfect! Thanks man. Just once correction use php testfile.php instead of php config.php – Kul May 9 '17 at 7:01
  • alright did I earn the bounty? – Konstantin Gerasimov May 9 '17 at 13:39
  • Ofcourse. sorry forgot to award bounty. – Kul May 9 '17 at 14:04
  • Please Update same code for Magento 2 – SURENDER SINGH Mar 30 at 22:32

Please try below code to get child product object.

 $_children = $_product->getTypeInstance()->getUsedProducts($_product);
    foreach ($_children as $child){
        $logger->info("Here are your child Product Ids ".$child->getID());
    }

Hope this will help you.

  • Thanks. but i want to show price with option. not only child product of configrable product. How can i get relation of option and child product? – Kul May 4 '17 at 6:56

What if, not evenn tested but think should work, inject \Magento\Catalog\Model\ProductRepository, load some product with method: getById

    $result = [];
    $options = (array)$product->getOptions();
    foreach ($options as $option) {
        $optionValues = $option->getValues() ? $option->getValues() : [];
        $optionValuesArray = [];
        foreach ($optionValues as $optionValue) {
            $optionValuesArray[] =  [
                'option_id'  => $optionValue->getOptionTypeId(),
                'sku'                 => $optionValue->getSku(),
                'title'           => $optionValue->getTitle(),
                'price'           => $optionValue->getPrice(),
                'price_type'      => $optionValue->getPriceType()
            ];
        }

        $resultOption = [
                    'option_id'  => $option->getOptionId(),
                    'title'      => $option->getTitle(), 
                    'type'       => $option->getType(),
                    'price'      => $option->getPrice(),
                    'price_type' => $option->getPriceType(),
                    'require' => $option->getIsRequire(),
                    'values' => $optionValuesArray
                ]

        $result[] = $resultOption;
    }

To get the configurable option along with their price all you need to do is,

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);
if(!empty($product->getOptions()))
{
foreach($product->getOptions() as $o) {
                foreach ($o->getValues() as $value) {
                    $data=$value->getData();
$id=$data['option_type_id'];
$price=$data['price'];
$title=$data['title'];
}
}
}

I have used objectManager though it is not the recommended way to do it. Just use this code for reference.You can do this by dependency injection. Hope it helps someone.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.