Sascha Grossenbacher
https://slides.com/saschagrossenbacher/commerce
1. Start Checkout
2. Checkout
3. Processing
// With a product:
$order_item = OrderItem::create([
'type' => 'default',
'purchased_entity' => $commerce_product->getDefaultVariation()->id(),
'quantity' => 2,
]);
// Arbitrary amount and title
order_item = OrderItem::create([
'type' => 'custom_item_type',
'title' => 'Subscription fee',
'unit_price' => [
'number' => 50,
'currency_code' => 'CHF',
],
'quantity' => 1,
''
]);
$store = \Drupal::service('commerce_store.current_store')->getStore();
$order = Order::create([
'type' => 'default',
'mail' => $this->currentUser()->getEmail(),
'uid' => $this->currentUser()->id(),
'ip_address' => \Drupal::request()->getClientIp(),
'store_id' => $store->id(),
'order_items' => [$order_item],
'cart' => FALSE,
]);
$order->save();
// In a controller.
return $this->redirect('commerce_checkout.form', ['commerce_order' => $order->id()]);
// In a form submit callback.
$form_state->setRedirect('commerce_checkout.form', ['commerce_order' => $order->id()]);
$cart_session = \Drupal::service('commerce_cart.cart_session');
$cart_session->addCartId($order->id(), CartSessionInterface::COMPLETED);
/**
* @CommerceCheckoutFlow(
* id = "custom_multistep",
* label = "Multistep - Custom",
* )
*/
class CustomMultiStep extends MultistepDefault {
/**
* {@inheritdoc}
*/
public function getSteps() {
$steps = parent::getSteps();
$steps['complete']['label'] = $this->t('Thanks for subscribing!');
return $steps;
}
// Alternatively switch out the default class with yours.
function custom_commerce_checkout_flow_info_alter(&$definitions) {
$definitions['multistep_default']['class'] = MultiStepCustom::class;
}
/**
* @CommerceCheckoutPane(
* id = "points_payment",
* label = @Translation("Points payment"),
* default_step = "_sidebar",
* wrapper_element = "container",
* )
*/
class PointsPayment extends CheckoutPaneBase {
/**
* {@inheritdoc}
*/
public function buildPaneForm(array $pane_form,
FormStateInterface $form_state, array &$complete_form) {
// ...
return $pane_form;
}
}
Define custom processes or sublcass and repace default panes to customize them
function custom_block_access(Block $block, $operation, AccountInterface $account) {
$route_match = \Drupal::routeMatch();
if ($route_match->getRouteName() == 'commerce_checkout.form'
&& $route_match->getParameter('commerce_order') instanceof OrderInterface) {
$order = $route_match->getParameter('commerce_order');
// Hide blocks in certain regions during checkout (show on complete page).
if ($order->getState()->getId() == 'draft') {
$config = \Drupal::configFactory()->get('custom.settings');
$regions = $config->get('checkout_empty_regions');
$whitelisted_blocks = $config->get('checkout_whitelisted_blocks');
if (in_array($block->getRegion(), $regions)
&& !in_array($block->id(), $whitelisted_blocks)) {
return AccessResult::forbidden()->addCacheContexts(['url.path']);
}
}
}
}
}
public function enqueueOrder(WorkflowTransitionEvent $transition_event) {
$order = $transition_event->getEntity();
if ($order instanceof OrderInterface &&
$order->getState()->getWorkflow()->getId() == 'order_aboware'
&& $transition_event->getTransition()->getToState()->getId() == 'needs_sync') {