Loading...
How to display a child product’s attribute value at a configurable product’s grid panel?

How to display a child product’s attribute value at a configurable product’s grid panel?

Introduction

To display a child product's attribute value in a configurable grid panel, you can create a new column in the grid panel and set its data source to the attribute value of the child product. This can be done through programming or with the help of a plugin that allows customization of grid panels.

Step-1

Magento\ConfigurableProduct\Ui\DataProvider\Product\Form\Modifier\ConfigurablePanel.php

Find function getRows() and add the heading. Like :- ‘manufacturer_country’ => $this->getColumn(‘manufacturer_country’, __(‘Manufacturer Country’)),

Working Example for ConfigurablePanel.php

php

protected function getRows()
{
   return [
       'record' => [
           'arguments' => [
               'data' => [
                   'config' => [
                       'componentType' => Container::NAME,
                       'isTemplate' => true,
                       'is_collection' => true,
                       'component' => 'Magento_Ui/js/dynamic-rows/record',
                       'dataScope' => '',
                   ],
               ],
           ],
           'children' => [
               'thumbnail_image_container' => $this->getColumn(
                   'thumbnail_image',
                   __('Image'),
                   [
                       'fit' => true,
                       'formElement' => 'fileUploader',
                       'componentType' => 'fileUploader',
                       'component' => 'Magento_ConfigurableProduct/js/components/file-uploader',
                       'elementTmpl' => 'Magento_ConfigurableProduct/components/file-uploader',
                       'fileInputName' => 'image',
                       'isMultipleFiles' => false,
                       'links' => [
                           'thumbnailUrl' => '${$.provider}:${$.parentScope}.thumbnail_image',
                           'thumbnail' => '${$.provider}:${$.parentScope}.thumbnail',
                           'smallImage' => '${$.provider}:${$.parentScope}.small_image',
                           '__disableTmpl' => [
                               'thumbnailUrl' => false,
                               'thumbnail' => false,
                               'smallImage' => false
                           ],
                       ],
                       'uploaderConfig' => [
                           'url' => $this->urlBuilder->getUrl(
                               'catalog/product_gallery/upload'
                           ),
                       ],
                       'dataScope' => 'image',
                   ],
                   [
                       'elementTmpl' => 'ui/dynamic-rows/cells/thumbnail',
                       'fit' => true,
                       'sortOrder' => 0
                   ]
               ),
               'name_container' => $this->getColumn(
                   'name',
                   __('Name'),
                   [],
                   ['dataScope' => 'product_link']
               ),
               'sku_container' => $this->getColumn(
                   'sku',
                   __('SKU'),
                   [
                       'validation' => [
                               'required-entry' => true,
                               'max_text_length' => Sku::SKU_MAX_LENGTH,
                           ],
                   ],
                   [
                       'elementTmpl' => 'Magento_ConfigurableProduct/components/cell-sku',
                   ]
               ),
               'price_container' => $this->getColumn(
                   'price',
                   __('Price'),
                   [
                       'imports' => [
                           'addbefore' => '${$.provider}:${$.parentScope}.price_currency',
                           '__disableTmpl' => ['addbefore' => false],
                       ],
                       'validation' => ['validate-zero-or-greater' => true]
                   ],
                   ['dataScope' => 'price_string']
               ),
               'quantity_container' => $this->getColumn(
                   'quantity',
                   __('Quantity'),
                   ['dataScope' => 'qty'],
                   ['dataScope' => 'qty']
               ),
               'price_weight' => $this->getColumn('weight', __('Weight')),
               'status' => [
                   'arguments' => [
                       'data' => [
                           'config' => [
                               'componentType' => 'text',
                               'component' => 'Magento_Ui/js/form/element/abstract',
                               'template' => 'Magento_ConfigurableProduct/components/cell-status',
                               'label' => __('Status'),
                               'dataScope' => 'status',
                           ],
                       ],
                   ],
               ],
               'manufacturer_country' => $this->getColumn('manufacturer_country', __('Manufacturer Country')), // here we added heading for manufacturer country
               'attributes' => [
                   'arguments' => [
                       'data' => [
                           'config' => [
                               'componentType' => Form\Field::NAME,
                               'formElement' => Form\Element\Input::NAME,
                               'component' => 'Magento_Ui/js/form/element/text',
                               'elementTmpl' => 'ui/dynamic-rows/cells/text',
                               'dataType' => Form\Element\DataType\Text::NAME,
                               'label' => __('Attributes'),
                           ],
                       ],
                   ],
               ],
               'actionsList' => [
                   'arguments' => [
                       'data' => [
                           'config' => [
                               'additionalClasses' => 'data-grid-actions-cell',
                               'componentType' => 'text',
                               'component' => 'Magento_Ui/js/form/element/abstract',
                               'template' => 'Magento_ConfigurableProduct/components/actions-list',
                               'label' => __('Actions'),
                               'fit' => true,
                               'dataScope' => 'status',
                           ],
                       ],
                   ],
               ],
           ],
       ],
   ];
}

Step-2

Magento\ConfigurableProduct\Ui\DataProvider\Product\Form\Modifier\Data\AssociatedProducts.php

Here we need to call the attribute value associated with the attribute code.

Find function prepareVariations() and call the product attribute value. Like :- ‘manufacturer_country’ => $this->escaper->escapeHtml($product->getManufacturerCountry()),

Working Example for AssociatedProducts.php

php

protected function prepareVariations()
{
   $variations = $this->getVariations();
   $productMatrix = [];
   $attributes = [];
   $productIds = [];
   if ($variations) {
       $usedProductAttributes = $this->getUsedAttributes();
       $productByUsedAttributes = $this->getAssociatedProducts();
       $currency = $this->localeCurrency->getCurrency($this->locator->getBaseCurrencyCode());
       $configurableAttributes = $this->getAttributes();
       foreach ($variations as $variation) {
           $attributeValues = [];
           foreach ($usedProductAttributes as $attribute) {
               $attributeValues[$attribute->getAttributeCode()] = $variation[$attribute->getId()]['value'];
           }
           $key = implode('-', $attributeValues);
           if (isset($productByUsedAttributes[$key])) {
               $product = $productByUsedAttributes[$key];
               $price = $product->getPrice();
               $variationOptions = [];
               foreach ($usedProductAttributes as $attribute) {
                   if (!isset($attributes[$attribute->getAttributeId()])) {
                       $attributes[$attribute->getAttributeId()] = [
                           'code' => $attribute->getAttributeCode(),
                           'label' => $attribute->getStoreLabel(),
                           'id' => $attribute->getAttributeId(),
                           'position' => $configurableAttributes[$attribute->getAttributeId()]['position'],
                           'chosen' => [],
                       ];
                       $options = $attribute->usesSource() ? $attribute->getSource()->getAllOptions() : [];
                       foreach ($options as $option) {
                           if (!empty($option['value'])) {
                               $attributes[$attribute->getAttributeId()]['options'][$option['value']] = [
                                   'attribute_code' => $attribute->getAttributeCode(),
                                   'attribute_label' => $attribute->getStoreLabel(0),
                                   'id' => $option['value'],
                                   'label' => $option['label'],
                                   'value' => $option['value'],
                               ];
                           }
                       }
                   }
                   $optionId = $variation[$attribute->getId()]['value'];
                   $variationOption = [
                       'attribute_code' => $attribute->getAttributeCode(),
                       'attribute_label' => $attribute->getStoreLabel(0),
                       'id' => $optionId,
                       'label' => $variation[$attribute->getId()]['label'],
                       'value' => $optionId,
                   ];
                   $variationOptions[] = $variationOption;
                   $attributes[$attribute->getAttributeId()]['chosen'][$optionId] = $variationOption;
               }

               $productMatrix[] = [
                   'id' => $product->getId(),
                   'product_link' => '<a href="' . $this->urlBuilder->getUrl(
                       'catalog/product/edit',
                       ['id' => $product->getId()]
                   ) . '" target="_blank">' . $this->escaper->escapeHtml($product->getName()) . '</a>',
                   'sku' => $product->getSku(),
                   'name' => $this->escaper->escapeHtml($product->getName()),
                   'manufacturer_country' => $this->escaper->escapeHtml($product->getManufacturerCountry()), // here we are calling attribute value
                   'qty' => $this->getProductStockQty($product),
                   'price' => $price,
                   'price_string' => $currency->toCurrency(sprintf("%f", $price)),
                   'price_currency' => $this->locator->getStore()->getBaseCurrency()->getCurrencySymbol(),
                   'configurable_attribute' => $this->getJsonConfigurableAttributes($variationOptions),
                   'weight' => $product->getWeight(),
                   'status' => $product->getStatus(),
                   'variationKey' => $this->getVariationKey($variationOptions),
                   'canEdit' => 0,
                   'newProduct' => 0,
                   'attributes' => $this->getTextAttributes($variationOptions),
                   'thumbnail_image' => $this->imageHelper->init($product, 'product_thumbnail_image')->getUrl(),
               ];
               $productIds[] = $product->getId();
           }
       }
   }

   $this->productMatrix = $productMatrix;
   $this->productIds = $productIds;
   $this->productAttributes = array_values($attributes);
}
 

Note: If the attribute type is dropdown or anything else, you must call it as per its syntax.

Contact us:+91 8128405131

Email send us at hello@elightwalk.com

FAQs about child product’s attribute value

Can this method be used for all types of attributes?

Why Is Displaying a Child Product Important?

What Are the Benefits of Displaying a Child Product's Attribute Value?

Pravin Prajapati
Full Stack Developer

Expert in frontend and backend development, combining creativity with sharp technical knowledge. Passionate about keeping up with industry trends, he implements cutting-edge technologies, showcasing strong problem-solving skills and attention to detail in crafting innovative solutions.

Most Visited Blog

Blog
The Ultimate Guide to Creating the Best Grid in Magento of 2022

Dive into the guide to creating an excellent Magento grid in 2022. Discover professional insights, strategies, and tactics for improving the grid design and functioning of an e-commerce platform for a better user experience.

Read More
Blog
How to Create Crud Operations in Grid?

This step-by-step approach lets you creating CRUD operations in a grid. Learn how to integrate Create, Read, Update, and Delete functionality into your application's grid, allowing you to manage and change data efficiently.

Read More
Blog
How to make curl request in Magento 2? A Beginners Guide

Unlock the power of Curl requests in Magento 2 with our comprehensive guide. Learn how to make seamless API calls and optimize your Magento 2 development. Elevate your coding skills today!

Read More