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?

9
  • 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
    Commented Nov 20, 2019 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. Commented Nov 20, 2019 at 19:51
  • the dump above is the $form_state or $form ?
    – berramou
    Commented Nov 20, 2019 at 19:54
  • That dump is from my external site within $_REQUEST. Commented Nov 20, 2019 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
    Commented Nov 20, 2019 at 19:59

1 Answer 1

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 and acknowledge you have read our privacy policy.

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