Query Strings and Woocommerce

admin2025-06-04  1

I've got a wordpress website and am using WooCommerce to sell registrations for applications that I develop. To streamline the registration process, I would like users to be able to click a registration button in the application and be taken directly to my WooCommerce checkout page with the registration product already loaded in the cart and a username (generated by the application) entered automatically in the checkout page's username field (which I've added).

What I've got so far is, when the user clicks on the applications registration button, the application sends the user in their browser to a URL that looks like:

/?add-to-cart=532&username=myname

In my function.php file I'm redirecting to the checkout page when adding to cart, so this opens the checkout page with the product id = 532 already in the cart. Perfect.

To make the username variable public, I added the following to my functions.php file:

add_filter( 'query_vars', 'add_query_vars_filter' );
function add_query_vars_filter( $vars ){
   $vars[] = "username";
   return $vars;
}

To capture the username from the query string and enter it in a created username field, I also added the following to my functions.php file:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    $thename = get_query_var( 'username', 'defaultName' );
    $fields['billing']['billing_username'] = array(
        'label'     => __($label, 'woocommerce'),
        'placeholder'   => _x($placeholder, 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true,
       'default'    => $thename
    );
 return $fields;
}

Unfortunately, defaultName is entered in the username field. If I remove the add-to-cart=532 from the URL, then myname is entered. I think that the problem is that by the time the checkout page opens, username=myname is stripped from the URL. It seems I have to use $thename = get_query_var( 'username', 'defaultName' ) earlier in the process of opening the checkout page (and one of the problems is that I don't fully understand this process) and somehow make it accessible in the custom_override_checkout_fields function in my functions.php file. So the question is how do I accomplish this?

I've got a wordpress website and am using WooCommerce to sell registrations for applications that I develop. To streamline the registration process, I would like users to be able to click a registration button in the application and be taken directly to my WooCommerce checkout page with the registration product already loaded in the cart and a username (generated by the application) entered automatically in the checkout page's username field (which I've added).

What I've got so far is, when the user clicks on the applications registration button, the application sends the user in their browser to a URL that looks like:

http://www.example/checkout/?add-to-cart=532&username=myname

In my function.php file I'm redirecting to the checkout page when adding to cart, so this opens the checkout page with the product id = 532 already in the cart. Perfect.

To make the username variable public, I added the following to my functions.php file:

add_filter( 'query_vars', 'add_query_vars_filter' );
function add_query_vars_filter( $vars ){
   $vars[] = "username";
   return $vars;
}

To capture the username from the query string and enter it in a created username field, I also added the following to my functions.php file:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    $thename = get_query_var( 'username', 'defaultName' );
    $fields['billing']['billing_username'] = array(
        'label'     => __($label, 'woocommerce'),
        'placeholder'   => _x($placeholder, 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true,
       'default'    => $thename
    );
 return $fields;
}

Unfortunately, defaultName is entered in the username field. If I remove the add-to-cart=532 from the URL, then myname is entered. I think that the problem is that by the time the checkout page opens, username=myname is stripped from the URL. It seems I have to use $thename = get_query_var( 'username', 'defaultName' ) earlier in the process of opening the checkout page (and one of the problems is that I don't fully understand this process) and somehow make it accessible in the custom_override_checkout_fields function in my functions.php file. So the question is how do I accomplish this?

Share Improve this question edited Jun 15, 2016 at 9:02 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Jun 15, 2016 at 0:57 user3399941user3399941 211 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I think that I figured this out. In my functions.php file I had the below function to redirect to the checkout page when a product is added to the cart. So I captured the username variable there and then added it to the redirected URL:

add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    if ( ! empty( $_REQUEST['username'] ) ) {
        $thename = $_REQUEST['username'];
        $checkout_url = esc_url( add_query_arg('username', $thename, $checkout_url ) );
    }
    return $checkout_url;
}

Curiously, neither get_query_var('username') or $wp_query->query_vars['username'] worked. I'm not sure why.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748982442a315367.html

最新回复(0)