I am trying to show the attribute's dropdown values selected in product. For that I am using this way.

<?php $parentIds = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($_product->getId());

if(count($parentIds)>0 && $_product->getTypeId() == "simple")
        /*?><script>console.log('<?php echo $_product->getSku(); ?>')</script><?php */
else { 
        foreach ($_productCollection as $_product) :
            $_p = $objectManager->get('Magento\Catalog\Model\Product')->load($_product->getId());
            $productList[] = $_p;
            $priceRuleCats = $_p->getprice_rule_category();

                $priceRuleList = array_unique(array_merge($priceRuleList,explode(',',$priceRuleCats)));
    // save the full list 
    $fullList = $productList;

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $price_rules = $objectManager->get('Startupready\PriceRules\Model\Quantitybreaks');

    $current_table = 0;
    $size_options = array();
    $price_breaks = array();

    // use first product from loop to see what the size options are & load price breaks for those options
    $firstProduct = array_shift($productList);

    // this product's price rule category
    $price_rule_category = $firstProduct->getprice_rule_category();

    // loop through simple products
    $conf = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setProduct($firstProduct);
    $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
    foreach($simple_collection as $simple){
        echo $simple->getsize_option();
        //also i tried like this
        //echo "<pre>"; print_r($simple['size_option']); echo "</pre>";

but when I am putting my code inside the list.phtml file, no any products are displaying on this page.

Meanwhile I am just trying to get that product attribute inside this array so that if a product have assigned this values (doropdown values) then I'll show those values in frontend. Kindly please help me to do this. Thank in advance.

  • I think the bug is "setProduct()" function which i am using to assign my "$firstproduct". Because i search this function inside "Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable" path. but there is no any kind of function like this. And when i tried to create this function in my custom created module, and then connect my custom module. same thing my product not displaying on list page.
    – Yudi
    Commented Oct 19, 2016 at 6:04

1 Answer 1


First, I should start by saying that it is NOT recommended practice to directly instantiate the Object Manager. You can read more about that here. Although, it is not preventing your code from working.

Second, this logic does not belong in a template phtml file. You should be creating your own module, a small one, that runs through the logic and lets it echo out onto the page via a method call within your phtml file. Also, if you had a module, you could implement the necessary dependency injection to retrieve your data.

Thirdly, inside of the $_productCollection loop, you are calling $_p->load($_product->getId()). This is not only unnecessary but super inefficient.

Let it be known that the code sample below is enough to obtain the info you want to display, but by no means would I recommend implementing your custom code in this manner. So, once you get the data you seek, please see Alan Storm's Magento 2 Tutorial Series and patiently go through them from beginning to end. This will help you gain a better understanding as to how certain crucial elements on Magento 2 work.

Here is the quick and dirty way to get the data requested in your original question:

Find the line in the list.phtml file that looks like this <?php foreach($_productCollection as $_product): ?>. This is not the loop you created, but the loop that was already in the file before you edited it.

Completely replace this line:

<?php foreach($_productCollection as $_product): ?>

With this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\ConfigurableProduct\Model\Product\Type\ConfigurableFactory $configurableFactory */
$configurableFactory = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\ConfigurableFactory');

<?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
<?php foreach($_productCollection as $_product): ?>

    if($_product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
        /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable */
        $configurable = $configurableFactory->create();

        // 3 options to get attributes, but i am using
        # $attributes = $configurable->getConfigurableAttributes($_product);
        # $attributes = $configurable->getConfigurableAttributeCollection($_product);

        $attributes = $configurable->getConfigurableAttributesAsArray($_product);
        foreach($attributes as $attrId => $attribute){
            echo '<pre>',print_r($attribute),'</pre>';

This will expose all of the available data for each product and then you can do whatever you need to.

  • don't know why but when i am trying like this , my products are not showing on the list page...:(
    – Yudi
    Commented Oct 19, 2016 at 7:21
  • Something else must be wrong. Can you post the entire page of code? Also will check in the morning as I must sleep now. I did test my code before posting and it works so the key is to figure out what the remaining problem is. Commented Oct 19, 2016 at 7:23
  • you didnt post your file Commented Oct 19, 2016 at 12:33
  • I edited my question. please check it.
    – Yudi
    Commented Oct 19, 2016 at 12:47
  • looking now. What does $_p->getprice_rule_category() refer to? Is price_rule_category an attribute code? Commented Oct 19, 2016 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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