M2.3CreateOrderShipment
<?php
namespace DotsquaresImexportModel;
class Shipstation extends MagentoFrameworkmodelAbstractModel
{
/**
* @var MagentoSalesModelOrderShipmentTrackFactory
*/
protected $_shipmentTrackFactory;
/**
* @var MagentoSalesModelOrderShipmentFactory
*/
protected $_shipmentFactory;
/**
* @var MagentoFrameworkDBTransactionFactory
*/
protected $_transactionFactory;
/**
* @param MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory
* @param MagentoSalesModelOrderShipmentFactory $shipmentFactory
* @param MagentoFrameworkDBTransactionFactory $transactionFactory
*/
function" title="public function">public function __construct(
MagentoSalesModelOrderShipmentTrackFactory $shipmentTrackFactory,
MagentoSalesModelOrderShipmentFactory $shipmentFactory,
MagentoFrameworkDBTransactionFactory $transactionFactory,
MagentoSalesApiOrderRepositoryInterface $orderRepository
) {
$this->_shipmentTrackFactory = $shipmentTrackFactory;
$this->_shipmentFactory = $shipmentFactory;
$this->_transactionFactory = $transactionFactory;
}
/**
*开始处理订单发货信息
* param $orderId:订单ID
* param $tranking:发货号码
* boole
*/
public function insertShipInfo(&$objectManager,$orderId,$tranking,$shipDate)
{
try {
// Load the order
$order = $objectManager->create("MagentoSalesModelOrder")->loadByAttribute("increment_id", $orderId);
if ( !$order->getId() ) {
return ["status" => 0, "info" => "Order Not Exist."];
}
//create order shipment
$result = $this->createShipment($order, $tranking, $shipDate);
if ($result) {
return ["status" => 1, "info" => "Success."];
}
return ["status" => 0, "info" => "CreateShipment Fail."];
} catch (Exception $e) {
return ["status" => 0, "info" => "Exception:".$e->getMessage()];
}
}
/**
* @param $order
* @param string $trackingNumber
* @return MagentoSalesModelShipment $shipment
*/
protected function createShipment($order, $trackingNumber, $shipDate)
{
try {
if ($order){
$data = array(array(
"carrier_code" => "usps",
"title" => "United States Postal Service",
"number" => $trackingNumber,
));
$shipment = $this->prepareShipment($order, $data);
if ($shipment) {
$order->setIsInProcess(true);
$comment = "Package shipped on ".$shipDate." via USPS First Class Mail. Tracking No ".$trackingNumber;
$order->addStatusHistoryComment($comment, false);
$transactionSave = $this->_transactionFactory->create()->addObject($shipment)->addObject($shipment->getOrder());
$transactionSave->save();
}
return true;
}
return false;
} catch (Exception $e) {
return false;
}
}
/**
* @param $order MagentoSalesModelOrder
* @param $track array
* @return $this
*/
protected function prepareShipment($order, $track)
{
$shipment = $this->_shipmentFactory->create(
$order,
$this->prepareShipmentItems($order),
$track
);
return $shipment->getTotalQty() ? $shipment->register() : false;
}
/**
* @param $order MagentoSalesModelOrder
* @return array
*/
protected function prepareShipmentItems($order)
{
$items = [];
foreach($order->getAllItems() as $item) {
$items[$item->getItemId()] = $item->getQtyOrdered();
}
return $items;
}
}
以上是 M2.3CreateOrderShipment 的全部内容, 来源链接: utcz.com/z/516795.html