Question

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 ?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top