RESOLVED: uc_varprice: problem simulating form submission with drupal_execute()

6 replies [Last post]
Joined: 09/10/2009
Juice: 16

UPDATED: 29/Sept/09 - corrected the code a bit...

I'm using uc_varprice with this patch: http://drupal.org/node/466262
(and this is how the code looks like: http://drupalbin.com/11597 )
I'm trying to submit a product form with uc_varprice feature enabled using drupal_execute(), but for some reason it fails to pass on the variable price that the product is added to the cart with its default price.

Here's the code I'm using:

  $node = $item['nid'];
  $varprice_form_state['values']['varprice_sel'] = $item['varprice_sel'];
  $varprice_form_state['values']['varprice_arb'] = $item['varprice_arb'];
  $varprice_form_state['values']['nid'] = $item['nid'];
  drupal_execute('uc_product_add_to_cart_form', $varprice_form_state, $node);

When uc_varprice_add_to_cart_data($form_values), which adds the variable price information to the product, is called through this process, $form_state loses both varprice_sel and varprice_arb. These values are of course successfully passed onto the hook implementation when the varprice form is submitted normally.

Am I passing the $form_state wrong?

Joined: 09/12/2008
Juice: 64

I don't know if it will totally solve your problem, but note that you misspelled $item['varprice_arb'] on that third line of code. Assuming that error is also in your actual code, try correcting it and let us know if that's enough to fix the problem.

(Also, there's no semicolon on the end of the first line, but I'm assuming that that's fixed in your actual code, or else it wouldn't be able to run at all.)

Joined: 09/10/2009
Juice: 16

Thanks Garrett for your response. Sorry I pasted the code but modified it slightly in the editor and that's where the typo came from. The original code doesn't have those typos you pointed out and has the problem.

Joined: 09/12/2008
Juice: 64

Hmm. Well, I notice in your code, you're using krumo() in uc_varprice_add_to_cart_data(). Does it look like $form_values have the correct value at that point?

Can you try putting a drupal_set_message() line after line 266 to see if the code is falling through that if clause?

Joined: 09/10/2009
Juice: 16

I got to the bottom of this. drupal_execute() calls drupal_process_form() whose first line is:

  $form_state['values'] = array();

Since uc_product_add_to_cart_form_submit() invokes all implementations of hook_add_to_cart_data() and passes $form_state to uc_cart_add_item() as $data, all variables get wiped.

drupal_execute() passes $form_state['values'] to $form['#post'] so it's still possible to refer to the values. However this means drupal_execute() is inherently unusable for uc_cart.module unless you hack the core or have your own version of drupal_execute() and drupal_process_form()

Joined: 09/10/2009
Juice: 16

Thanks Garrett for your reply. You were probably looking at on this almost exactly at the same time as I was. As described in the comment above, I got to the bottom of it and resolved this by having my version of drupal_execute() and drupal_process_form(). I didn't post the code properly to both here and drupalbin that your attention got diverted. I apologise for that and thank you very much for taking your time for this.

Joined: 09/10/2009
Juice: 16

I should add that drupal_execute() *does* work with uc_cart.module. It's just that (probably) any module that works with the module by hooking into hook_add_to_cart_data() gets ignored.