$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Query Strings and Woocommerce|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Query Strings and Woocommerce

matteradmin9PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far