Collecting One-Step Payment with Woocommerce

The URL

add a single product in the cart and direct to checkout

https://CHECKOUT_PATH?add-to-cart=PRODUCT_ID

Clean up Cart

remove all products in the cart but the product just added to ensure single checkout

//TO BE ADDED

Pre-fill Billing Form

https://CHECKOUT_PATH?add-to-cart=PRODUCT_ID&bfn=FIRSTNAME&bln=LASTNAME&bem=EMAIL

pre-fill fields of the checkout form with request parameters

// save additional data to the product
add_filter( 'woocommerce_add_cart_item_data', 'prefill_cart_data', 9999, 3 );
 
function prefill_cart_data( $cart_item_data, $product_id, $variation_id ) {
    $topopulate = array(
       'bfn' => 'billing_first_name',
       'bln' => 'billing_last_name',
       'bem' => 'billing_email',
     );
     foreach ( $topopulate as $urlparam => $checkout_field ) {         
         if ( isset( $_GET[$urlparam] ) && ! empty( $_GET[$urlparam] ) ) {
           $cart_item_data[$checkout_field] = esc_attr( $_GET[$urlparam] );
       }
    }
     return $cart_item_data;
}


add_filter( 'woocommerce_checkout_fields' , 'populate_checkout', 9999 );
   
function populate_checkout( $fields ) {
    $topopulate = array(
      'bfn' => 'billing_first_name',
      'bln' => 'billing_last_name',
      'bem' => 'billing_email',
    );
   foreach ( WC()->cart->get_cart() as $cart_item ) {
      foreach ( $topopulate as $urlparam => $checkout_field ) {
           if ( isset( $cart_item[$checkout_field] ) && ! empty( $cart_item[$checkout_field] ) ) {
            $fields['billing'][$checkout_field]['default'] = $cart_item[$checkout_field];
            // set checkout input read only
            $fields['billing'][$checkout_field]['custom_attributes'] = array('readonly'=>'readonly');
            // TODO removed in production
            $fields['billing'][$checkout_field]['disabled'] = true;
         }
            
        }
    }       
    return $fields;
}

Additional Form Manipulation

for example:

  • disabled form input

Cautious! The following code will disable input submission.

add_filter('woocommerce_before_checkout_billing_form', 'hold_output_before_checkout_form', 9999);

function hold_output_before_checkout_form(){
 ob_start();
}

add_filter('woocommerce_after_checkout_billing_form', 'checkout_form_modify_and_echo', 9999);

function checkout_form_modify_and_echo(){
  $billing_form_html = ob_get_contents();
  ob_end_clean();
  //TODO add processing
  echo $contents;
}