0

I am trying to display a product attribute value

I have found the template where it is displayed:

vendor/magento/module-checkout/view/frontend/web/template/summary/item/details.html

How can I get the value of an attribute to display?

<div class="product-item-details">

    <div class="product-item-inner">
        <div class="product-item-name-block">
            <strong class="product-item-name" data-bind="text: $parent.name"></strong>
            <div class="details-qty">
                <span class="label"><!-- ko i18n: 'Qty' --><!-- /ko --></span>

                <span class="value" data-bind="text: $parent.qty"></span>
            </div>
        </div>
        <!-- ko foreach: getRegion('after_details') -->
            <!-- ko template: getTemplate() --><!-- /ko -->
        <!-- /ko -->
    </div>

    <!-- ko if: (JSON.parse($parent.options).length > 0)-->
    <div class="product options" data-bind="mageInit: {'collapsible':{'openedState': 'active'}}">
        <span data-role="title" class="toggle"><!-- ko i18n: 'View Details' --><!-- /ko --></span>
        <div data-role="content" class="content">
            <strong class="subtitle"><!-- ko i18n: 'Options Details' --><!-- /ko --></strong>
            <dl class="item-options">
                <!--ko foreach: JSON.parse($parent.options)-->
                <dt class="label" data-bind="text: label"></dt>
                    <!-- ko if: ($data.full_view)-->
                    <dd class="values" data-bind="html: full_view"></dd>
                    <!-- /ko -->
                    <!-- ko ifnot: ($data.full_view)-->
                    <dd class="values" data-bind="html: value"></dd>
                    <!-- /ko -->
                <!-- /ko -->
            </dl>
        </div>
    </div>
    <!-- /ko -->
</div>
1

You can set your custom attribute inside checkout page summary block using below way,

app/code/{Package}/{Module}/etc/di.xml

 <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">    
    <!-- Set Custom attribute in chekcout page summary block -->
    <type name="Magento\Checkout\Model\DefaultConfigProvider">
        <plugin name="default_config_provider" type="Package\Module\Model\DefaultConfigProvider"/>
    </type>
</config>

app/code/{Package}/{Module}/Model/DefaultConfigProvider.php file

<?php
namespace Package\Module\Model;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Quote\Api\CartItemRepositoryInterface as QuoteItemRepository;

class DefaultConfigProvider
{
    private $checkoutSession;
    private $quoteItemRepository;
    protected $scopeConfig;

    public function __construct(
        CheckoutSession $checkoutSession,
        QuoteItemRepository $quoteItemRepository,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->_scopeConfig = $scopeConfig;
        $this->checkoutSession = $checkoutSession;
        $this->quoteItemRepository = $quoteItemRepository;
    }

    public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, $result)
    {        
        $quoteId = $this->checkoutSession->getQuote()->getId();
        if ($quoteId) {            
            $itemOptionCount = count($result['totalsData']['items']);
            $quoteItems = $this->quoteItemRepository->getList($quoteId);

            $isbnOptions = array();
            foreach ($quoteItems as $index => $quoteItem) {
                $quoteItemId = $quoteItem['item_id'];
                $isbnOptions[$quoteItemId] = $quoteItem['isbn'];               
            }

            for($i=0;$i<$itemOptionCount;$i++){
                $quoteParentId = $result['totalsData']['items'][$i]['item_id'];                    
                $currentOption = array();
                $currentOption = json_decode($result['totalsData']['items'][$i]['options'],true);

                $newOption = $isbnOptions[$quoteParentId];

                //for set new option inside current option tab
                if(count($newOption) > 0 && is_array($newOption)){                        
                    foreach($newOption as $key=>$value){
                        if( !is_array($value) ){
                            $currentOption[] = array('value'=>$value,'label'=>$key);
                        }
                    }
                    $result['totalsData']['items'][$i]['options'] = json_encode($currentOption);
                }                    
            }
        }
        return $result;
    }
}
1

Yeah, We can set the custom attribute in order summary in checkout.

Create a custom Extension

Step 1: Declare plugin app/code/{Package}/{Module}/etc/di.xml

     <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">    
    <!-- Set Custom attribute in chekcout page summary block -->
    <type name="Magento\Checkout\Model\DefaultConfigProvider">
        <plugin name="default_config_provider" type="Package\Module\Model\DefaultConfigProvider"/>
    </type>
</config>

Step 2 : Declare new file

app/code/{Package}/{Module}/Model/DefaultConfigProvider.php file

Create a plugin to set custom value in checkoutConfig Json

public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, $result)
{
    $quoteId = $this->_checkoutSession->getQuote()->getId();

    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    if ($quoteId) {
        $itemOptionCount = count($result['quoteItemData']);
        $quoteItems = $this->_quoteItemRepository->getList($quoteId);
        //$logger->info(print_R($itemOptionCount,1));

        $isbnOptions = [];
        foreach ($quoteItems as $index => $quoteItem) {
            $quoteItemId = $quoteItem['item_id'];
            $isbnOptions[$quoteItemId] = $quoteItem['isbn'];
        }

        for ($i=0; $i < $itemOptionCount; $i++) {
            $quoteParentId = $result['totalsData']['items'][$i]['item_id'];
            $currentOption = [];
            $isNew = null;
            $result['quoteItemData'][$i]['new'] = 'newvv';
            $productId = $result['quoteItemData'][$i]['product']['entity_id'];
            $productObj = $this->_productRepository->load($productId);
            $isNew = $productObj->getCustomAttribute();
            $result['quoteItemData'][$i]['new'] = $isNew;
            json_encode($currentOption);
        }
    }
    return $result;
}

Step 3 : Add custom atribute in JS file Put vendor file in theme Json

vendor/magento/module-checkout/view/frontend/web/js/view/summary/item/details.js

define(
[
    'uiComponent'
],
function (Component) {
    "use strict";
    var quoteItemData = window.checkoutConfig.quoteItemData;
    return Component.extend({
        defaults: {
            template: 'Magento_Checkout/summary/item/details'
        },
        quoteItemData: quoteItemData,
        getValue: function(quoteItem) {
            return quoteItem.name;
        },
        getItemTag: function(quoteItem) {
            var itemProduct = this.getItemProduct(quoteItem.item_id);
            return (itemProduct.new == true) ? 'new!' : null;
        },
        getItemProduct: function(item_id) {
            var itemElement = null;
            _.each(this.quoteItemData, function(element, index) {
                if (element.item_id == item_id) {
                    itemElement = element;
                }
            });
            return itemElement;
        }

    });
}

);

Step 4 : Copy below code in Html file to show in order summary vendor/magento/module-checkout/view/frontend/web/template/summary/item/details.html

<span class="product-item-new" data-bind="text: getItemTag($parent)"></span>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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