0

I have created extension attributes which i would like to use in my custom plugin.I want to call this plugin when after credit memo item is saved which happens when credit memo is saved(refund) But my plugin is not getting called( var_dump("hello");exit(); ) and I am not getting any errors or warning. I cleaned, cache, flushed compile and upgrade. It would be really helpful if someone can suggest something so that I can learn new concept and continue my learning. I have referred to lot of similar questions and tried to understand the flow and hence I implemented this which seems to be missing a step. app\code\Vendor\Sales\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\Sales\Api\Data\CreditmemoItemRepositoryInterface">
           <plugin name="creditmemoitemdetail_plugin" type="Vendor\Sales\Plugin\Order\Creditmemo\CreditmemoItemDetailPlugin" sortOrder="1" disabled="false"/>
       </type>
    </config>

app\code\Vendor\Sales\etc\extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Sales\Api\Data\CreditmemoItemInterface">
           <attribute code="creditmemoitemdetail" type="Vendor\Sales\Api\Data\CreditmemoItemDetailInterface">
               <join reference_table="sales_creditmemo_item_detail" reference_field="parent_id" join_on_field="entity_id">
                   <field column="entity_id">entity_id</field>
                   <field column="parent_id">parent_id</field>
                   <field column="item_number">item_number</field>
                   <field column="description">description</field>
                   <field column="sku">sku</field>
                   <field column="name">name</field>
               </join>
           </attribute>
       </extension_attributes>
    </config>

app\code\Vendor\Sales\Plugin\Order\Creditmemo\CreditmemoItemDetailPlugin.php

    <?php
        namespace Vendor\Sales\Plugin\Order\Creditmemo;
class CreditmemoItemDetailPlugin {
            public function afterSave(      
                \Magento\Sales\Api\CreditmemoItemRepositoryInterface $subject,  
                \Magento\Sales\Api\Data\CreditmemoItemInterface $resultOrder    
                ) {     
                $resultOrder = $this->saveTigrenAttribute($resultOrder);    

                return $resultOrder;    
                }       
                private function saveTigrenAttribute(\Magento\Sales\Api\Data\CreditmemoItemInterface $order)        
                {       
                    var_dump("hello");exit();
                $extensionAttributes = $order->getExtensionAttributes();    
                if (    
                    null !== $extensionAttributes &&    
                    null !== $extensionAttributes->getTigrenAttribute() 
                ) { 
                    $tigrenAttributeValue = $extensionAttributes->getTigrenAttribute()->getParentId();
                    $name = $extensionAttributes->getTigrenAttribute()->getName();
                    $sku = $extensionAttributes->getTigrenAttribute()->getSku();
                    $description = $extensionAttributes->getTigrenAttribute()->getDescription();
                    $item_number = $extensionAttributes->getTigrenAttribute()->getItemNumber();

                    try {   
                        // The actual implementation of the repository is omitted   
                        // but it is where you would save to the database (or any other persistent storage) 
                        $this->tigrenExampleRepository->save($order->getParentId(), $tigrenAttributeValue);
                        $this->tigrenExampleRepository->save($order->getName(), $name); 
                        $this->tigrenExampleRepository->save($order->getSku(), $sku);   
                        $this->tigrenExampleRepository->save($order->getOrderItemId(), $item_number);   
                        $this->tigrenExampleRepository->save($order->getDescription(), $description);   

                    } catch (\Exception $e) {   
                        throw new CouldNotSaveException(    
                            __('Could not add attribute to order: "%1"', $e->getMessage()), 
                            $e  
                        );  
                    }   
                }   
                return $order;  
                }
        }
0

Plugin type type name="Magento\Sales\Api\Data\CreditmemoItemRepositoryInterface" is wrong .

It should be Magento\Sales\Api\CreditmemoItemRepositoryInterface instead of Magento\Sales\Api\Data\CreditmemoItemRepositoryInterface.

di.xml code will be like

<?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\Sales\Api\CreditmemoItemRepositoryInterface">
       <plugin name="creditmemoitemdetail_plugin" type="Vendor\Sales\Plugin\Order\Creditmemo\CreditmemoItemDetailPlugin" sortOrder="1" disabled="false"/>
   </type>
</config>
  • yes there was mistake and changed di.xml still its not calling my plugin and I cleaned flushed compile and upgrade my creditmemo gets saved without entering that plugin also no errors or warnings thanks – J. Doe Apr 29 '20 at 12:32

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.