CoreShop Checkout Step
If you want to implement a custom checkout step, you need to implement the interface CoreShop\Component\Order\Checkout\CheckoutStepInterface
and register your step into your Cart Manager:
# app/config/config.yml
core_shop_core:
checkout:
default:
steps:
custom:
step: app.coreshop.checkout.custom
priority: 50
The Checkout Controller takes care about handling the Checkout for you then.
Optional Checkout Step
If you have an optional checkout step - depending on the cart, your Checkout Step can implement the interface CoreShop\Component\Order\Checkout\OptionalCheckoutStepInterface
.
You need to implement the function isRequired(CartInterface $cart)
Optional Checkout Step Example
<?php
namespace CoreShop\Bundle\CoreBundle\Checkout\Step;
use CoreShop\Bundle\CoreBundle\Form\Type\Checkout\PaymentType;
use CoreShop\Component\Order\Checkout\CheckoutException;
use CoreShop\Component\Order\Checkout\CheckoutStepInterface;
use CoreShop\Component\Order\Checkout\OptionalCheckoutStepInterface;
use CoreShop\Component\Order\Checkout\ValidationCheckoutStepInterface;
use CoreShop\Component\Order\Manager\CartManagerInterface;
use CoreShop\Component\Order\Model\CartInterface;
use CoreShop\Component\Payment\Model\PaymentProviderInterface;
use CoreShop\Component\Store\Context\StoreContextInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
class PaymentCheckoutStep implements CheckoutStepInterface, OptionalCheckoutStepInterface, ValidationCheckoutStepInterface
{
/**
* @var FormFactoryInterface
*/
private $formFactory;
/**
* @var StoreContextInterface
*/
private $storeContext;
/**
* @var CartManagerInterface
*/
private $cartManager;
/**
* @param FormFactoryInterface $formFactory
* @param StoreContextInterface $storeContext
* @param CartManagerInterface $cartManager
*/
public function __construct(
FormFactoryInterface $formFactory,
StoreContextInterface $storeContext,
CartManagerInterface $cartManager
)
{
$this->formFactory = $formFactory;
$this->storeContext = $storeContext;
$this->cartManager = $cartManager;
}
/**
* {@inheritdoc}
*/
public function getIdentifier()
{
return 'payment';
}
/**
* {@inheritdoc}
*/
public function isRequired(CartInterface $cart)
{
return $cart->getTotal() > 0;
}
/**
* {@inheritdoc}
*/
public function doAutoForward(CartInterface $cart)
{
return false;
}
/**
* {@inheritdoc}
*/
public function validate(CartInterface $cart)
{
return $cart->hasItems() && $cart->getPaymentProvider() instanceof PaymentProviderInterface;
}
/**
* {@inheritdoc}
*/
public function commitStep(CartInterface $cart, Request $request)
{
$form = $this->createForm($request, $cart);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$cart = $form->getData();
$this->cartManager->persistCart($cart);
return true;
} else {
throw new CheckoutException('Payment Form is invalid', 'coreshop.ui.error.coreshop_checkout_payment_form_invalid');
}
}
return false;
}
/**
* {@inheritdoc}
*/
public function prepareStep(CartInterface $cart, Request $request)
{
return [
'form' => $this->createForm($request, $cart)->createView(),
];
}
/**
* @param Request $request
* @param CartInterface $cart
*
* @return \Symfony\Component\Form\FormInterface
*/
private function createForm(Request $request, CartInterface $cart)
{
$form = $this->formFactory->createNamed('', PaymentType::class, $cart, [
'payment_subject' => $cart
]);
if ($request->isMethod('post')) {
$form = $form->handleRequest($request);
}
return $form;
}
}