Support » Plugin: WooCommerce » Capturing posted variables at Place Order (WooCommerce Checkout)

  • I’m trying to add new variables to be posted when “Place Order” is submitted via checkout. These variables are then to be captured and added to product post_meta (with add_post_meta). However, I have found that $_POST variables simply are not captured, and may somehow be cleared by WooCommerce.

    I am wondering if a) PLACE ORDER clears all $_POST variables somehow. If so, b), can that behavior be bypassed directly, or do I c) have to place the form in a new area independent of PLACE ORDER, so it is submitted separately, or d) is it some completely other problem?

    To give simplified versions of the form submit code and add_post_meta code I’m using, the first looks something like this (the brackets on the name variable are so that the fields can accept arrays via multiple input actions):

    echo '  <input type="text" name="first_name[]" id="first_name" />';
    echo '  <input type="text" name="last_name[]" id="last_name" />';

    The lines are added to Woo checkout – after the action woocommerce_checkout_after_customer_details – with a function that also grabs the post ID for the product, which is used in the add_post_meta() function triggered on PLACE ORDER submit. The problem is that, though add_post_meta() with send the array of keys it’s meant to send, it won’t capture $_POST variables. I’m using something like the following:

    if (isset($_POST) == true) {
        $att_data = array(
            'last_name'         => $_POST['last_name'],
            'first_name'        => $_POST['first_name']
        );
    add_post_meta($event_id,'attendee_list',$att_data);

    It works just fine with dummy values or as a direct addition rather than a form submit (has to be the latter because the values are user-supplied). Variable dumps of the $_POST values confirms an empty array, which shows up on post_meta as key names with NULL values.

    I’ve been working on this intensively, and I think my syntax is correct, so, to return to where I’m started, is there something in the WooC Checkout/PLACE ORDER code that prevents capture of $_POST variables in this manner, and can it be counteracted or bypassed ?

    https://wordpress.org/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter CK MacLeod

    (@ck-macleod)

    Since I’ve had limited initial success 1) using $_GET instead of $_POST and 2) displacing the form outside of the Woo checkout form (to just before it via woocommerce_before_checkout_form), I’m now wondering even more if there’s a particular problem with $_POST variables inside of WooC checkout.

    Hello,
    I seem to be having the same problem. Did you ever figure out why $_POST variables are being cleared/not stored?

    Thread Starter CK MacLeod

    (@ck-macleod)

    Hi – never did nail that one down. Also put up a question at StackExchange WordPress. The GET kludge I settled for was adequate for the client, and it was a small project, so, even though I had some additional ideas on things to try out, I never got around to it. The details are hazy now, but, if you showed your code, they might come back to me. No one ever said, “Yes, this something WooCommerce does you should be aware of.”

    Thanks for the reply.

    I just stumbled across a potential fix. For some reason it seems as if some of woo’s actions do flush $_POST, including woocommerce_checkout_update_order_meta. In each of the functions that were triggered by this or other offending actions, I simply inserted “session_start()” at the top and got it working.

    It was weird though because doing the same with woocommerce_checkout_process caused problems.

    Hope that helps.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Capturing posted variables at Place Order (WooCommerce Checkout)’ is closed to new replies.