Magento 2 – Checkout Page Order Summary Section Data in Payment Step

checkoutmagento-2.1onestepcheckout

Checkout page it displays order summary section.It has Product data displaying .Product infromation coming from Magento\Checkout\Model\DefaultConfigProvider.php and from getTotalsData() function if it is in shipping step and when I switch in payment step order summary section display product information but it is not coming from getTotals() in payment section.

If I change code from $output['totalsData'] = $this->getTotalsData() to $output['totalsData'] = array() in getConfig() function so result is order summary product section not display in checkout shipping step but it displays in payment step

I have added following code in file: pub\static\frontend\Magento\luma\en_US\Magento_Checkout\template\summary\item

<pre data-bind="text: ko.toJSON($parent, null, 2)"></pre>

And display following result in checkout page:-
I have changed code in getTotalsData() function in Magento\Checkout\Model\DefaultConfigProvider.php path and add isbn attribute in totalsdata object but not diplay in payment step but it display in shipping step

Shipping Step 1: http://i.stack.imgur.com/hFV0t.jpg
This above shipping step screenshot where it display totals information and also display my custom product attribute 'isbn'.

Payment Step 1: http://i.stack.imgur.com/GlIe7.jpg
This above shipping step screenshot where it display totals information and not display my custom product attribute 'isbn'.

I have changed in core file DefaultconfigProvider.php temporary to check

And changed only this getTotalsData() function

private function getTotalsData()
    {
        /** @var \Magento\Quote\Api\Data\TotalsInterface $totals */
        $totals = $this->cartTotalRepository->get($this->checkoutSession->getQuote()->getId());
        $items = [];
        /** @var  \Magento\Quote\Model\Cart\Totals\Item $item */
        foreach ($totals->getItems() as $item) {
            $data = $item->__toArray(); // custom code
            $data['isbn'] = 'ISBN-12345'; // custom code
            $items[] = $data;
        }
        $totalSegmentsData = [];
        /** @var \Magento\Quote\Model\Cart\TotalSegment $totalSegment */
        foreach ($totals->getTotalSegments() as $totalSegment) {
            $totalSegmentArray = $totalSegment->toArray();
            if (is_object($totalSegment->getExtensionAttributes())) {
                $totalSegmentArray['extension_attributes'] = $totalSegment->getExtensionAttributes()->__toArray();
            }
            $totalSegmentsData[] = $totalSegmentArray;
        }
        $totals->setItems($items);
        $totals->setTotalSegments($totalSegmentsData);
        $totalsArray = $totals->toArray();
        if (is_object($totals->getExtensionAttributes())) {
            $totalsArray['extension_attributes'] = $totals->getExtensionAttributes()->__toArray();
        }
        return $totalsArray;
    }

And this is my details.html file which I have modified

<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->

<!-- ko foreach: getRegion('before_details') -->
    <!-- ko template: getTemplate() --><!-- /ko -->
<!-- /ko -->
<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 class="details-isbn">
                <span class="label"><!-- ko i18n: 'Isbn' --><!-- /ko --></span>
                <span class="value" data-bind="text: $parent.isbn"></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>

In payment step,product information where is coming from ?

Best Answer

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;
    }
}
Related Topic