1

I want to save value in sales_invoice table of custom attribute ("current_seller_id"). i want to know any event that fire before save the invoice in sales_invoice table or use plugin of which method. . I already done like this functionality in sales_order table via using event sales_model_service_quote_submit_before code is give below .

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getData('order');
    $quote = $observer->getData('quote');
    $quoteItems = $quote->getItems();
    foreach ($quoteItems as $quoteItem){
        if ($quoteItem->getCurrentSellerId() != ""){
            $items[]=$quoteItem->getCurrentSellerId();
        }
    }
    $seller_ids=implode(',',$items);
    $order->setCurrentSellerId($seller_ids);
    return $this;
}

Same i want to do with sales_invoice table. Can anyone guide me ?

1 Answer 1

1

If you want to be in sync with your code you already have implemented in Observer or plugin use before the invoice creation you should use for this also a plugin method after Magento\Sales\Model\Order\Invoice::register(). This is the method wich calls also the register method for each item.

So if you create the plugin method after Magento\Sales\Model\Order\Invoice::register() you will have all the current seller IDs of the invoice items and can access them there.

The plugin function could be something like this:

public function afterRegister(\Magento\Sales\Model\Order\Invoice $subject, $result)
{
    $items = [];
    foreach ($result->getAllItems() as $item) {
        $items[]=$item->getCurrentSellerId();
    }
    $seller_ids=implode(',',$items);
    $result->setCurrentSellerId($seller_ids);

    return $result;
}

It is almost the same action point as using the sales_order_invoice_register event but the recommended way to change data is to use plugins.

5
  • HelgeB ! i try this solution. in this plugin use $result->getAllItems(). Dear i use plugin of this method(getAllItems()) . public function afterGetAllItems(\Magento\Sales\Model\Order\Invoice $subject,$items) generate error in the method define line. Error is ` Uncaught ArgumentCountError: Too few arguments to function Codility\VendorOrder\Plugin\Item::beforeRegister(), 1 passed in /var/www/html/codility-magento-market-place/vendor/magento/framework/Interception/Interceptor.php on line 121 and exactly 2 expected `
    – HaFiz Umer
    Commented Mar 18, 2019 at 15:02
  • I don't know hat happens in your code, the error you have posted is about the beforeRegister() method in the item not in a afterRegister method for the invoice as proposed in the answer. Also I wouldn't suggest to use afterGetAllItems() but afterRegister as posted in my answer.
    – HelgeB
    Commented Mar 18, 2019 at 15:08
  • HelgeB ! it work fine . Thank you Dear. Actually i was use beforeRegister() plugin . i just use now afterRegister it work fine. in my last question you own say you can use before or after plugin thats why i replace after with before and get error. I don't why can you explain why we can't use beforeRegister plugin ? Thanks
    – HaFiz Umer
    Commented Mar 18, 2019 at 15:35
  • In the case of the invoice you need to add your code after register, because the register calls for each items are fired within the invoice register method. Therefore before the invoice register method your items do not have the current seller id. Just look at the code of Magento\Sales\Model\Order\Invoice::register() and you will understand why you need the after plugin in this case.
    – HelgeB
    Commented Mar 18, 2019 at 15:45
  • yes got it ! understand thank you
    – HaFiz Umer
    Commented Mar 18, 2019 at 16:22

Your Answer

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

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