Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default values in woocommerce checkout

I have just created a page with order review using the following code

<form name="checkout" method="post" class="checkout woocommerce-checkout" action="<?php echo esc_url( $get_checkout_url ); ?>" enctype="multipart/form-data">
<h3 id="order_review_heading"><?php _e( 'Your order', 'woocommerce' ); ?></h3>

    <?php do_action( 'woocommerce_checkout_before_order_review' ); ?>

    <div id="order_review" class="woocommerce-checkout-review-order">
        <?php do_action( 'woocommerce_checkout_order_review' ); ?>
    </div>

    <?php do_action( 'woocommerce_checkout_after_order_review' ); ?></div>
    </form>
    <?php do_action( 'woocommerce_after_checkout_form', $checkout ); ?>
      </div>

I have set the default value in function.php as follow :

function overridefields($fields)
{
    global $wpdb;
    session_start();
    //print_r($_SESSION);
    //$fields['billing']['billing_delivery_date'] = array('label'=>'Delivery Date','Placeholder'=>'Date','required'=>'false','readonly'=>'true');
    $select_r = $wpdb->get_results("select * from `register` where `userid`='18'");
    //print_r($select_r);
    $fields['billing']['billing_first_name']['default']=$_SESSION['f_name'];
    $fields['billing']['billing_last_name']['default']=$_SESSION['l_name'];
    $fields['billing']['billing_address_1']['default']=$_SESSION['address'];
    $fields['billing']['billing_address_2']['default']='';
    $fields['billing']['billing_city']['default']=$_SESSION['city'];
    //$fields['billing']['billing_state']['default']=$select_r[0]->state;
    $fields['billing']['billing_email']['default']=$_SESSION['email'];
    $fields['billing']['billing_phone']['default']=$_SESSION['phone'];
    $fields['billing']['billing_postcode']['default']=$_SESSION['pincode'];
    //$fields['billing']['billing_delivery_date']['default']=$_SESSION['delivdate'];
    return $fields;
}
add_filter('woocommerce_checkout_fields','overridefields');

but when I click on checkout button then i got the error

Country is a required field. First Name is a required field. Last Name is a required field. Address is a required field. Town / City is a required field. State / County is a required field. Postcode / Zip is a required field. Email Address is a required field. Phone is a required field.

How can I set default value in woocommerce checkout page

like image 290
Rohitashv Singhal Avatar asked Aug 19 '15 10:08

Rohitashv Singhal


People also ask

How do I change the default checkout page in WooCommerce?

First, go to WooCommerce » Settings and click on the 'Advanced' tab. Then, select the new WooCommerce checkout page URL from the drop down menu. Make sure to click 'Save changes' at the bottom of the screen.

How do I change the WooCommerce checkout code?

Moving, Adding, or Removing Checkout Fields After installing and activating this plugin, you can visit WooCommerce => Checkout Fields to begin editing. To edit a field, simply change the desired value and save. To add a field, click Add Field, and to remove, check the field and click Disable/Remove.


2 Answers

This code I found might be useful:

<?php
  add_filter( 'woocommerce_checkout_fields' , 'default_values_checkout_fields' );
  function default_values_checkout_fields( $fields ) {
    // You can use this for postcode, address, company, first name, last name and such. 
    $fields['billing']['billing_city']['default'] = 'SomeCity';
    $fields['shipping']['shipping_city']['default'] = 'SomeCity';
         return $fields;
  }
?>

Then use CSS to hide the inputs.

References

  1. Field names can be found here.
  2. For main function woocommerce_form_field()
  3. GitHub reference
like image 183
Sadoo Avatar answered Nov 10 '22 13:11

Sadoo


Use default and custom_attributes in the field definition to apply custom attributes to billing/shipping address and account fields: https://github.com/woocommerce/woocommerce/blob/master/includes/wc-template-functions.php#L2510-L2514

add_filter('woocommerce_checkout_fields', 'my_woocommerce_checkout_fields');
add_filter('woocommerce_billing_fields', 'my_woocommerce_billing_fields');

function my_woocommerce_checkout_fields($fields) {
  $fields['billing'] = my_woocommerce_billing_fields($fields['billing']);
  return $fields;
}

function my_woocommerce_billing_fields($fields) {
  // Note: Default value is only used if the user account does
  // not have any value for the meta field yet. (Empty value is
  // also a value.) Ensure that the value is set correctly.
  $fields['billing_city']['default'] = 'SomeCity';

  // Disable the field in the form.
  // (Note: Does not prevent different values from being posted.)
  $fields['billing_city']['custom_attributes']['readonly'] = TRUE;
  $fields['billing_city']['custom_attributes']['disabled'] = TRUE;

  return $fields;
}
like image 41
sun Avatar answered Nov 10 '22 13:11

sun