2

Hi I am looking to fetch the product category name in the order summary of checkout page. Which file do I need to edit to fetch the data

1 Answer 1

2

You can do using create plugin on Magento\Checkout\Model\DefaultConfigProvider

Create a plugin on method GetConfig and modify the result

Create di.xml at app/code/{Vendorname}/{Modulename}/etc

Source Code

<?xml version="1.0"?>
<!--
/**
 * @category   Stackexchange
 * @package    Stackexchange
 * @author     Amit Bera<[email protected]>
 * @website    http://www.amitbera.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Model\DefaultConfigProvider">
        <plugin name="add_category_ids_to_checkout"
                type="{Vendorname}\{Modulename}\Plugin\Checkout\Model\DefaultConfigProviderPlugin" />
    </type>
</config>

Plugin file DefaultConfigProviderPlugin.php at app/code/{Vendorname}/{Modulename}/Plugin/Checkout/Model/DefaultConfigProviderPlugin.php

<?php
/**
 * @category   Stackexchange
 * @package    Stackexchange
 * @author     Amit Bera<[email protected]>
 * @website    http://www.amitbera.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

namespace {Vendorname}\{Modulename}\Plugin\Checkout\Model;


class DefaultConfigProviderPlugin
{
    /**
     * @var \Magento\Catalog\Model\ProductCategoryList
     */
    private $productCategoryList;
    /**
     * @var \Magento\Catalog\Api\CategoryRepositoryInterface
     */
    private $categoryRepository;

    public function __construct(
      \Magento\Catalog\Model\ProductCategoryList $productCategoryList,
      \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
    )
    {
        $this->productCategoryList = $productCategoryList;
        $this->categoryRepository = $categoryRepository;
    }

    public function afterGetConfig(
        \Magento\Checkout\Model\DefaultConfigProvider $subject,
        array $result
    )
    {
        if (isset($result['quoteItemData']) && isset($result['totalsData']['items'])) {

            $tempsQuoteItems = [];
            $quoteItems = $result['quoteItemData'];

            foreach ($quoteItems as $index => $quoteItem) {

                $categroyName = $this->getQuoteItemCategoryName($quoteItem['product_id']);
                $tempsQuoteItems[$quoteItem['item_id']] = $categroyName;
                $name = $result['quoteItemData'][$index]['name'];
                $result['quoteItemData'][$index]['name'] = $name . $categroyName;

            }
            $items = $result['totalsData']['items'];
            foreach ($items as $index => $item) {
                $name = $result['totalsData']['items'][$index]['name'];
                $categroyName = '';
                if (array_key_exists($item['item_id'],$tempsQuoteItems))
                {
                    $categroyName = $tempsQuoteItems[$item['item_id']];
                }
                $result['totalsData']['items'][$index]['name'] = $name.'-'.$categroyName;

            }
        }

        return $result;
    }
    private function getQuoteItemCategoryName($productId)
    {
        $categoryIds = $this->productCategoryList->getCategoryIds($productId);
        if(!empty($categoryIds)){
            try{
                $category = $this->categoryRepository->get($categoryIds[0]);
                return $category->getName();
            }catch (\Magento\Framework\Exception\NoSuchEntityException $exception)
            {
                return '';
            }

        }
        return '';
    }
}
7
  • Can you please provide me any example Commented Dec 12, 2018 at 11:49
  • Will given you Full answer
    – Amit Bera
    Commented Dec 12, 2018 at 12:27
  • how to make js to get affect after the pages is load for checkout page Commented Dec 13, 2018 at 4:35
  • Sorry, don't understand the scenario
    – Amit Bera
    Commented Dec 13, 2018 at 7:24
  • I mean add custom js to the checkout page after the page is completely loaded Commented Dec 13, 2018 at 8:34

Your Answer

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

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