Question

How to add one more state after order status and state processing. I want to add one custom state like delivery after order status and state processing in magento 2 Before order get completed (ship). accordingly i want to sent email "Out for delivery"

Was it helpful?

Solution

To create a new order status follow the below step :

  1. Create a Custom Order Status
  2. Assign an order status to a state
  3. Custom an existing order status
  4. Remove an order status from the current state

On the Admin Panel, Stores > Settings > Order Status.

In the upper-right corner, click on Create New Status.

To check detail description check this How to Create Custom Order Status in Magento 2

If you want to create order status in programmatically then go for below way.

Create a file in your custom module like

<?php
namespace Test\OrderFlow\Setup;

use Exception;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Status;
use Magento\Sales\Model\Order\StatusFactory;
use Magento\Sales\Model\ResourceModel\Order\Status as StatusResource;
use Magento\Sales\Model\ResourceModel\Order\StatusFactory as StatusResourceFactory;


class InstallData implements InstallDataInterface
{
    const ORDER_STATUS_PROCESSING_FULFILLMENT_CODE = 'processing_custom';

    const ORDER_STATUS_PROCESSING_FULFILLMENT_LABEL = 'Processing Custom';

    const ORDER_STATE_CUSTOM_CODE = 'some_custom_state';
    const ORDER_STATUS_CUSTOM_CODE = 'some_custom_status';

    const ORDER_STATUS_CUSTOM_LABEL = 'Some Custom Status';

    protected $statusFactory;
    protected $statusResourceFactory;

    public function __construct(
        StatusFactory $statusFactory,
        StatusResourceFactory $statusResourceFactory
    ) {
        $this->statusFactory = $statusFactory;
        $this->statusResourceFactory = $statusResourceFactory;
    }


    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->addNewOrderProcessingStatus();
        $this->addNewOrderStateAndStatus();
    }


    protected function addNewOrderProcessingStatus()
    {
        /** @var StatusResource $statusResource */
        $statusResource = $this->statusResourceFactory->create();
        /** @var Status $status */
        $status = $this->statusFactory->create();
        $status->setData([
            'status' => self::ORDER_STATUS_PROCESSING_FULFILLMENT_CODE,
            'label' => self::ORDER_STATUS_PROCESSING_FULFILLMENT_LABEL,
        ]);

        try {
            $statusResource->save($status);
        } catch (AlreadyExistsException $exception) {

            return;
        }

        $status->assignState(Order::STATE_PROCESSING, false, true);
    }


    protected function addNewOrderStateAndStatus()
    {
        /** @var StatusResource $statusResource */
        $statusResource = $this->statusResourceFactory->create();
        /** @var Status $status */
        $status = $this->statusFactory->create();
        $status->setData([
            'status' => self::ORDER_STATUS_CUSTOM_CODE,
            'label' => self::ORDER_STATUS_CUSTOM_LABEL,
        ]);

        try {
            $statusResource->save($status);
        } catch (AlreadyExistsException $exception) {

            return;
        }

        $status->assignState(self::ORDER_STATE_CUSTOM_CODE, true, true);
    }
}

Run the Setup Upgrade command in order to activate the module and execute the setup script:

php bin/magento setup:upgrade

Now you can check the results in Stores -> Settings -> Order Status.

I hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top