0

I have create custom attribute("current_seller_id") in sales_invoice_item and now want to save some value in this column how can i do this. Actually i already save the value in sale_order_item table with same name attribute("current_seller_id"). i was use around plugin for that. code is

<?php
namespace MyVendor\MyModuleName\Plugin;
class QuoteToOrder
{
    public function aroundConvert(
        \Magento\Quote\Model\Quote\Item\ToOrderItem $subject,
        \Closure $proceed,
        \Magento\Quote\Model\Quote\Item\AbstractItem $item,
        $additional = []
    ) {
        /** @var $orderItem \Magento\Sales\Model\Order\Item */
        $orderItem = $proceed($item, $additional);
        $orderItem->setCurrentSellerId($item->getCurrentSellerId());
        return $orderItem;
    }
}

these lines of code save value in each record of sale_order_item table. you see this $item->getCurrentSellerId() i make attribute "current_seller_id" in eav_attribute table and save value of current user with each product saving. I need the same event or plugin that fire before saving invoce items in sales_invoice_item table because i need to send some custom value in table column

1

I would suggest to create a plugin after Magento\Sales\Model\Order\Invoice\Item::register() and copy there the data from the order item to the newly created corresponding invoice item. This could be the plugin method:

public function afterRegister(\Magento\Sales\Model\Order\Invoice\Item $subject, $result)
{
    $result->setCurrentSellerId($result->getOrderItem()->getCurrentSellerId());
    return $result;
}

However you can try to add the current_seller_id to the fieldset which is used for a newly created invoice item to copy values from an order item. But I heard that this doesn't work in some Magento versions.

For this approach add the following into a fieldset.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
    <scope id="global">
        <fieldset id="sales_convert_order_item">
            <field name="current_seller_id">
                <aspect name="to_invoice_item" />
            </field>
        </fieldset>
    </scope>
<config>
  • HelgeB ! after plugin or before ? i want to save value in database. – HaFiz Umer Mar 18 at 11:24
  • 1
    after is better, before would probably also work. The saving process is however later. The register method is only called when a new invoice is created and therefore it is the best place for you. – HelgeB Mar 18 at 11:28
  • let me check please :) – HaFiz Umer Mar 18 at 11:29
  • Dear HelgeB! It work perfect i use afterRegister plugin. Thank You . – HaFiz Umer Mar 18 at 11:44
  • HelgeB ! ? Dear any event fire before invoice save in sales_invoice table ? Just like event sales_model_service_quote_submit_before that fired before save order in sales_order and i save values in custom attribute ("current_seller_id") ? – HaFiz Umer Mar 18 at 13:28

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.