First it needs to said that you cant apply any filter on getItemsCollection()
in Order model because the collection is already loaded in that method and
after loading filters can't be applied anymore to a collection.
But that shouldn't bother you. You can just iterate through the collection and remove non matching items. For example like this:
$filteredItems = $order->getItemsCollection();
foreach ($filteredItems as $key => $orderItem){
if ($orderItem->getCurrentSellerId() != 27){
$filteredItems->removeItemByKey($key);
}
}
$order->setItems($filteredItems->getItems());
You could apply a code like above somewhere appropriate, for example before Magento\Sales\Model\Service\InvoiceService::prepareInvoice()
is called.
That's the method which creates new invoices. If you want to use that, you can change the method code below with that and you won't see non matching items.
But you with that approach the totals calculation process may have some difficulties since it's not a good idea to manipulate the order collection in the invoice creation process.
I would suggest you a plugin before the mentioned method, but I would strongly recommend not to manipulate the order item collection but to work with invoiced quantities.
That would be exactly the same procedute as you would set quantities into the invoice form and klick UpdateQty's
, just automatically based on your current_seller_id
.
You will see all items in the form, but all non matching have qty 0. That would work as follows:
di.xml entry:
<type name="Magento\Sales\Model\Service\InvoiceService">
<plugin name="Module_InvoiceService" type="Vendor\Module\Plugin\InvoiceService" sortOrder="10" disabled="false" />
</type>
Vendor/Module/Plugin/InvoiceService.php
namespace Vendor\Module\Plugin;
class InvoiceService{
public function beforePrepareInvoice(
\Magento\Sales\Model\Service\InvoiceService $subject,
\Magento\Sales\Model\Order $order,
array $qtys = []
)
{
foreach ($order->getItemsCollection() as $key => $orderItem){
if ($orderItem->getCurrentSellerId() == 27){
$qtys[$orderItem->getId()] = $orderItem->getQtyOrdered() - $orderItem->getQtyInvoiced();
} else {
$qtys[$orderItem->getId()] = 0;
}
}
return [$order, $qtys];
}
I hope that helps you understand the context and may be a solution for your requirement.
Invoice
model. That collection comes fromsales_order_invoice_item
. If your code would be in theOrder
model it would work. Do you want to filter the invoice item collection?