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:
- { name: kernel.event_listener, event: extendable_action.finish_checkout, method: onCreateOrder, priority: 10 }
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 :
ibnab_eventtest.event_listener.create_order_listener: class: Ibnab\Bundle\EventTestBundle\EventListener\CreateOrderEventListener tags: - { name: kernel.event_listener, event: extendable_action.finish_checkout, method: onCreateOrder, priority: 10 }
is simple service and the class is Ibnab\Bundle\EventTestBundle\EventListener\CreateOrderEventListener ith content :
<?php namespace Ibnab\Bundle\WhoBoughtBundle\EventListener; use Oro\Component\Action\Event\ExtendableActionEvent; use Oro\Bundle\WorkflowBundle\Entity\WorkflowItem; use Oro\Bundle\WorkflowBundle\Model\WorkflowData; use Oro\Bundle\OrderBundle\Entity\Order; class CreateOrderEventListener { public function __construct() { } /** * @param ExtendableActionEvent $event */ public function onCreateOrder(ExtendableActionEvent $event) { if (!$this->isCorrectOrderContext($event->getContext())) { return; } $orderId = $event->getContext()->getEntity()->getId(); } } /** * @param mixed $context * @return bool */ protected function isCorrectOrderContext($context) { return ($context instanceof WorkflowItem && $context->getData() instanceof WorkflowData && $context->getData()->has('order') && $context->getData()->get('order') instanceof Order ); } }
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:
- { name: kernel.event_listener, event: extendable_condition.pre_order_create, method: onBeforeOrderCreate, priority: 10 }
add the method onBeforeOrderCreate and make in change to your order line items for example:
/** * @param ExtendableConditionEvent $event * @throws InventoryLevelNotFoundException */ public function onBeforeOrderCreate(ExtendableConditionEvent $event) { // your code here }
don’t forget to add use Oro\Component\Action\Event\ExtendableConditionEvent; to your class, that is all for this tutorial .
Comments