I load my checkout form through a custom template file region--content--checkout.tpl.php

Inside this file at the beginning I load the checkout form:

$form = drupal_get_form('uc_cart_checkout_form');
		//cache_clear_all('form_'. $form['#build_id'], 'cache_form');
		foreach ($form['panes'] as &$pane) {
   			 if (isset($pane['#collapsible'])) {
      			$pane['#collapsible'] = FALSE;
    		 }
  		}
		
		$items = uc_cart_get_contents();
		$total = 0;

I then proceed to load each pane where I want inside the template file via calls such as:

<?php
print drupal_render($form['panes']['customer']);
print drupal_render($form['panes']['delivery']);
?>

The problem I am having is that everything works for anonymous users just fine, but for users who are logged in I always get the following error:

This form is outdated. Reload the page and try again. Contact the site administrator if the problem persists.

which occurs right after they change their country, which makes some AJAX calls to load available cities etc.

This never occurs for anonymous users and everything works fine. So it's a caching issue for logged in users correct? What's the best way to never cache or clear the cache of this form everything it hits?

You can see above I tried the call:

cache_clear_all('form_'. $form['#build_id'], 'cache_form');

But this didn't work. I think my $cid value might be wrong but not sure? Any help please?

Comments

longwave’s picture

Priority: Major » Normal
Status: Active » Postponed (maintainer needs more info)

I have never seen that method of rendering forms directly in a template before, so it's probably related to that. What exactly are you trying to accomplish with this? Why do you need to render the form this way? Does this method work with other (non-Ubercart) forms?

Note that in latest -dev you do not even have to set #collapsible to FALSE, this is now done by default.

longwave’s picture

Status: Postponed (maintainer needs more info) » Fixed

Instead of using this method you should use the standard /cart/checkout page and theme the form using uc-cart-checkout-form.tpl.php which now works in 7.x after #1299242: What the D7 equivalent template to uc-cart-checkout-form.tpl.php???

Woggers’s picture

Status: Fixed » Needs review

@longwave

Have tried using uc-cart-checkout-form.tpl.php but D7 does't seem to be picking it up at all? Is the syntax in the template name correct? Or am I missing a double-dash somewhere?

The reason it was so heavily customized was because of http://drupal.org/node/1298386 and I had to go the fancy javascript route of making it look like multiple pages.

What is the correct syntax for the template file?

Thanks!

longwave’s picture

Status: Needs review » Fixed

uc-cart-checkout-form.tpl.php is not in 7.x-3.0-rc2 but does work in current -dev. You need to clear cache after adding the file for the theme system to pick it up. The basic syntax to duplicate the existing form will be:

print drupal_render_children($form);
Woggers’s picture

Status: Fixed » Needs review

@longwave

Thanks for the assistance.

Have the latest ubercart -dev release, just downloaded (Nov 3, 2011) and updated today. Have cleared cache 10x now after adding uc-cart-checkout-form.tpl.php to the templates folder. Also have the theme registry set to rebuild every page load through Devel.

Not being picked up?

For testing purposes my template file only contains:

<?php
   print kpr($variables);
   print 'TEST';
?>

to see if it's being picked up but when I go to checkout it's rendering the entire checkout form ?

longwave’s picture

Status: Needs review » Fixed

Works for me. I added /themes/bartik/templates/uc-cart-checkout-form.tpl.php with the word "test", cleared cache, and when I went to the checkout page I only saw the word "test" instead of the checkout form.

"Needs review" is for patches, btw. Use "active" to reopen support issues.

longwave’s picture

Note this is not really Ubercart specific any more, we do not handle general theming support in this issue queue. http://drupal.org/project/devel_themer might help you discover what's happening here.

Woggers’s picture

Status: Fixed » Active

@longwave

sorry about that needs review / active thing.

Got it working with the correct template uc-cart-checkout-form.tpl.php Thanks!

I still have to render the form manually though to correctly get everything laid out in the javascript ui tabs.
ie. Email Information Tab, Shipping pane tab, Billing Pane Tab etc.

So I am still doing the following inside the template file:

<?php
$form = $variables['form'];

....some random code
?>
<form id="uc-cart-checkout-form" class="uc-cart-checkout-form" accept-charset="UTF-8" method="post" action="/cart/checkout">
		<div id="checkout-form-tabs">
		<div id="tabs">
			.....random tab code....
                       <?php print drupal_render[$form['panes']['customer']; ?> // inside tab 1
                       <?php print drupal_render[$form['panes']['delivery']; ?> // inside tab 2
                       <?php print drupal_render[$form['panes']['billing']; ?> // inside tab 3

// and then we add the 2 hidden variables that are part of the form manually to the page
<input type="hidden" value="<?php print $form['#build_id']; ?>" name="form_build_id">
<input type="hidden" value="uc_cart_checkout_form" name="form_id">
</form>

For anonymous users everything works! For logged in users, as soon as an AJAX call is made after they change the country from E.g USA to Canada (and the states update to provinces) I get an error saying:

This form is outdated. Reload the page and try again. Contact the site administrator if the problem persists.

Does not happen with anonymous users though.

I ran a quick test to to just render the form in it's entirety instead of in pieces using:

drupal_render_children($form);

...and of course this works fine for all users. What the heck? :) any thoughts?

tr’s picture

The "form outdated" is a Form API error meaning the form session token submitted back to the web server doesn't match the token sent to the browser.

This is not an Ubercart problem - you will have to search the Drupal forums and read up on the Form API to figure out how to properly do what you want in the case of an anonymous user.

Woggers’s picture

@TR

Thank you sir! You steered me in the right direction.

Adding the following line to the form fixed everything up!

<input type="hidden" name="form_token" value="<?php print drupal_get_token($form['#form_id']); ?>">
Woggers’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.