Skip to content

Instantly share code, notes, and snippets.

@sumanthkumarc
Created February 8, 2017 09:29
Show Gist options
  • Select an option

  • Save sumanthkumarc/fe754d76932e2e0ca41cb42b33f654c2 to your computer and use it in GitHub Desktop.

Select an option

Save sumanthkumarc/fe754d76932e2e0ca41cb42b33f654c2 to your computer and use it in GitHub Desktop.
Add a button to view and add submit handler -taken from Commerce2.x D8
/**
* Implements hook_form_alter().
*/
function commerce_checkout_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (strpos($form_id, 'views_form_commerce_cart_form_') === 0) {
// Only add the Checkout button if the cart form view has order items.
$view = reset($form_state->getBuildInfo()['args']);
if (!empty($view->result)) {
$form['actions']['checkout'] = [
'#type' => 'submit',
'#value' => t('Checkout'),
'#weight' => 5,
'#access' => \Drupal::currentUser()->hasPermission('access checkout'),
'#submit' => array_merge($form['#submit'], ['commerce_checkout_order_item_views_form_submit']),
'#order_id' => $view->argument['order_id']->value[0],
];
}
}
}
/**
* Submit handler used to redirect to the checkout page.
*/
function commerce_checkout_order_item_views_form_submit($form, FormStateInterface $form_state) {
$order_id = $form_state->getTriggeringElement()['#order_id'];
$form_state->setRedirect('commerce_checkout.form', ['commerce_order' => $order_id]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment