0

I am using magento2.3.x

I have created a dropdown attribute(product attribute) from admin.

now I have showing this attribute on product page using below code:-

file path:- app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_product_view.xml

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.upc_code" template="Magento_Catalog::product/view/attribute.phtml" after="product.info.sku">
     <arguments>
        <argument name="at_call" xsi:type="string">getUpcCode</argument>
        <argument name="at_code" xsi:type="string">upc_code</argument>
        <argument name="css_class" xsi:type="string">upc_code</argument>
        <argument name="at_label" xsi:type="string">default</argument>
        <argument name="at_type" xsi:type="string">text</argument>
        <argument name="add_attribute" xsi:type="string">itemprop="upc_code"</argument>
     </arguments>
</block>

Now I have created one more multiselect attribute(product attribute) from admin.

I am using the same code above for this attribute to show on product detail page but it showing value of the select option instead of label of the selected options.

Please let me know how to show label of the selected option in multiselect attribute.

1 Answer 1

1

You need to update code like below for multiselect attribute.

For xml file please add like below.

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.upc_code2" template="Magento_Catalog::product/view/attribute.phtml" after="product.info.upc_code">
     <arguments>
        <argument name="at_call" xsi:type="string">getUpcMulti</argument>
        <argument name="at_code" xsi:type="string">upc_multi</argument>
        <argument name="css_class" xsi:type="string">upc_multi</argument>
        <argument name="at_label" xsi:type="string">default</argument>
        <argument name="at_type" xsi:type="string">multiselect</argument>
        <argument name="add_attribute" xsi:type="string">itemprop="upc_multi"</argument>
     </arguments>
</block>

You need to override attribute.phtml file in your theme path. Path will be like app/design/frontend/{{YOUR THEME NAME HERE}}/{{YOUR THEME PACKAGE NAME HERE}}/Magento_Catalog/templates/product/view/attribute.phtml

Multi select attributes values are saved comma separated in database. So we need to get it's option labels programmatically like below.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis

/**
 * Product view template
 *
 * @var $block \Magento\Catalog\Block\Product\View\Description
 */
?>
<?php
$_helper = $this->helper(Magento\Catalog\Helper\Output::class);
$_product = $block->getProduct();

if (!$_product instanceof \Magento\Catalog\Model\Product) {
    return;
}

$_call = $block->getAtCall();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();

$renderLabel = true;
// if defined as 'none' in layout, do not render
if ($_attributeLabel == 'none') {
    $renderLabel = false;
}

if ($_attributeLabel && $_attributeLabel == 'default') {
    $_attributeLabel = $_product->getResource()->getAttribute($_code)->getStoreLabel();
}
if ($_attributeType && $_attributeType == 'text') {
    $_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code))
        ? $_product->getAttributeText($_code)
        : '';
} if ($_attributeType && $_attributeType == 'multiselect') {
    $_attributeValue = '';
    $explodeAttributes = $_helper->productAttribute($_product, $_product->$_call(), $_code);
    $explodeAttributes = explode(',', $explodeAttributes);
    $isAttributeExist = $_product->getResource()->getAttribute($_code);

    if ($isAttributeExist && $isAttributeExist->usesSource()) {
        foreach ($explodeAttributes as $key => $optionId) {
            $_attributeValue .= $isAttributeExist->getSource()->getOptionText($optionId). ',';
        }
    }
    $_attributeValue = trim($_attributeValue, ',');
} else {
    $_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code);
}
?>

<?php if ($_attributeValue) :?>
<div class="product attribute <?= $block->escapeHtmlAttr($_className) ?>">
    <?php if ($renderLabel) :?>
        <strong class="type"><?= $block->escapeHtml($_attributeLabel) ?></strong>
    <?php endif; ?>
    <div class="value" <?= /* @noEscape */ $_attributeAddAttribute ?>><?= /* @noEscape */ $_attributeValue ?></div>
</div>
<?php endif; ?>

Please do changes in code as per you want to show multi select option labels.

Cheers!

14
  • thanks for your answer but, when I using your code, it showing multi-select attribute properly but for other attribute it show their code not their label. Sep 1, 2021 at 10:15
  • For which attribute type? I have shown you only for multi select attribute type
    – ZealousWeb
    Sep 1, 2021 at 10:16
  • other yes/no type. dropdown type and input text type etc... Sep 1, 2021 at 10:18
  • This is your different issue. I have provided solution that how you shown selected option label instead of value
    – ZealousWeb
    Sep 1, 2021 at 10:24
  • And it's a default magento code , i have just shown you how you need to do for multi select attribute
    – ZealousWeb
    Sep 1, 2021 at 10:25

Your Answer

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

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