php 如何在Magento 2中以编程方式创建货件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 如何在Magento 2中以编程方式创建货件相关的知识,希望对你有一定的参考价值。

<?php

class CreateShipment extends \Magento\Backend\App\Action
{
    /**
     * The OrderRepository is used to load, save and delete orders.
     *
     * @var \Magento\Sales\Model\OrderRepository
     */
    protected $orderRepository;

    /**
     * The ShipmentFactory is used to create a new Shipment.
     *
     * @var Order\ShipmentFactory
     */
    protected $shipmentFactory;

    /**
     * The ShipmentRepository is used to load, save and delete shipments.
     *
     * @var Order\ShipmentRepository
     */
    protected $shipmentRepository;

    /**
     * The ShipmentNotifier class is used to send a notification email to the customer.
     *
     * @var ShipmentNotifier
     */
    protected $shipmentNotifier;

    /**
     * All specified parameters in the constructor will be injected via dependency injection.
     *
     * @param \Magento\Backend\App\Action\Context $context
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Sales\Model\OrderRepository $orderRepository,
        \Magento\Sales\Model\Order\ShipmentRepository $shipmentRepository,
        \Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory,
        \Magento\Shipping\Model\ShipmentNotifier $shipmentNotifier,
    ) {
        parent::__construct($context);

        $this->orderRepository = $orderRepository;
        $this->shipmentFactory = $shipmentFactory;
        $this->shipmentRepository = $shipmentRepository;
        $this->shipmentNotifier = $shipmentNotifier;
    }

    /**
     * Since our class extends the Magento Action class it *must* have an execute() function.
     * This function is automatically called as soon as the request is sent.
     * @return void
     */
    public function execute()
    {
        // we are assuming that the order ID passed to this action via the request parameter.
        $orderId = $this->getRequest()->getParam('order_id', 0);

        if ($orderId <= 0) {
            echo 'Order not found.';
            return;
        }

        try {
            // load order from database
            $order = $this->orderRepository->get($orderId);

            if ($order == null) {
                echo "Order not loaded from database."
                return;
            }

            $this->createShipment($order);

        } catch (\Exception $exception) {
            echo 'Error: ' . $exception->getMessage();
        }
    }

    /**
     * Creates a new shipment for the specified order.
     *
     * @param \Magento\Sales\Model\Order $order
     */
    protected function createShipment($order)
    {
        // check if it's possible to ship the items
        if ($order->canShip()) {
            // create the shipment
            $shipment = $this->shipmentFactory->create($order);

            // save the newly created shipment
            $this->shipmentRepository->save($shipment);

            // send shipping confirmation e-mail to customer
            $this->shipmentNotifier->notify($shipment);
        }
    }
}

以上是关于php 如何在Magento 2中以编程方式创建货件的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 php 代码在 WHM/Cpanel 中以编程方式创建数据库?

请问 Magento某产品库存无货如何在前台继续显示?

如何在 JSF 2 中以编程方式或动态创建复合组件

php 在drupal 8中以编程方式创建词汇和术语: - Drupal

如何在 PHP 中以编程方式验证 Tether (TRC20) 钱包地址?

Magento 2:自定义属性不起作用