0

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();
        }
    }
}
?>

http://www.w3.org/2001/XMLSchema-instance

6
  • try with <preference in your di.xml
    – Arif Ahmad
    Sep 12, 2019 at 12:32
  • tried not working
    – Arjun
    Sep 12, 2019 at 13:12
  • Where is this file coming from? Is it a magento module? Sep 12, 2019 at 15:03
  • Have you got the solution?
    – aravind
    Sep 13, 2019 at 12:23
  • You cannot override a protected/public function using Plugin or preference..Is this your custom module?
    – aravind
    Sep 13, 2019 at 12:24

1 Answer 1

0

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>
5
  • 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 override
    – Arjun
    Sep 13, 2019 at 7:49
  • okay, now you can try plugin after to override the method. Sep 13, 2019 at 8:07
  • But I need to override protected method so it is not override using plugin
    – Arjun
    Sep 13, 2019 at 8:46
  • I have updated my question please chk & advice
    – Arjun
    Sep 13, 2019 at 12:12
  • Can you share Itemized.php code here (Pepperjam\Network\Model\Beacon\Itemized). Sep 16, 2019 at 8:50

Your Answer

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

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