Question

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

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top