Question

I want to get configuration product attributes values in phtml file.

enter image description here

Above two value i want to get in magento 2.

So please Explain me How to get.

THANKS.

Was it helpful?

Solution

I have try two solutions for the possibility

First Method get Configurable products all assigned attribute values.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load(**product id**);

$productTypeInstance = $objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable');
$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $key => $value) {

    $tmp_option = $value['values'];
    if(count($tmp_option) > 0)
    {
        echo "<h3>".$value['label']."</h3>";
        echo "<select id='".$key."_".$value['label']."'>";
        foreach ($tmp_option as $tmp) 
        {
            echo "<option value='".$key."_".$tmp['value_index']."'>".$tmp['label']."</option>";
        }
        echo "</select>";
    }
}

Second Method is get Configurable product particular assigned attribute values gets.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load('60');

$productTypeInstance = $objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable');
$productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $key => $value) {
    if($key == '150'){
        $tmp_option = $value['values'];
        if(count($tmp_option) > 0)
        {
            echo "<h3>".$value['label']."</h3>";
            echo "Hello Testing" .  "<select id='".$key."_".$value['label']."'>";
            foreach ($tmp_option as $tmp) 
            {
                echo "<option value='".$key."_".$tmp['value_index']."'>".$tmp['label']."</option>";
            }
            echo "</select>";
        }
    }
}

Third Method i have Create with objectManager.

namespace Options\Configurable\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{

    protected $_registry;
    
    public function __construct(
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\Product $product,
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable
    ) {
        $this->_registry = $registry;
        $this->_product = $product;
        $this->_configurable = $configurable;
    }

    public function getConfigAttributes()
    {
        $product = $this->_registry->registry('current_product');
        $product = $this->_product->load($product->getId());
        $productAttributeOptions = $this->_configurable->getConfigurableAttributesAsArray($product);
        return $productAttributeOptions;
    }
}

And get function .phtml file

<?php
$customHelper = $this->helper('Options\Configurable\Helper\Data');
$productAttributeOptions = $customHelper->getConfigAttributes();

foreach ($productAttributeOptions as $key => $value) {

    $tmp_option = $value['values'];
    if(count($tmp_option) > 0)
    {
        echo "<h3>".$value['label']."</h3>";
        echo "<select id='".$key."_".$value['label']."'>";
        foreach ($tmp_option as $tmp) 
        {
            echo "<option value='".$key."_".$tmp['value_index']."'>".$tmp['label']."</option>";
        }
        echo "</select>";
    }

THANKS.

OTHER TIPS

Change the layout file where you want, for example I am using cms_index_index.xml in Sathya/ConfigProduct/view/frontend/layout

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Catalog\Block\Product\View" name="ConfigProduct.Attributes" template="Sathya_ConfigProduct::attributes.phtml">
            <arguments>
                <argument name="view_model" xsi:type="object">Sathya\ConfigProduct\ViewModel\Attributes</argument>
            </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

In attributes.phtml file under Sathya/ConfigProduct/view/frontend/templates

<?php
/** @var $block View */
/**@var Sathya\ConfigProduct\ViewModel\Attributes $viewModel */
$viewModel = $block->getData('view_model');

use Magento\Catalog\Block\Product\View;

?>
<?php $product = $block->getProduct(); ?>

<?php if ($product->getTypeId() == 'configurable'): ?>
    <?php $options = $viewModel->getAttributeOptions($product); ?>

    <?php foreach ($options as $option): ?>
        <?php $values = $viewModel->getAttributeValue($option); ?>
        <label><?= $option['label'] ?>
            <select>
                <?php foreach ($values as $value): ?>
                    <option><?= $value ?></option>
                <?php endforeach; ?>
            </select>
        </label>
    <?php endforeach; ?>
<?php endif; ?>

View Model in Sathya/ConfigProduct/ViewModel as Attributes.php

<?php


namespace Sathya\ConfigProduct\ViewModel;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\View\Element\Block\ArgumentInterface;

class Attributes implements ArgumentInterface
{
    protected $productRepository;

    public function __construct(ProductRepository $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    public function getAttributeOptions($product)
    {
        $options = [];
        $productAttributeOptions = $product->getTypeInstance()->getConfigurableAttributesAsArray($product);
        foreach ($productAttributeOptions as $attributeOption) {
            $options[] = $attributeOption;
        }
        return $options;
    }

    public function getAttributeValue($attributeOptions)
    {
        $attributeValues = [];
        foreach ($attributeOptions['values'] as $attribute) {
            $attributeValues[] = $attribute['store_label'];
        }
        return $attributeValues;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top