3

Task

I have been issued to alter the cart form view Shopping cart form (Commerce Order) on the site.com/cart page of the Drupal 7's commerce module. This includes adding extra choices such as sizes together with simply updating the quantity of each product.

My attempts so far

I've tried overriding commerce_cart_block just realise it was commerce_cart_form that was in charge of the content. Now I've tried a module that's called Commerce Cart View Override and it helps me have a template file called views-view--commerce-cart-form.tpl.php inside my template to display the content.

However I'm not really sure what I'm supposed to do now. In my mind I would override the table that show the items and the form surrounding it. I echo'ed get_defined_vars() in the template just to realise that there are quite many variables passed.

Question

What I desire is an elegant solution that allows me to append more forms to modify existing cart session, all in the /cart page, and also be able to have full control of the current cart table.

What is the ideal way for me to customize the /cart page? Am I on the right path, or there some simplier alternatives that I have missed? Personally I want a coded solution and not a database saved one.

4
  • No offence but this just reads like a frustrated rant, it's difficult to understand what specific question you're asking. A better approach would be to include what you've already tried (including code, importantly), and what about that didn't work. You might not know yet but you're actually asking a bunch if different questions, the answers to which require a reasonably good knowledge of Drupal Commerce's workflow, and (more so) a good knowledge of Views theming. It's difficult to see how a single answer can address all the issues so I'd advise breaking it down into much smaller chunks
    – Clive
    Commented Jun 25, 2013 at 13:01
  • 1
    Sorry, I will try to break it up a bit. Sadly my patience and focus runs thin when I try to google an answer for what feels like forever without finding any documentation nor similiar problems with solution to guide me to the answer. Commented Jun 25, 2013 at 13:04
  • Believe me I sympathise (what programmer wouldn't?) I was only thinking about you actually getting answers - at the moment it's probably a bit much for a single person to take on. Try and break it down into individual tasks and attacking those one at a time. It'll be much easier for us to help you with that format, plus your questions will be useful to future visitors - everybody wins :)
    – Clive
    Commented Jun 25, 2013 at 13:09
  • 1
    Cleaned it up a bit hopefully. Thanks for your understanding. I have a bit more of a hard time getting along with Drupal then with CodeIgniter and also CakePHP. Feels like Drupal is alot about having faith to contrib modules, a faith which I don't have. Commented Jun 25, 2013 at 13:17

1 Answer 1

4

Editing the /cart page in Views

I think this is the expected approach. Note: this assumes you know your way around Views.

  1. Install Commerce Cart View Override, as you've done.
  2. Edit the view named Shopping Cart Form, clone it, change the machine name to something starting with commerce_cart_form like commerce_cart_form_custom
  3. Select this in the dropdown on the admin page admin/commerce/config/cart-overrides (Administer >> Store >> Configuration >> Cart Overrides), then save.
  4. You can now edit the view that appears on the /cart page by editing this new view. A few non-intuitive tips:
    • To see a preview in the Views preview, you'll need to enter a shopping cart ID number into the "Preview with contextual filters" box above the preview. You can see some shopping cart ID numbers at /admin/commerce/orders/carts. You just enter the ID number, no other text needed.
    • To add any views fields from the products, for example product images, you'll need to add a new relationship from Line Items to Products. Under Advanced in the Views UI, under Relationships, click Add, then choose Commerce Line item: Referenced products. Product fields like images and taxonomy tags will then become available under Fields.
    • A common Commerce Cart configuration confusion: If no "Checkout" button appears for customers who aren't logged in, you need to go to admin/people/permissions and tick the "Anonymous user" box next to "Access checkout"
    • If you want to change the labels of buttons, for example the "Remove" buttons... that's a whole different story. Form elements are generated in a whole different part of the Drupal ecosystem and put into a View like this pre-rendered, all you can edit in Views is the labels that appear alongside or above the button. To edit the buttons themselves has to be PHP code:

Editing form elements like buttons

In a custom module or your theme's template.php file (remember that if you use a seperate admin theme, your template.php won't have any effect in the views preview, only the customer-facing pages), add something like this (credit):

function yourthemename_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id)  {
  case 'views_form_commerce_cart_form_custom_default': 
  //               ^ from here on should match the view machine name from earlier 
  //                                         ^ from here, the particular view display, which you can see at the end of the Views UI URL while editting that display 
      foreach ($form['edit_delete'] as $row_id => $row) {
        if(isset($form['edit_delete'][$row_id]['#value'])){
            $form['edit_delete'][$row_id]['#value'] = 'New delete button label';
            }
        }

    $form['actions']['checkout']['#value'] = 'New checkout button label';
    $form['actions']['submit']['#value'] = 'New update button label';

  break;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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