/   /   /  OroCommerce for Developers: Add Order Internal Statuses

Note:

For more extensions and themes visit our store

OroCommerce for Developers: Add Order Internal Statuses


An order can have the following internal statuses:

  •     Open — The order has been submitted and is being processed. You can cancel, mark as shipped, and close an open order.
  •     Cancelled — The order that has been submitted but successively canceled. There are many circumstances in which you may need to cancel the order. For example, the order has expired, or your organization cannot fulfill the order for any reasons, or the buyer asked you to do so. The submitted order may be canceled by an administrator or automatically when its ‘do not ship later than’ date is passed.

      You can close a canceled order.
Shipped — The order is on the way or delivered to the buyer. You can close a shipped order.

  •     Closed — The order does not require any further actions: has been successfully delivered, or canceled for some time, etc. You can archive a closed order
  •     Archived – An old order stored for historical purposes only. No further actions with the order are required (the buyer is not going to send a return request, etc.).

How to add Extra Order Statuses: 

In this tutorial I will add Approved internal status:

  •     Approved — The order has been opened. You can cancel, mark as shipped, and close an approved order.

To see the default order internal statuses you can go to ‘oro_enum_order_internal_status’ table in you db by phpmyadmin.

In this step you need to create an Interface ExtraStatusesProviderInterface.php in Provider folder in our bundle to add our internal status id ‘approved’.
you can add more internal statuses.

  1.  
  2. <?php
  3. namespace Ibnab\Bundle\ExtraOrderStatusesBundle\Provider;
  4. interface ExtraStatusesProviderInterface
  5. {
  6.     const INTERNAL_STATUS_APPROVED = 'approved';
  7.     //Add More internal statuses
  8. }
  9.  


Load internal status in migrations:

The first step you need to create a php class extends from AbstractEnumFixture

  •      AbstractEnumFixture is abstract class have two abstract functions getData() and getEnumCode()

getData() Returns an array of possible enum values, where array key is an id and array value is an English translation
getEnumCode() Returns an enum code of an extend entity (in our case it’s Order Entity):
Oro\Bundle\OrderBundle\Entity\Order.php and enm code is: const INTERNAL_STATUS_CODE = 'order_internal_status';

Ibnab\Bundle\ExtraOrderStatusesBundle\Migrations\Data\ORM\LoadInternalStatus.php

  1.  
  2. <?php
  3. namespace Ibnab\Bundle\ExtraOrderStatusesBundle\Migrations\Data\ORM;
  4. use Oro\Bundle\EntityExtendBundle\Migration\Fixture\AbstractEnumFixture;
  5. use Oro\Bundle\OrderBundle\Entity\Order;
  6. use Ibnab\Bundle\ExtraOrderStatusesBundle\Provider\ExtraStatusesProviderInterface;
  7. class LoadInternalStatus extends AbstractEnumFixture
  8. {
  9.     /** @var array */
  10.     protected static $data = [
  11.         ExtraStatusesProviderInterface::INTERNAL_STATUS_APPROVED => 'Approved'
  12.         //Add More internal statuses
  13.     ];
  14.     /**
  15.      * {@inheritdoc}
  16.      */
  17.     protected function getData()
  18.     {
  19.         return self::$data;
  20.     }
  21.     /**
  22.      * Returns array of data keys
  23.      *
  24.      * @return array
  25.      */
  26.      public static function getDataKeys()
  27.     {
  28.         return array_keys(self::$data);
  29.     }
  30.    /**
  31.      * {@inheritdoc}
  32.      */
  33.    protected function getEnumCode()
  34.     {
  35.         return Order::INTERNAL_STATUS_CODE;
  36.     }
  37. }
  38.  

The second step add new php class UpdateOrderStatuses.php to get dependent class and load the new Internal Status

Ibnab\Bundle\ExtraOrderStatusesBundle\Migrations\Data\ORM\UpdateOrderStatuses.php

  1.  
  2. <?php
  3. namespace Ibnab\Bundle\ExtraOrderStatusesBundle\Migrations\Data\ORM;
  4. use Doctrine\Common\DataFixtures\AbstractFixture;
  5. use Doctrine\Common\DataFixtures\DependentFixtureInterface;
  6. use Doctrine\Common\Persistence\ObjectManager;
  7. use Doctrine\ORM\EntityManager;
  8. use Ibnab\Bundle\ExtraOrderStatusesBundle\Provider\ExtraStatusesProviderInterface;
  9. class UpdateOrderStatuses extends AbstractFixture implements DependentFixtureInterface
  10. {
  11.    /**
  12.      * {@inheritdoc}
  13.      */
  14.     public function getDependencies()
  15.     {
  16.         return [LoadInternalStatus::class];
  17.     }
  18.     /**
  19.      * @var EntityManager $manager
  20.      * {@inheritdoc}
  21.      */
  22.     public function load(ObjectManager $manager)
  23.     {
  24.         $qb = $manager->getConnection()->createQueryBuilder();
  25.         $qb->update('oro_order')
  26.             ->set('internal_status_id', ':status')
  27.             ->setParameter('status', ExtraStatusesProviderInterface::INTERNAL_STATUS_APPROVED)
  28.             ->where($qb->expr()->isNull('internal_status_id'))
  29.             ->execute();
  30.     }
  31. }
  32.  

Add Approved operation in Order View in the Back-Office:

 Ibnab\Bundle\ExtraOrderStatusesBundle\Resources\config\oro\actions.yml

  1.  
  2. // Add Approved operation just for opened Order
  3. operations:
  4.     ibnab_order_approved:
  5.         label: ibnab.order.action.order.approved
  6.         enabled: true
  7.         applications: [default]
  8.         routes:
  9.             - oro_order_view
  10.         order: -1
  11.         button_options:
  12.             icon: fa fa-check-circle-o
  13.         preconditions:
  14.             '@equal': [$internalStatus.id, 'open']
  15.         actions:
  16.             - '@request_enum_entity':
  17.                 enum_code: 'order_internal_status'
  18.                 attribute: $internalStatus
  19.                 identifier: 'approved'
  20.             - '@flush_entity': $.data
  21.             - '@flash_message':
  22.                 message: ibnab.order.action.message.order.approved.success
  23.                 type: 'info'
  24.                 message_parameters:
  25.                     id: $identifier
  26.     // Add Cancel operation for approved Order
  27.     ibnab_order_cancel:
  28.         label: ibnab.order.action.order.cancel
  29.         enabled: true
  30.         applications: [default]
  31.         routes:
  32.             - oro_order_view
  33.         order: 1
  34.         button_options:
  35.             icon: fa-close
  36.         preconditions:
  37.             '@equal': [$internalStatus.id, 'approved']
  38.         actions:
  39.             - '@request_enum_entity':
  40.                 enum_code: 'order_internal_status'
  41.                 attribute: $internalStatus
  42.                 identifier: 'cancelled'
  43.             - '@flush_entity': $.data
  44.             - '@flash_message':
  45.                 message: ibnab.order.action.message.order.cancel.success
  46.                 type: 'info'
  47.                 message_parameters:
  48.                     id: $identifier
  49.     // Add Close operation for approved Order                
  50.     ibnab_order_close:
  51.         label: ibnab.order.action.order.close
  52.         enabled: true
  53.         applications: [default]
  54.         routes:
  55.             - oro_order_view
  56.         order: 2
  57.         button_options:
  58.             icon: fa-window-close-o
  59.         preconditions:
  60.             '@equal': [$internalStatus.id, 'approved']
  61.         actions:
  62.             - '@request_enum_entity':
  63.                 enum_code: 'order_internal_status'
  64.                 attribute: $internalStatus
  65.                 identifier: 'closed'
  66.             - '@flush_entity': $.data
  67.             - '@flash_message':
  68.                 message: ibnab.order.action.order.close.success
  69.                 type: 'info'
  70.                 message_parameters:
  71.                     id: $identifier
  72.     // Add Mark as shipped operation for approved Order
  73.     ibnab_order_mark_as_shipped:
  74.         label: ibnab.order.action.order.mark_as_shipped
  75.         enabled: true
  76.         applications: [default]
  77.         routes:
  78.             - oro_order_view
  79.         order: 3
  80.         button_options:
  81.             icon: fa-check
  82.         preconditions:
  83.             '@equal': [$internalStatus.id, 'approved']
  84.         actions:
  85.             - '@request_enum_entity':
  86.                 enum_code: 'order_internal_status'
  87.                 attribute: $internalStatus
  88.                 identifier: 'shipped'
  89.             - '@flush_entity': $.data
  90.             - '@flash_message':
  91.                 message: ibnab.order.action.order.mark_as_shipped.success
  92.                 type: 'info'
  93.                 message_parameters:
  94.                     id: $identifier
  95.  

Is all Done

Comments

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.