How to override customerData folder's file in magento 2?
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.
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.