0

I am going to be phasing out Drupal Commerce on our website in favor of a new custom built application that is hosted at another URL. Without completely rebuilding everything I am trying to override the Add to Cart form to redirect with POST data to the new app.

Using $form['#action'] I can redirect properly and dumping out shows something like this:

array(7) {
    ["quantity"]=>
    string(1) "2"
    ["product_id"]=>
    string(4) "8566"
    ["form_build_id"]=>
    string(48) "form--90fc5Yp9qgp4CVFJlq03jfEZyYBkAhDX-j7E5g9oBQ"
    ["form_token"]=>
    string(43) "T08Fl1dNHuI4BCZ4JuLNwxbw6gUTOfnodbc1zcQMR4E"
    ["form_id"]=>
    string(35) "commerce_cart_add_to_cart_form_8566"
    ["field_are_you_a_product"]=>
    array(1) {
        [0]=>
        array(1) {
            ["field_are_you_a_"]=>
            array(1) {
                ["und"]=>
                string(3) "yes"
            }
        }
    }
    ["op"]=>
        string(11) "Add to cart"

This is helpful but the product_id is useless as I need more info, like the SKU or a custom ID field I have on the product level. I can't seem to get this to provide me with the product info I need.

I initially tried using a custom submit handler instead but that didn't work. drupal_http_request isn't right as it just sends post data, does actually redirect with post data like action does.

So since setting the action works great, how would I alter this form to send more information during that step within hook_form_alter?

  • you can use commerce_product_load($product_id) to load product object, than you can retrieve any info see the fuction api.drupalhelp.net/api/commerce/… – berramou Nov 20 at 18:34
  • Yes sorry should have included that. I can use that no problem. However storing that information where it is added to post is a problem. Seems that add to cart only submits some information and not all the form data. – Travis Johnston Nov 20 at 19:51
  • the dump above is the $form_state or $form ? – berramou Nov 20 at 19:54
  • That dump is from my external site within $_REQUEST. – Travis Johnston Nov 20 at 19:55
  • I think you should add other fields to your form, in the hook_form_alter for example SKU $form['sky'] = array( '#type' => 'hidden', '#value' => YOUR_VALUE, ); as hidden fields and they will submitted with the form – berramou Nov 20 at 19:59
1

You can add other fields to your form as hiden, in the hook_form_alter for example add SKU field as hidden.

  $form['sky'] = [
    '#type'  => 'hidden',
    '#value' => YOUR_VALUE,
  ];

Hidden fields will submitted like others.

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.