/   /   /  OroCommerce B2B for developer: Customize Automatic Order Cancellation

Note:

For more extensions and themes visit our store

OroCommerce B2B for developer: Customize Automatic Order Cancellation


You can set all orders with selected internal statuses to be automatically cancelled past their “Do Not Ship Later Than” date. By default OroCommerce cancel all orders which has not null ship Until field or the current date grant than this field . 
So How you can customize this atomization for example the order which created before 60 days and still open cancel it .
OroCommerce system us Processes (processes.yml) which provide the possibility to automate tasks related to entity management. They use the main doctrine events to perform described tasks at the right time. Each process can be performed immediately or after a timeout. Processes use the OroMessageQueue component and the bundle to provide the possibility of delayed execution. 

You can find  processes.yml responsible for auto cancellation inside path :
/vendor/oro/commerce/src/Oro/Bundle/OrderBundle/Resources/config/oro/processes.yml

  1.  
  2. processes:
  3.     definitions:
  4.         expire_orders:
  5.             label: 'Cancel old Orders'
  6.             enabled: true
  7.             entity: Oro\Bundle\OrderBundle\Entity\Order
  8.             preconditions:
  9.                 '@is_system_config_equal': ['oro_order.order_automation_enable_cancellation', true]
  10.             actions_configuration:
  11.                 - '@call_service_method':
  12.                     attribute: $.applicableStatuses
  13.                     service: oro_order.provider.configuration
  14.                     method: getApplicableInternalStatuses
  15.                 - '@call_service_method':
  16.                     attribute: $.targetStatusId
  17.                     service: oro_order.provider.configuration
  18.                     method: getTargetInternalStatus
  19.                 - '@tree':
  20.                     conditions:
  21.                         '@and':
  22.                             - '@not_empty': $.targetStatusId
  23.                             - '@not_empty': $.applicableStatuses
  24.                     actions:
  25.                         - '@request_enum_entity':
  26.                             enum_code: 'order_internal_status'
  27.                             attribute: $.targetStatus
  28.                             identifier: $.targetStatusId
  29.                         - '@create_datetime':
  30.                             attribute: $.date
  31.                             #should be start of the day, to avoid cancellation of orders with today's DNSL
  32.                             time: '00:00:00'
  33.                         - '@find_entities':
  34.                             class: Oro\Bundle\OrderBundle\Entity\Order
  35.                             attribute: $.orders
  36.                             where:
  37.                                 and:
  38.                                     - e.shipUntil IS NOT NULL
  39.                                     - e.shipUntil < :date
  40.                                     - NOT IDENTITY(e.internal_status) = :targetStatus
  41.                                     - IDENTITY(e.internal_status) IN (:statuses)
  42.                             query_parameters:
  43.                                 date: $.date
  44.                                 targetStatus: $.targetStatusId
  45.                                 statuses: $.applicableStatuses
  46.                         - '@foreach':
  47.                             array: $.orders
  48.                             value: $.order
  49.                             actions:
  50.                                 - '@assign_value': [$.order.internalStatus, $.targetStatus]
  51.     triggers:
  52.         expire_orders:
  53.             -
  54.                 cron: '10 * * * *'
  55.  

The id  definitions is  expire_orders the first conditions is testing if the feature is enabled from global configuration:

  1.  
  2.           preconditions:
  3.                 '@is_system_config_equal': ['oro_order.order_automation_enable_cancellation', true]
  4.  


you can change from:
To configure automatic order cancellation globally:

  •     Navigate to System > Configuration in the main menu.
  •     Select Commerce > Orders > Order Automation in the menu to the left.

The next is to find all status enabled and target cancel status :

  1.  
  2.             actions_configuration:
  3.                 - '@call_service_method':
  4.                     attribute: $.applicableStatuses
  5.                     service: oro_order.provider.configuration
  6.                     method: getApplicableInternalStatuses
  7.                 - '@call_service_method':
  8.                     attribute: $.targetStatusId
  9.                     service: oro_order.provider.configuration
  10.                     method: getTargetInternalStatus
  11.  

they will start the query to find all entities from filter:

  1.  
  2.                             where:
  3.                                 and:
  4.                                     - e.shipUntil IS NOT NULL
  5.                                     - e.shipUntil < :date
  6.                                     - NOT IDENTITY(e.internal_status) = :targetStatus
  7.                                     - IDENTITY(e.internal_status) IN (:statuses)
  8.                             query_parameters:
  9.                                 date: $.date
  10.                                 targetStatus: $.targetStatusId
  11.                                 statuses: $.applicableStatuses
  12.  

$date is affected before by time: '00:00:00' .
after that you will change all status of orders result to cancel status:

  1.  
  2.                         - '@foreach':
  3.                             array: $.orders
  4.                             value: $.order
  5.                             actions:
  6.                                 - '@assign_value': [$.order.internalStatus, $.targetStatus]
  7.  

you can add filter to query for example copy past processes.yml in your bundle MyCompany/Bundle/OverrideBundle/Resources/config/oro/processes.yml and fill with the content of default file and you can change to:

  1.  
  2.                         - '@find_entities':
  3.                             class: Oro\Bundle\OrderBundle\Entity\Order
  4.                             attribute: $.orders
  5.                             where:
  6.                                 and:
  7.                                     - e.shipUntil IS NOT NULL
  8.                                     - e.shipUntil < :date
  9.                                     - NOT IDENTITY(e.internal_status) = :targetStatus
  10.                                     - IDENTITY(e.internal_status) IN (:statuses)
  11.                                 or:
  12.                                     - e. createdAt > :dateBetween
  13.                                     - IDENTITY(e.internal_status) = :openStatus
  14.                                     - IDENTITY(e.internal_status) IN (:statuses)
  15.                             query_parameters:
  16.                                 date: $.date
  17.                                 targetStatus: $.targetStatusId
  18.                                 statuses: $.applicableStatuses
  19.                                 openStatus: ‘open’
  20.                                 dateBetween: ‘<dateBetween("-60 days", "now")>
  21.  

Here you have add an Or clause to cancel all orders which has internal status open and passed 60 days from day of creation of order . That all Thanks .
                                

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

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.