How to override this below file? Pepperjam\Network\Model\Beacon\Itemized.php
I need to override protected function inside this file, I tried with reference but not working
I have add override using plugin method but it is not working please advise if I am wrong:
In etc/di.xml add below code
<?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="Pepperjam\Network\Model\Beacon\Itemized">
<plugin name="cmarix-pepperjam-itemized-model" type="[VendorName][ModuleName]\Plugin\ItemizedPlugin" sortOrder="1" />
</type>
</config>
And In [VendorName][ModuleName]\Plugin\ItemizedPlugin.php file add below code
<?php
namespace [VendorName][ModuleName]\Plugin;
class ItemizedPlugin
{
protected function aftergetQuantity($item)
{
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/afterlog.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Your text message');
if ($item->getProduct()->canConfigure()) {
return 0;
} else {
return (int) $item->getQtyOrdered();
}
}
}
?>
-
try with <preference in your di.xmlArif Ahmad– Arif Ahmad2019年09月12日 12:32:51 +00:00Commented Sep 12, 2019 at 12:32
-
tried not workingArjun– Arjun2019年09月12日 13:12:37 +00:00Commented Sep 12, 2019 at 13:12
-
Where is this file coming from? Is it a magento module?Philipp Sander– Philipp Sander2019年09月12日 15:03:54 +00:00Commented Sep 12, 2019 at 15:03
-
Have you got the solution?aravind– aravind2019年09月13日 12:23:42 +00:00Commented Sep 13, 2019 at 12:23
-
You cannot override a protected/public function using Plugin or preference..Is this your custom module?aravind– aravind2019年09月13日 12:24:53 +00:00Commented Sep 13, 2019 at 12:24
1 Answer 1
You can easily override the file as this following steps
Step 1: you need to create di.xml file in your own Module. Path: [VendorName]\[ModuleName]\etc\
Step 2: Write this following code in your 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="Pepperjam\Network\Model\Beacon\Itemized" type="[VendorName]\[ModuleName]\Model\Beacon\Itemized"/>
</config>
-
I have already override using same way but it is not override I have created di.xml file in etc folder and added below code: <?xml version="1.0"?> <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Pepperjam\Network\Model\Beacon\Itemized" type="[VendorName][ModuleName]\Model\Beacon\Itemized" /> </config> After that I have created model file in below location: [VendorName][ModuleName]\Model\Beacon\Itemized.php And added function that I want to overrideArjun– Arjun2019年09月13日 07:49:13 +00:00Commented Sep 13, 2019 at 7:49
-
okay, now you can try plugin after to override the method.Oscprofessionals– Oscprofessionals2019年09月13日 08:07:00 +00:00Commented Sep 13, 2019 at 8:07
-
But I need to override protected method so it is not override using pluginArjun– Arjun2019年09月13日 08:46:50 +00:00Commented Sep 13, 2019 at 8:46
-
I have updated my question please chk & adviceArjun– Arjun2019年09月13日 12:12:47 +00:00Commented Sep 13, 2019 at 12:12
-
Can you share Itemized.php code here (Pepperjam\Network\Model\Beacon\Itemized).Oscprofessionals– Oscprofessionals2019年09月16日 08:50:37 +00:00Commented Sep 16, 2019 at 8:50