0

I need to move the payment method from the checkout page in drupal commerce and embed it into the cart page. is that even possible? I have been finding some resources and it does not lead me anywhere.

I believe I will need a custom module for this and which hook should I used for presenting a payment option in the cart page and when hitting 'checkout' button it will just go and review everything. so payment is processed in the cart page also.

thank you

2

It's actually very straight forward! If you want to not completely override the /cart url (basically, leaving the /cart in tact, but simply link key user interface elements directly to checkout) then you can skip to step 2...

Step 1: Overriding the cart view

Add an alias

You will want to do three things:

  1. First, you'll want to "alias" the "/cart" to "/checkout" (see screenshot above) ... this will ensure all system links that go to cart actually just link to "/checkout"

  2. Second, you'll want to override the system path "/cart" and have it redirect to an appropriate location. (code below)

  3. Finally, you'll need to disable the view "Shopping cart form" (otherwise our hook_menu_alter may not work).

See verified code below (as in I checked to make sure this works).

<?php 
function mymodule_menu_alter(&$items) {
  $items['cart']['page callback'] = '_mymodule_redirectcart';
}
function _mymodule_redirectcart() {
  global $user;
  $account = $user;
  $cart = commerce_cart_order_load($account->uid);

  // if user doesn't have a cart, go to <front>
  // else if user's cart is empty
  if ($cart == false || count($cart->commerce_line_items) == 0) {
    drupal_goto('<front>');
  }

  // go to checkout
  drupal_goto('checkout/'.$cart->order_id);
}
?>

Don't forget to disable the shopping cart form

disable shopping cart form

Step 2: Making a one page checkout

I borrowed the step-by-step from an excellent "Express Checkout" blog.

  1. From http://www.example.com/admin, click on the Store link, then Configuration, then Checkout settings
  2. Drag the Review item out of the Review order section and into the disabled section
  3. Drag the Payment item out of the Review order section and up into the Checkout section
  4. Click Save Configuration

one page checkout screenshot

Step 3: Modify the title and button

This step is optional, but useful. Basically, we just use hook_form_alter to change the "Continue" button to indicate that payment will be taken and we modify the page title to "Cart" to further our ruse that this is the cart page and not checkout.

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'commerce_checkout_form_checkout') {
    drupal_set_title("Cart");
    $form['buttons']['continue']['#value'] = t('Proceed with purchase');
  }
}
?>

Step 4: Your new cart checkout

This is not really a step, but it is the result of your effort. Now your new "cart" has a checkout/payment pane. Only thing you would be missing from the original cart is the ability to modify your cart (remove products) and that is where this process would fail you.

cart checkout

Other Thoughts

If your customers will need to be able to modify the cart (most likely the case), then you will need to link to a clone of the commerce_cart_view at a unique url (perhaps /cart-edit?) and then add a link called "Edit Cart Contents" in the footer of your cart contents view to indicate that they can remove items, but they have to click a link to do it.

clone shopping cart

  1. The view you want to clone is called "Shopping cart form" and the clone option can be found at /admin/structure/views
  2. Make sure you edit the path in your cloned view to "cart-edit" or some variation.
  3. Edit the footer of the "Shopping Cart Summary" view found at /admin/structure/views/view/commerce_cart_summary/edit to look like the below:

edit cart link example

The reason you can't modify cart items in checkout is that "forms within forms" is not a straightforward thing to implement, but linking to a view to edit your order is pretty simple and everyone wins with a 10 minute change.

  • 1
    this is great solutions! yeah, I build it like you told me and all are working well, thank you once again! – Boby Jan 19 '16 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.