• I have an external (Non-Wordpress) website that has a table of addresses and associated wordpress user id’s. Each row has a link that opens the Woocommerce Admin page for creating a new order, and populates the billing and shipping information from rawurlencoded $_GET data like this:

    http://cooldomain.com/wp-admin/post-new.php?post_type=shop_order&f=BOBBY&l=SMITH
    &a=133+M+STREET&b&c=MONROE&s=LA&p=24754&n=US&u=2
    You'll notice that the last token is u=2

    This is the user_id associated with that address in our WordPress site, so we want to generate the new order for that user.

    Here’s the hook we use in functions.php

    if (isset($_GET['f'])) {
        add_action('woocommerce_admin_order_data_after_order_details', 'my_auto_populated_meta');
    }

    Then our function is like this:

    function my_auto_populated_meta($order) {
        $first_name = isset($_GET['f']) ? rawurldecode(trim($_GET['f'])) : '';
        ...
        ...
        $uid = isset($_GET['u']) ? intval($_GET['u']) : '';
    
        update_post_meta($order->id, '_billing_first_name', $first_name);
        ...
        ...
        update_post_meta($order->id, '_customer_user', $uid);
     }

    The problem:

    When the page first loads and the form is auto-populated, and I echo out the post_meta, the _customer_user value shows as 2, like this:

    [_customer_user] = array( [0] => 2 )

    But as soon as I click the “Create” button in Woocommerce, it strips that value out so it is:

    [_customer_user] = array( [0] => )

    How do I make it stick?

    Should I use a different action or hook?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter poptartgun

    (@poptartgun)

    I have found that when the page first loads, and is auto-populated with the addresses, the _customer_user value is set in the database fine… but clicking save whacks it back to a 0… any way around that?

    Thread Starter poptartgun

    (@poptartgun)

    anyone?

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Programmatically Assign _customer_user in Woocommerce post meta’ is closed to new replies.