• Hi All,
    First a bit of a warning. I’m very much a newb when it comes to wordpress and php, so if you are able to offer assistance to the below, please use small words if possible :-).

    On to the problem. 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.mysite.com/checkout/?add-to-cart=532&username=myname

    In my function.php file I’m redirecting to the checkout 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?

    Thanks in advance for any help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi,

    I will check the issue.

    Hi,

    Rename this “custom_override_checkout_fields” function and try this following function

    function custom_override_checkout_fields( $fields ) {
    	global $wp_query;
    	$thename = 'defaultName';
    	if(isset($wp_query->query_vars['username']) && !empty($wp_query->query_vars['username'])) {
    		$thename = $wp_query->query_vars['username'];
    	}
    	$fields['billing']['billing_first_name'] = array(
            	'label'     => __($label, 'woocommerce'),
        		'placeholder'   => _x($placeholder, 'placeholder', 'woocommerce'),
        		'required'  => true,
        		'class'     => array('form-row-wide'),
        		'clear'     => true,
    		'default'	=> $thename
        	);
    
         return $fields;
    }
    Thread Starter emmgunn

    (@emmgunn)

    Thanks for the response. I gave this a try and the username field is still set to “defaultName”.

    Looking into this some more, I think that there is some redirecting going on and so the “username=myname” is lost from the URL??? I’ve already got this code in the functions.php file to redirect straight to the checkout page when something is added to the cart:

    add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
    function redirect_to_checkout() {
    	global $woocommerce;
    	$checkout_url = $woocommerce->cart->get_checkout_url();
    	return $checkout_url;
    }

    So I tried to use $thename = get_query_var( ‘username’, ‘defaultName’ ); in the above function thinking I could use add_query_arg() to add the username to the redirect, but $thename is still set to “defaultName”. I also tried your suggestion inside this function.

    I’m guessing that the “add-to-cart=532” is handled somewhere earlier in the WooCommerce code and is doing some redirecting on its own. Perhaps that is where I need to read the username variable. I just don’t know how to find that in the code, and to be honest, I’m not sure how I would get that information to my custom_override_checkout_fields function in the functions.php file. Global variables?

    Thread Starter emmgunn

    (@emmgunn)

    I figured it out. I modified the redirect function like so:

    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;
    }

    I’m not sure why $_REQUEST works, and the other methods I tried above do not??

    do the above codes work ?

    Thread Starter emmgunn

    (@emmgunn)

    Yes. Now you’re making me nervous though. Do you see something questionable in my code?

    thanks a lot for the hint, @emmgunn, this workaround solved a related problem I had!

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

The topic ‘Query String with WooCommerce’ is closed to new replies.