/   /   /  OroCommece for developer: Observe Place order with event listener

Note:

For more extensions and themes visit our store

OroCommece for developer: Observe Place order with event listener


The default checkout system inside OroCommerce based on workflow ,So in backend from workflow you have ability to enable  multi steps or one page checkout .

In this tutorial we will show how you can observe the place order which means action finish checkout in wrokflow , So the normal case with symfony is going to services.yml – or any other yaml responsible of DI – and create a service tagged with custom event listener in our case is:

  1.  
  2. - { name: kernel.event_listener, event: extendable_action.finish_checkout, method: onCreateOrder, priority: 10 }
  3.  

the event  extendable_action.finish_checkout is predefined inside orocommerce to observe finish checkout action – and the method responsible  onCreateOrder we should add to our class service the full code service is :

  1.  
  2.     ibnab_eventtest.event_listener.create_order_listener:
  3.         class: Ibnab\Bundle\EventTestBundle\EventListener\CreateOrderEventListener
  4.         tags:
  5. - { name: kernel.event_listener, event: extendable_action.finish_checkout, method: onCreateOrder, priority: 10 }
  6.  

is simple service and the class is Ibnab\Bundle\EventTestBundle\EventListener\CreateOrderEventListener ith content :

  1.  
  2. <?php
  3. namespace Ibnab\Bundle\WhoBoughtBundle\EventListener;
  4. use Oro\Component\Action\Event\ExtendableActionEvent;
  5. use Oro\Bundle\WorkflowBundle\Entity\WorkflowItem;
  6. use Oro\Bundle\WorkflowBundle\Model\WorkflowData;
  7. use Oro\Bundle\OrderBundle\Entity\Order;
  8. class CreateOrderEventListener
  9. {
  10.     public function __construct() {
  11.     }
  12.     /**
  13.      * @param ExtendableActionEvent $event
  14.      */
  15.     public function onCreateOrder(ExtendableActionEvent $event)
  16.     {
  17.         if (!$this->isCorrectOrderContext($event->getContext())) {
  18.             return;
  19.         }
  20.         $orderId = $event->getContext()->getEntity()->getId();
  21.         }       
  22.     }
  23.     /**
  24.      * @param mixed $context
  25.      * @return bool
  26.      */
  27.     protected function isCorrectOrderContext($context)
  28.     {
  29.         return ($context instanceof WorkflowItem
  30.             && $context->getData() instanceof WorkflowData
  31.             && $context->getData()->has('order')
  32.             && $context->getData()->get('order') instanceof Order
  33.         );
  34.     }
  35. }
  36.  


So we have test if is correct order context which has data contain order instance , and inside  onCreateOrder you can write you custom new function for example you can get the order instance $order = $event->getContext()->getEntity() ; 
and make any change on create order .

Other useful event is before create order So OroCommerce give ability to add:

  1.  
  2. - { name: kernel.event_listener, event: extendable_condition.pre_order_create, method: onBeforeOrderCreate, priority: 10 }
  3.  

add the method  onBeforeOrderCreate and make in change to your order line items for example:

  1.  
  2.     /**
  3.      * @param ExtendableConditionEvent $event
  4.      * @throws InventoryLevelNotFoundException
  5.      */
  6.     public function onBeforeOrderCreate(ExtendableConditionEvent $event)
  7.     {
  8.        // your code here
  9.     }
  10.  

don’t forget to add use Oro\Component\Action\Event\ExtendableConditionEvent; to your class,  that is all for this tutorial .

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.