Magento 2 – Override Third Party Module Function with Plugin

after-pluginmagento2plugin

I am using third party module for print pdf and now I want to customize the one function with using plugin, currently I am using after plugin and able to print the result of original function in my custom module.

Problem is in original method there is one of foreach loop and final result is based on that loop so how is possible to change the result as per foreach and if else condition.

below is Original Method code:

public function getOptions()
    {
        $order = $this->getOrder();
        $splitButtonOptions = [];

        $templateCollection = $this->collectionFactory
            ->create()
            ->addFieldToFilter('template_type', ['neq' => TemplateType::TYPE_PRODUCT])
            ->addFieldToFilter('template_type', ['neq' => TemplateType::TYPE_SECONDARY_ATTACHMENT])
            ->addFieldToFilter('is_active', ['eq' => TemplateActive::STATUS_ENABLED]);

        $templateCollection->addStoreFilter($order->getStoreId());

        

            there are some more condition but I need to update below code in my override function

           

            foreach ($templateCollection as $template) {
                if ($templateType !== $template->getTemplateType()) {
                    continue;
                }

                $label =  $templateTypeName . ': ' . $template->getTemplateName();
                if ($templateCount === 1) {
                    $label = $templateTypeName;
                }
                $templateId = $template->getTemplateId();


               if($label=="some value") 
               {
                $splitButtonOptions[] = [some value assignment];
              }


              else{

                $splitButtonOptions[] = [somevalue assignment]
                  
                ];

              }
            }
        }

My Plugin have below code

public function afterGetOptions(\Vendorname\Modulename\Block\Adminhtml\Sales\Order\PrintPdf $subject,$templateCollection)
    {
        echo "<pre/>";
        print_r($templateCollection);

       die;
     //I want here check some conditions and then return the final updated array.
        //return $splitButtonOptions;
    }

Is there any way to change login with foreach loop in my after plugin.

Best Answer

According to your requirement you have the following options.

  1. Use Around Plugin to modify that things.
  2. Use Preference Concept to override the class and modify that one.
Related Topic