Skip to content

Instantly share code, notes, and snippets.

@Elendev
Created April 16, 2019 08:35
Show Gist options
  • Select an option

  • Save Elendev/ba5aa84ce7cef8daf0f71ac4901959de to your computer and use it in GitHub Desktop.

Select an option

Save Elendev/ba5aa84ce7cef8daf0f71ac4901959de to your computer and use it in GitHub Desktop.
Sylius - add an On Hold state
# file config/packages/_sylius.yaml
winzou_state_machine:
sylius_order:
states:
on_hold: ~
transitions:
hold:
from: [new]
to: on_hold
unhold:
from: [on_hold]
to: new
unhold_fulfill:
from: [on_hold]
to: fulfilled
callbacks:
guard:
guard_on_unhold:
on: unhold
do: ['App\StateMachine\OnHoldGuard', 'canUnhold']
args: ['object']
guard_unhold_fulfill:
on: unhold_fulfill
do: ['App\StateMachine\OnHoldGuard', 'canUnholdFulfill']
args: ['object']
<?php
// file src/EventSubscriber/AdminMenuSubscriber.php
namespace App\EventSubscriber;
use Sylius\Bundle\AdminBundle\Event\OrderShowMenuBuilderEvent;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\OrderShippingStates;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
class AdminMenuSubscriber implements EventSubscriberInterface
{
/**
* @var CsrfTokenManagerInterface
*/
private $csrfTokenManager;
public function __construct(
CsrfTokenManagerInterface $csrfTokenManager
)
{
$this->csrfTokenManager = $csrfTokenManager;
}
/**
* @param OrderShowMenuBuilderEvent $event
* @throws \SM\SMException
*/
public function onSyliusMenuAdminOrderShow(OrderShowMenuBuilderEvent $event)
{
if ($event->getStateMachine()->can('hold')) {
$event->getMenu()
->addChild('hold', [
'route' => 'sylius_admin_order_hold',
'routeParameters' => [
'id' => $event->getOrder()->getId(),
'_csrf_token' => $this->csrfTokenManager->getToken((string) $event->getOrder()->getId())->getValue(),
],
])
->setAttribute('type', 'transition')
->setLabel('sylius.ui.hold')
->setLabelAttribute('icon', 'pause circle')
->setLabelAttribute('color', 'blue')
;
}
if ($event->getStateMachine()->can('unhold')) {
$event->getMenu()
->addChild('unhold', [
'route' => 'sylius_admin_order_unhold',
'routeParameters' => [
'id' => $event->getOrder()->getId(),
'_csrf_token' => $this->csrfTokenManager->getToken((string) $event->getOrder()->getId())->getValue(),
],
])
->setAttribute('type', 'transition')
->setLabel('sylius.ui.unhold')
->setLabelAttribute('icon', 'play circle')
->setLabelAttribute('color', 'blue')
;
}
if ($event->getStateMachine()->can('unhold_fulfill')) {
$event->getMenu()
->addChild('unhold_fulfill', [
'route' => 'sylius_admin_order_unhold_fulfill',
'routeParameters' => [
'id' => $event->getOrder()->getId(),
'_csrf_token' => $this->csrfTokenManager->getToken((string) $event->getOrder()->getId())->getValue(),
],
])
->setAttribute('type', 'transition')
->setLabel('sylius.ui.unhold_fulfill')
->setLabelAttribute('icon', 'play circle')
->setLabelAttribute('color', 'blue')
;
}
}
public static function getSubscribedEvents()
{
return [
'sylius.menu.admin.order.show' => 'onSyliusMenuAdminOrderShow',
];
}
}
# file translations/messages.en.yaml
sylius:
ui:
hold: Hold
unhold: Unhold
unhold_fulfill: Unhold and fulfill the order
<?php
// file src/StateMachine/OnHoldGuard.php
namespace App\StateMachine;
use App\Entity\Order\Order;
use Sylius\Component\Core\OrderPaymentStates;
use Sylius\Component\Core\OrderShippingStates;
class OnHoldGuard
{
public static function canUnhold(Order $order) : bool {
return $order->getShippingState() !== OrderShippingStates::STATE_SHIPPED ||
$order->getPaymentState() !== OrderPaymentStates::STATE_PAID;
}
public static function canUnholdFulfill(Order $order) : bool {
return !self::canUnhold($order);
}
}
# file config/routes/sylius_admin.yaml
sylius_admin:
resource: "@SyliusAdminBundle/Resources/config/routing.yml"
prefix: /admin
sylius_admin_order_hold:
path: /orders/{id}/hold
methods: [PUT]
defaults:
_controller: sylius.controller.order:applyStateMachineTransitionAction
_sylius:
permission: true
state_machine:
graph: sylius_order
transition: hold
redirect: referer
sylius_admin_order_unhold:
path: /orders/{id}/unhold
methods: [PUT]
defaults:
_controller: sylius.controller.order:applyStateMachineTransitionAction
_sylius:
permission: true
state_machine:
graph: sylius_order
transition: unhold
redirect: referer
sylius_admin_order_unhold_fulfill:
path: /orders/{id}/unhold-fulfill
methods: [PUT]
defaults:
_controller: sylius.controller.order:applyStateMachineTransitionAction
_sylius:
permission: true
state_machine:
graph: sylius_order
transition: unhold_fulfill
redirect: referer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment