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

asked Sep 12, 2019 at 12:26
6
  • try with <preference in your di.xml Commented Sep 12, 2019 at 12:32
  • tried not working Commented Sep 12, 2019 at 13:12
  • Where is this file coming from? Is it a magento module? Commented Sep 12, 2019 at 15:03
  • Have you got the solution? Commented Sep 13, 2019 at 12:23
  • You cannot override a protected/public function using Plugin or preference..Is this your custom module? Commented 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>
answered Sep 12, 2019 at 14:16
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 Commented Sep 13, 2019 at 7:49
  • okay, now you can try plugin after to override the method. Commented Sep 13, 2019 at 8:07
  • But I need to override protected method so it is not override using plugin Commented Sep 13, 2019 at 8:46
  • I have updated my question please chk & advice Commented Sep 13, 2019 at 12:12
  • Can you share Itemized.php code here (Pepperjam\Network\Model\Beacon\Itemized). Commented Sep 16, 2019 at 8:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.