php – Trying to Set Shipping and Billing Details on Form on Order-Pay Page-ThrowExceptions

Exception or error:

I’m trying to set Billing and Shipping Details for a WooCommerce order using a form on the order-pay page rather than the original checkout page

I need the billing and shipping details to be set from the order-pay page

add_action('woocommerce_thankyou', 'set_details', 10, 1);
function set_details() {

    global $post;
    $user_id = wc_get_order($order_id);

    // billing details
    $billing_first_name = $_POST['billing_first_name'];
    $billing_last_name = $_POST['billing_last_name'];
    $billing_company = $_POST['billing_company'];
    $billing_address_1 = $_POST['billing_address_1'];
    $billing_address_2 = $_POST['billing_address_2'];
    $billing_city = $_POST['billing_city'];
    $billing_state = $_POST['billing_state'];
    $billing_postcode = $_POST['billing_postcode'];
    $billing_country = $_POST['billing_country'];

    update_user_meta($user_id, "billing_first_name", $billing_first_name);
    update_user_meta($user_id, "billing_last_name", $billing_last_name);
    update_user_meta($user_id, "billing_company", $billing_company);
    update_user_meta($user_id, "billing_address_1", $billing_address_1);
    update_user_meta($user_id, "billing_address_2", $billing_address_2);
    update_user_meta($user_id, "billing_city", $billing_city);
    update_user_meta($user_id, "billing_state", $billing_state);
    update_user_meta($user_id, "billing_postcode", $billing_postcode);
    update_user_meta($user_id, "billing_country", $billing_country);

    // shipping details
    $shipping_first_name = $_POST['shipping_first_name'];
    $shipping_last_name = $_POST['shipping_last_name'];
    $shipping_company = $_POST['shipping_company'];
    $shipping_address_1 = $_POST['shipping_address_1'];
    $shipping_address_2 = $_POST['shipping_address_2'];
    $shipping_city = $_POST['shipping_city'];
    $shipping_state = $_POST['shipping_state'];
    $shipping_postcode = $_POST['shipping_postcode'];
    $shipping_country = $_POST['shipping_country'];

    update_user_meta($user_id, "shipping_first_name", $shipping_first_name);
    update_user_meta($user_id, "shipping_last_name", $shipping_last_name);
    update_user_meta($user_id, "shipping_company", $shipping_company);
    update_user_meta($user_id, "shipping_address_1", $shipping_address_1);
    update_user_meta($user_id, "shipping_address_2", $shipping_address_2);
    update_user_meta($user_id, "shipping_city", $shipping_city);
    update_user_meta($user_id, "shipping_state", $shipping_state);
    update_user_meta($user_id, "shipping_postcode", $shipping_postcode);
    update_user_meta($user_id, "shipping_country", $shipping_country);
}

Nothing happens upon thankyou i.e. no billing or shipping details are set

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *