Question

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

Was it helpful?

Solution

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<dev.amitbera@gmail.com>
 * @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<dev.amitbera@gmail.com>
 * @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 '';
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top