I am getting error on creating an shipment
TypeError: Argument 2 passed to
MagentoSalesModelOrderValidationShipOrder::MagentoSalesModelOrderValidation
{closure}() must be an instance of MagentoSalesApiDataShipmentItemCreationInterface,
instance of MagentoSalesModelOrderShipmentItem given in /vendor/magento/module-
sales/Model/Order/Validation/ShipOrder.php:127
Here is how I am creating the shipment
<?php
namespace NavienCustomModel;
class ShipmentManager
{
protected $_objectManager;
protected $orderInterface;
/**
* @var MagentoSalesModelOrderShipmentTrackFactory
*/
protected $trackingFactory;
/**
* @var MagentoSalesApiDataShipmentCommentCreationInterface
*/
protected $commentInterface;
/**
* @var MagentoSalesModelShipOrder
*/
protected $shipOrderService;
/**
* @var MagentoSalesModelConvertOrder
*/
protected $orderConverter;
/**
* ShipmentManager constructor.
*/
public function __construct()
{
//this is instance of my custom class to get instance object manager.
$this->_objectManager = MagentoFrameworkAppObjectManager::getInstance();
//order interface
$this->orderInterface = $this->_objectManager->get(
MagentoSalesApiDataOrderInterface::class
);
//instance of tracking factory to add tracking number in shipment
$this->trackingFactory = $this->_objectManager->get(
MagentoSalesModelOrderShipmentTrackFactory::class
);
//Comment interface to add comment on the shipment.
$this->commentInterface = $this->_objectManager->get(
MagentoSalesApiDataShipmentCommentCreationInterface::class
);
//Shipment order service to create shipment
$this->shipOrderService = $this->_objectManager->get(
MagentoSalesModelShipOrder::class
);
//Order converter to create shipment items
$this->orderConverter = $this->_objectManager->get(
MagentoSalesModelConvertOrder::class);
}
/**
* Prepares tracking data form tracking number.
*
* @param $trackingNumber
*
* @return MagentoSalesModelOrderShipmentTrack
*/
protected function setTrackingData($shippingPartner, $trackingNumber)
{
$track = $this->trackingFactory->create();
$track->setTrackNumber($trackingNumber);
//Carrier code can not be null/empty. Default carrier code is used
$track->setCarrierCode('custom'); //Put your carrier code here
$track->setTitle($shippingPartner); //add your title here
$trackInfo[] = $track;
return $trackInfo;
}
/**
* @param string $comment
*
* @return MagentoSalesApiDataShipmentCommentCreationInterface
*/
protected function setShipmentComment($comment)
{
//comment can not be empty
$comment = !empty($comment) ? $comment : 'Not Available';
return $this->commentInterface->setComment($comment);
}
/**
* {@inheritdoc}
*/
public function createShipment($orderId, array $items, $shippingPartner = "", $trackingNumber = '', $comment = null, $notify = false, $includeComment = false )
{
$order = $this->orderInterface->load($orderId);
// $order = $this->orderInterface->loadByIncrementId('000000050');
if ($order->canShip()) {
try {
$orderId = $order->getId();
$tracks = $this->setTrackingData($shippingPartner, $trackingNumber);
$comment = $this->setShipmentComment($comment);
$shippedItems = $this->createShipmentItems($items, $order);
//creates shipment
$shipmentId = $this->shipOrderService->execute($orderId,
$shippedItems,
$notify,
$includeComment,
$comment,
$tracks);
} catch (Exception $e) {
echo $e->getMessage();
return false;
}
return $shipmentId;
}
return null;
}
/**
* Create shipment items required to create shipment.
*
* @param array $items
* @param MagentoSalesModelOrder $order
*
* @return array
*/
protected function createShipmentItems(array $items, $order)
{
$shipmentItem = [];
foreach ($order->getAllItems() as $orderItem) {
if (array_key_exists($orderItem->getId(), $items)) {
$shipmentItem[] = $this->orderConverter
->itemToShipmentItem($orderItem)
->setQty($items[$orderItem->getId()]);
}
}
return $shipmentItem;
}
}
Some users reported that the class was working on Magento 2.3 but not working on Magento 2.4 . I am using Magento 2.4.2. Can someone help me to find the fix.