5

I want to override app/code/Magento/Checkout/CustomerData/DefaultItem.php file. Becuase i have to change in this file. So how can i override this file

7

registration.php file,

filepath app/code/Package/Modulename/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Package_Modulename',
    __DIR__
);

module.xml file,

filepath app/code/Package/Modulename/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Package_Modulename" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

di.xml file, filepath app/code/Package/Modulename/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">
     <preference for="Magento\Checkout\CustomerData\DefaultItem"
                 type="Package\Modulename\CustomerData\DefaultItem" />
</config>

DefaultItem.php file, file path, app/code/Package/Modulename/CustomerData/DefaultItem.php

<?php

namespace Package\Modulename\CustomerData;

class DefaultItem extends \Magento\Checkout\CustomerData\DefaultItem
{   
        //override function code here.....
}

you can change function code inside file function.

3

I have override one of file as below,

app/code/Namespace/Checkout/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">

 <type name="Magento\Checkout\CustomerData\Cart">
        <plugin name="checkout.cart.message" type="Namespace\Checkout\CustomerData\CartPlugin" sortOrder="1"/>
    </type>
</config>

And then I have added customer data file in below path,

app/code/Namespace/Checkout/CustomerData/CartPlugin.php

In which my class is as below,

class CartPlugin extends \Magento\Checkout\CustomerData\Cart implements SectionSourceInterface

and its method is as below,

 public function aftergetSectionData()
    {.. my code goes here}

Same thing you can do with defaultItem file as well.

0

Use around or after plugin modify that file.

<type name="Magento\Checkout\CustomerData\DefaultItem">
        <plugin name="starbucksb2b_add_attribute" type="NameSpace\Checkout\Plugin\DefaultItem" disabled="false" sortOrder="10"/>
</type>

And since this is the only public function getItemData() (extends from AbstractItem) , create plugin for that and get the array, then reassign value using new function, eg create new function getProductUrl() in this file.

NameSpace/Checkout/Plugin/DefaultItem

<?php
namespace NameSpace\Checkout\Plugin;

use Magento\Quote\Model\Quote\Item;

class DefaultItem
{
    const GROUP = 'grouped';

    public function aroundGetItemData($subject, \Closure $proceed, Item $item)
    {
        $data = $proceed($item);
        $product = $item->getProduct();

        if ($item->getProductType() == self::GROUP) {
            $data['product_url'] = $this->getUrl();
        }

        $atts = [
            'product_size' => $product->getAttributeText('st_size')
        ];

        return array_merge($data, $atts);
    }
    protect function getUrl(){
        return "test";
    }

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.