5

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 ?

  • please add screenshot to get clear idea of your issue. – Rakesh Jesadiya Dec 20 '16 at 7:18
  • I updated my question – Niraj Patel Dec 20 '16 at 7:29
  • please share your defaultconfigprovider.php file code – Rakesh Jesadiya Dec 21 '16 at 8:58
  • Have you done with plugin method? – Rakesh Jesadiya Dec 21 '16 at 10:43
  • @Rakesh I have uninstall magento 2.1 and reinstallling magento then step all thing then let you know and thanks for interest .I will let you know after all setup – Niraj Patel Dec 21 '16 at 10:57
1
+50

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;
    }
}
  • I tried with Plugin but did not work in old instance but I will check by this way – Niraj Patel Dec 22 '16 at 5:39
  • I have done successfully with above code. – Rakesh Jesadiya Dec 22 '16 at 5:41
  • Oh nice.I am excited I will sure check your code.thanks – Niraj Patel Dec 22 '16 at 5:43
  • isbn is product attribute so Should I add isbn column in sales_quote_item table ? – Niraj Patel Dec 22 '16 at 5:46
  • Yes first you have to add attribute to sales_quote_item table – Rakesh Jesadiya Dec 22 '16 at 5:49
0

Product information is in $output['totalsData']

vendor/magento/module-checkout/Model/DefaultConfigProvider.php

public function getConfig()
{ 
    ......

    $output['totalsData'] = $this->getTotalsData();
    ......
}

enter image description here

You should read more here: https://magento.stackexchange.com/a/133732/33057

  • I have done same thing but it woks in shipping step but not working in payment step – Niraj Patel Dec 11 '16 at 6:40
  • If I put die function in gettotalsdata function so die function called in shipping step but not work in payment step – Niraj Patel Dec 11 '16 at 6:42
  • In the payment step, what you want to do? – Khoa TruongDinh Dec 11 '16 at 6:51
  • I want to add product attribute called 'isbn' 'isbn' number display works perfectly in shipping step but not display in payment step – Niraj Patel Dec 11 '16 at 6:54
  • I have added product 'isbn' attribute value in totals items array it works fine shipping but payment step not display in payment step – Niraj Patel Dec 11 '16 at 7:00

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.