• Resolved t.schwarz

    (@tschwarz-1)


    I’m saving additional order meta and order item meta from custom form fields on the checkout page upon order creation using "woocommerce_checkout_update_order_meta" for the order meta and "woocommerce_checkout_create_order_line_item" for  the addtional order item meta.

    This approach works well when a) the user is logged in or b) users are guests (no account creation allowed). However, when a user is creating a new customer account while submitting an order, $_POST isn’t available at the hooks mentioned above for reasons I don’t understand (I assume they will have to do with the user creation process).

    For order meta, it’s possible to use the data provided at the hook to create the additional meta fields. However, for the line item meta, the data provided at the hook "woocommerce_checkout_create_order_line_item" doesn’t contain the additional fields that I want to add to the line items through the form on the checkout page.

    To me this implies that I either need to access $_POST at the above hook to create the additional line item meta or somehow add the required variables from $_POST to the cart using a hook that fires after the submission click but before the user creation and thus that still has access to $_POST.

    Can you think of a hook to use? Thanks for any suggestions.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @tschwarz-1

    Your issue seems to be related to the timing of when the $_POST array is accessible during the checkout process.

    The woocommerce_new_customer_data hook might be helpful in this situation. It fires after a user submits the form but before the user data is saved. You could use this hook to save the additional data from the $_POST array into a temporary session variable.

    Then, you could use the woocommerce_checkout_create_order_line_item hook to retrieve that data from the session and add it to the line item meta.

    If you are still having problems, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question.

    I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.

    Thread Starter t.schwarz

    (@tschwarz-1)

    Thanks for your reply! Turns out, my analysis was a bit wrong – I based by conclusion that $_POST was missing at the specific hook on the fact that the “checkout-process-nonce” will not verify in case a new customer is registered, while it will verify in case of guests and previously logged-in users. $_POST is available after all. But the nonce verification will still fail in the described case.

    Since I also read elsewhere that I don’t really need to check the nonce at that point (for some reason, because WPCS will flag it, and usually even within admin the admin screens custom functionality is told to do proper nonce-checking?), so that solves my specific problem, but it still feels a bit like a bug (because it makes no sense for the checkout nonce verification to be dependent on user creation), so I may open a ticket about this. Thanks again for your help!

    • This reply was modified 1 year, 1 month ago by t.schwarz.
    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @tschwarz-1,

    I’m glad you were able to find a solution to your inquiry here, and thanks for sharing it with the community too! ??

    Should you have further inquiries, kindly create a new topic here.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘$_Post not available when user is created at submission’ is closed to new replies.