Skip to content

Instantly share code, notes, and snippets.

  • Select an option

  • Save coresh/c22901a89fc5f66ed3f952936f3eb2c6 to your computer and use it in GitHub Desktop.

Select an option

Save coresh/c22901a89fc5f66ed3f952936f3eb2c6 to your computer and use it in GitHub Desktop.

Revisions

  1. @tegansnyder tegansnyder created this gist Mar 29, 2021.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,137 @@
    Create a file called `abstract.php` and place it in your root Magento folder:

    ##### abstract.php
    ```php
    <?php
    use \Magento\Framework\AppInterface as AppInterface;
    use \Magento\Framework\App\Http as Http;

    use Magento\Framework\ObjectManager\ConfigLoaderInterface;
    use Magento\Framework\App\Request\Http as RequestHttp;
    use Magento\Framework\App\Response\Http as ResponseHttp;
    use Magento\Framework\Event;
    use Magento\Framework\Filesystem;
    use Magento\Framework\App\AreaList as AreaList;
    use Magento\Framework\App\State as State;

    abstract class AbstractApp implements AppInterface
    {
    public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager,
    Event\Manager $eventManager,
    AreaList $areaList,
    RequestHttp $request,
    ResponseHttp $response,
    ConfigLoaderInterface $configLoader,
    State $state,
    Filesystem $filesystem,
    \Magento\Framework\Registry $registry
    ) {
    $this->_objectManager = $objectManager;
    $this->_eventManager = $eventManager;
    $this->_areaList = $areaList;
    $this->_request = $request;
    $this->_response = $response;
    $this->_configLoader = $configLoader;
    $this->_state = $state;
    $this->_filesystem = $filesystem;
    $this->registry = $registry;
    }

    public function launch()
    {
    $this->run();
    return $this->_response;
    }

    abstract public function run();

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
    return false;
    }
    }
    ```

    Then create a file called `manualShip.php` and place it in your root Magento folder.

    ##### manualShip.php
    ```php
    <?php
    require dirname(__FILE__) . '/app/bootstrap.php';
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    require dirname(__FILE__) . '/abstract.php';

    class Getapp extends AbstractApp
    {

    public function run()
    {

    // Your order ID here
    $orderIncrementId = '000000019';

    $this->_objectManager->get('Magento\Framework\Registry')
    ->register('isSecureArea', true);

    $orders = $this->_objectManager->create('\Magento\Sales\Model\Order');

    $order = $orders->loadByIncrementId($orderIncrementId);


    // Initialize the order shipment object
    $convertOrder = $this->_objectManager->create('Magento\Sales\Model\Convert\Order');
    $shipment = $convertOrder->toShipment($order);


    // Loop through order items
    foreach ($order->getAllItems() AS $orderItem) {
    // Check if order item has qty to ship or is virtual
    if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
    continue;
    }

    $qtyShipped = $orderItem->getQtyToShip();

    // Create shipment item with qty
    $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);

    // Add shipment item to shipment
    $shipment->addItem($shipmentItem);

    }

    $track = $this->_objectManager->create('\Magento\Sales\Model\Order\Shipment\TrackFactory')->create();
    $track->setNumber('1111111111');
    $track->setCarrierCode('fedex');
    $track->setTitle('Shipment Tracking Number');
    $track->setDescription("Description");
    $track->setUrl('http://www.fedex.com/whatesfds');
    $shipment->addTrack($track);

    $shipment->register();
    $shipment->getOrder()->setIsInProcess(true);

    // Save created shipment and order
    $shipment->save();
    $shipment->getOrder()->save();

    $this->_objectManager->create('Magento\Shipping\Model\ShipmentNotifier')
    ->notify($shipment);

    $shipment->save();

    }
    }



    /** @var \Magento\Framework\App\Http $app */
    $app = $bootstrap->createApplication('Getapp');
    $bootstrap->run($app);
    ```

    Then you can run `manualShip.php` on the command line from your Magento root folder like:
    ```sh
    php manualShip.php
    ```