I'm struggling trying to ship an order just after the order is created.
In the event sales_order_place_after
I'm executing the following code:
use \Magento\Sales\Model\Order\Shipment\TrackFactory;
use \Magento\Sales\Api\Data\ShipmentCommentCreationInterface;
use \Magento\Sales\Model\ShipOrder;
use \Magento\Sales\Model\Convert\Order;
use Transom\Pakke\Logger\Logger;
use \Magento\Sales\Api\Data\OrderInterface;
/**
* Custom shipping model
*/
class CreateShipment
{
protected $trackingFactory;
protected $commentInterface;
protected $shipOrderService;
protected $orderConverter;
protected $logger;
protected $orderInterface;
public function __construct(
TrackFactory $trackingFactory,
ShipmentCommentCreationInterface $commentInterface,
ShipOrder $shipOrderService,
Order $orderConverter,
Logger $logger,
OrderInterface $orderInterface
) {
$this->trackingFactory = $trackingFactory;
$this->commentInterface = $commentInterface;
$this->shipOrderService = $shipOrderService;
$this->orderConverter = $orderConverter;
$this->logger = $logger;
$this->orderInterface = $orderInterface;
}
protected function setTrackingData($trackingNumber)
{
$track = $this->trackingFactory->create();
$track->setTrackNumber($trackingNumber);
//Carrier code can not be null/empty. Default carrier code is used
$track->setCarrierCode('Test');//Put your carrier code here
$track->setTitle('TEST TITLE');//add your title here
$trackInfo[] = $track;
return $trackInfo;
}
protected function setShipmentComment($comment)
{
//comment can not be empty
$comment = !empty($comment) ? $comment : 'Not Available';
return $this->commentInterface->setComment($comment);
}
public function createShipment($order)
{
$this->logger->info("INSIDE CREATE SHIPMENT");
//$this->logger->info("Order ID: ".$orderIncrementId);
// $order = $this->orderInterface->loadByIncrementId($orderIncrementId);
$trackingNumber = "12345";
$comment = "Test comment";
$notify = true;
$includeComment =false;
// if ($order->canShip()) {
$this->logger->info("INSIDE A ");
try {
// $orderId = $order->getId();
$orderId = $order->getIncrementId();
$this->logger->info("INSIDE B ");
$tracks = $this->setTrackingData($trackingNumber);
$this->logger->info("INSIDE C ");
// $comment = $this->setShipmentComment($comment);
$this->logger->info("INSIDE D ");
$shippedItems = $this->createShipmentItems($order);
$this->logger->info("INSIDE E ");
//creates shipment
$shipmentId = $this->shipOrderService->execute($orderId,
$shippedItems,
$notify,
$includeComment,
$tracks);
$this->logger->info("INSIDE C ");
} catch (\Exception $e) {
// echo $e->getMessage();
$this->logger->info($e->getMessage());
}
$this->logger->info("END OF SHIPMENT ");
return $shipmentId;
// }
return null;
}
protected function createShipmentItems($order)
{
$shipmentItem = [];
foreach ($order->getAllItems() as $orderItem) {
if (array_key_exists($orderItem->getId())) {
$shipmentItem[] = $this->orderConverter
->itemToShipmentItem($orderItem)
->setQty($items[$orderItem->getId()]);
}
}
return $shipmentItem;
}
}
I'm not getting any error, it just does not let me to place the order, there are no errors in exception.log, system.log or debug.log, and my custom log is not writing anything inside the try
.
Could it be because this event is not proper for shipping the order?
Please help. Thanks!