How To Add In Cart Page Using Custom Form For Single Product In WooCommerce

Here we learn about how to add in cart page using custom form for single product in WooCommerce.

You can create a custom HTML form for submitting user details. this form is put on the page.

<div class="product-form">
  <form action="#" enctype="multipart/form-data" method="POST">
  <input type="hidden" name="product_id" value="58" />
  <input type="hidden" name="product_name" value="Test Product" />	
  <input name="name" required="" type="text" placeholder="Name*" />
  <input name="surname" required="" type="text" placeholder="Surname*" />
  <input name="address" required="" type="text" placeholder="Address*" />
  <input name="postcode" required="" type="text" placeholder="Post Code*" />
  <input name="city" required="" type="text" placeholder="City*" />
  <input name="phone" required="" type="text" placeholder="Phone*" />
  <input name="email" required="" type="email" placeholder="Email Address*" />
  <div class="clear"></div>
  <button>Get Now</button>
</form></div>

There are two hidden fields:

product_id – This field adds value for product id.
product_name – This field adds value for a product name.

Custom code copied and paste into your child theme’s functions.php file.

<?php
add_action('wp', 'single_product_checkout');
function single_product_checkout() {
  if ( isset( $_POST['product_id'] ) && $_POST['product_id'] != '' && isset( $_POST['product_name']) && ( $_POST['product_name'] == 'Test Product' ) ) {
    WC()->cart->empty_cart();
    WC()->cart->add_to_cart( $_POST['product_id'], 1 );			
?>
    <script>
      var site_url 	 = '<?php echo site_url(checkout) ?>';				
      var product_name   = '<?php echo $_POST["product_name"] ?>';
      var name 		 = '<?php echo $_POST["name"] ?>';
      var surname 	 = '<?php echo $_POST["surname"] ?>';
      var address 	 = '<?php echo $_POST["address"] ?>';
      var city 	 	 = '<?php echo $_POST["city"] ?>';
      var postcode 	 = '<?php echo $_POST["postcode"] ?>';
      var phone 	 = '<?php echo $_POST["phone"] ?>';
      var email 	 = '<?php echo $_POST["email"] ?>';
      
      if( product_name == 'Test Product' ){		
    var data_site_url_enc = '&page='+product_name+'&first_name='+name+'&last_name='+surname+'&address='+address+'&city='+city+'&postcode='+postcode+'&phone='+phone+'&email='+email;		
    window.location.href = site_url+'?data=1' + encodeURIComponent( data_site_url_enc );        
      }else{
        window.location.href = site_url+'?page='+product_name;
      }
    </script>
<?php
  }
}

/* checkout page field override */
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
  unset($fields['billing']['billing_company']);
  unset($fields['billing']['billing_address_2']);
  unset($fields['billing']['billing_state']);
  unset($fields['order']['order_comments']);

  if( $_GET['data'] == 1 ){		
    $data_array = explode( '&',$_GET['data'] );
    foreach( $data_array  as $user_data ){
      $user_info = explode( '=', $user_data );
      if( $user_info[0] == 'first_name' ){
        $fields['billing']['billing_first_name']['default'] = $user_info[1];
      }
      if( $user_info[0] == 'last_name' ){
        $fields['billing']['billing_last_name']['default'] = $user_info[1];
      }
      if( $user_info[0] == 'address' ){
        $fields['billing']['billing_address_1']['default'] = $user_info[1];
      }
      if( $user_info[0] == 'city' ){
        $fields['billing']['billing_city']['default'] = $user_info[1];
      }
      if( $user_info[0] == 'postcode' ){
        $fields['billing']['billing_postcode']['default'] = $user_info[1];
      }
      if( $user_info[0] == 'phone' ){
        $fields['billing']['billing_phone']['default'] = $user_info[1];
      }
      if( $user_info[0] == 'email' ){
        $fields['billing']['billing_email']['default'] = $user_info[1];
      }
    }	
  }
  return $fields;
}
?>

 

WC()->cart->empty_cart() – It’s user for Empties the cart.
WC()->cart->add_to_cart() – If the cart was empty I add 1 products to the cart programmatically.

The function “WC()->cart->add_to_cart();” accepts 5 parameters:

syntax:

<?php WC()->cart->add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array() ); ?>

Let’s take a look at those parameters:

$product_id – This is product ID.
$quantity – This is the quantity of this product which you want to add to the cart.
$variation_id – The variation is child of our parent product.
$variation – This is an array of the select attributes. This is used for displaying the variation data in the cart and on the order.

OUTPUT:

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories