Howdy,

Just like in the commerce checkout where I can provide a custom view for the cart summary, I'd like to be be able to build my own view for use with the commerce cart form.

This patch modifies commerce_cart_pages.inc so that the view name & display is retrieved from the global variables.

To get this to work, you need to set the global variable 'commerce_cart_form' to point your custom view ; e.g. 'mytheme_cart_form|default'.

Also, you need to implement hook_form_alter in your theme to support the form buttons. For example:

/**
 * Implements hook_form_alter
 *
 * Add the buttons to the cart form for our customised cart view
 *
 * This is needed to override the default actions on the cart actions footer element in the view
 *
 */
function mytheme_form_alter(&$form, &$form_values, $form_id) {
  if (strpos($form_id, 'commerce_line_item_views_form_mytheme_cart_form_') === 0) {
    // Change the Save button to say Update cart.
    $form['actions']['update']['#value'] = t('Update cart');
    $form['actions']['update']['#submit'] = array_merge($form['#submit'], array('commerce_cart_line_item_views_form_submit'));
	
    $form['actions']['checkout'] = array(
      '#type' => 'submit',
      '#value' => t('Checkout'),
      '#weight' => 5,
      '#access' => user_access('access checkout'),
      '#submit' => array_merge($form['#submit'], array('commerce_checkout_line_item_views_form_submit')),
    );

   // Change any Delete buttons to say Remove.
    if (!empty($form['edit_delete'])) {
      foreach(element_children($form['edit_delete']) as $line_item_id) {
        $form['edit_delete'][$line_item_id]['#value'] = t('Remove');
      }
    }
  }
}

Comments

thesurfinganalyst’s picture

StatusFileSize
new844 bytes

With patch

rszrama’s picture

Is there any reason you can't just customize that View?

thesurfinganalyst’s picture

I originally customised the 'commerce_cart_form' View for my theme. However, it was blown away when I downloaded a new version of Commerce.

My thinking is that is was a way to isolate my theming Views away from the Commerce package. My views have a bunch of extra joins and CSS class settings.

Cheers,
Simon.

rszrama’s picture

Hmm... ok, I guess I'm not really sure how your View got lost when you updated Commerce. : ?

Even if a default View changes in code, that shouldn't affect any customizations you've made in the DB. That said, I can understand wanting to just maintain a full separate replacement for the View.

I'm not really sure what to do with this patch, though, since it's not something we really have a place for. I guess the cart summary View is a bit of an oddity there, in that it is allowing selection through the checkout pane settings form. Dunno why I did that now that I think of it, but I'm glad it was useful. : D

Anyways, what should we do with this?

thesurfinganalyst’s picture

It's a long story regarding the loss of the my view; the pain was self inflicted but nevertheless prompted me to set up my own stand alone view.

I momentarily thought about modifying the Checkout settings shopping cart contents form to add a select list so that the variable could be set via the UI; but thought it didn't really make sense to stick that configuration option there.

And yes, it is most definitely useful :)

Simon.

rszrama’s picture

So, I just discovered that theoretically you could clone the existing cart View and add a page display using the cart URL to get the same effect. The problem is the function commerce_line_item_views_form() that we use in turning that View into a form doesn't respect the Order ID argument's settings if the argument is empty - it just chokes. We could probably fix this and turn the menu item itself into part of the View, but we'd have to reorganize how we're building and displaying the empty cart message... just putting it in a text based area handler on the View won't suffice as it wouldn't be translatable or easily themeable.

rszrama’s picture

Status: Active » Closed (duplicate)

As it turns out, this code has completely changed since the cart form code has now been put into Views 3 itself. The solution to this issue will no reside in #1316916: Make the Shopping cart form and block function entirely through Views, an issue just created to convert both the cart block and form to be purely based on Views.

smokris’s picture

Status: Closed (duplicate) » Needs review
StatusFileSize
new790 bytes

I'd also find it useful to be able to change the view that Commerce uses for the cart page — my use case is that I'd like to be able to built a custom cart view (to use Commerce Product Attributes) and add my customized view to a Feature module. (I've tried the Features Override module, but found it unreliable.)

Here's an updated version of the patch from #1.

chris matthews’s picture

The 6 year old patch in #8 applied cleanly to the latest commerce 7.x-1.x-dev and (if still relevant) needs to be reviewed.