php - Retrieve product custom field values in checkout custom fields -
i have form in single product page user insert informations (name, phone, country ,age ...) need display information in billing details .
so, did added 2 fields form in single product , here code :
<div class="col-md-6"> <div class="form-group"> <input class="form-control alt" name="billing_vol" type="text" placeholder="<?php esc_html_e( 'n° vol : *', 'rentcar' ); ?>" > </div> </div> <div class="form-group"> <input class="form-control alt" type="text" name="billing_cli_age" placeholder="<?php esc_html_e( 'age *', 'rentcar' ); ?>" " > </div><div class="col-md-6"> <div class="form-group"> <?php global $woocommerce; $countries_obj = new wc_countries(); $countries = $countries_obj->__get('countries'); ?> <?php woocommerce_form_field('billing_country', array( 'type' => 'select', 'class' => array( 'chzn-drop' ), 'placeholder' => __('country'), 'options' => $countries ) ); ?> </div> </div>
then did same thing in checkout page in billing detail form, have added 2 fields. here code :
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); function custom_override_checkout_fields( $fields ) { $fields['billing']['billing_cli_age'] = array( 'label' => __('age ', 'woocommerce'), 'placeholder' => _x('age ', 'placeholder', 'woocommerce'), 'required' => false, 'class' => array('form-row-wide'), 'clear' => true ); $fields['billing']['billing_vol'] = array( 'label' => __('n° vol', 'woocommerce'), 'placeholder' => _x('n° vol', 'placeholder', 'woocommerce'), 'required' => false, 'class' => array('form-row-wide'), 'clear' => true ); return $fields; }
now i'm stuck , don't know how send information added user in form (that have in single product) billing details form in checkout page.
how can achieve this?
thanks.
here complete functional code product custom fields submitted values when add-to-cart filled our checkout custom fields.
the first function testing purpose. product custom-fields code should inside add-to-cart form, working.
add_action( 'woocommerce_before_add_to_cart_button', 'action_before_add_to_cart_button',20 ); function action_before_add_to_cart_button() { ?> <div class="col-md-6"> <div> <input class="form-control alt" name="billing_vol" type="text" placeholder="<?php esc_html_e( 'n° vol : *', 'rentcar' ); ?>"> </div> <div> <input class="form-control alt" type="text" name="billing_cli_age" placeholder="<?php esc_html_e( 'age *', 'rentcar' ); ?>"> </div> <div class="form-group"> <?php // getting countries (simplified) $countries = wc()->countries->get_countries(); woocommerce_form_field('billing_country', array( 'type' => 'select', 'class' => array( 'chzn-drop' ), 'placeholder' => __('country'), 'options' => $countries ) ); ?> </div> </div> <?php }
here missing hooked function, pass cart product custom fields values:
// store data fields cart add_action( 'woocommerce_add_cart_item_data', 'action_save_my_custom_product_fields', 10, 2 ); function action_save_my_custom_product_fields( $cart_item_data, $product_id ) { $bool = false; $data = array(); if( isset( $_request['billing_vol'] ) ) { $cart_item_data['custom_data']['billing_vol'] = $_request['billing_vol']; $data['billing_vol'] = $_request['billing_vol']; $bool = true; } if( isset( $_request['billing_cli_age'] ) ) { $cart_item_data['custom_data']['billing_cli_age'] = $_request['billing_cli_age']; $data['billing_cli_age'] = $_request['billing_cli_age']; $bool = true; } if( isset( $_request['billing_country'] ) ) { $cart_item_data['custom_data']['billing_country'] = $_request['billing_country']; $data['billing_country'] = $_request['billing_country']; $bool = true; } if( $bool ) { // below statement make sure every add cart action unique line item $unique_key = md5( microtime().rand() ); $cart_item_data['custom_data']['unique_key'] = $unique_key; $data['unique_key'] = $unique_key; wc()->session->set( 'custom_data', $data ); } return $cart_item_data; }
as can have multiple cart items, need break loop, first item custom values:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); function custom_override_checkout_fields( $fields ) { // break loop custom data 1 cart item foreach(wc()->cart->get_cart() $cart_item) break; $data = $cart_item['custom_data']; // countries: getting countries (simplified) $countries = wc()->countries->get_countries(); $billing_country_key = $data['billing_country']; $billing_country_value = $countries[$billing_country_key]; $fields['billing']['billing_cli_age'] = array( 'type' => 'text', 'label' => __('age ', 'woocommerce'), 'placeholder' => _x('age ', 'placeholder', 'woocommerce'), 'required' => false, 'class' => array('form-row-wide'), 'clear' => true, 'default' => $data['billing_cli_age'], ); $fields['billing']['billing_vol'] = array( 'type' => 'text', 'label' => __('n° vol', 'woocommerce'), 'placeholder' => _x('n° vol', 'placeholder', 'woocommerce'), 'required' => false, 'class' => array('form-row-wide'), 'clear' => true, 'default' => $data['billing_vol'], ); $fields['billing']['billing_country'] = array( 'type' => 'select', 'default' => $billing_country_key, 'options' => array($billing_country_key => $billing_country_value), ); return $fields; }
the code goes in function.php file of active child theme (or theme) or in plugin file.
this code tested , works.
for country field quiet complicated work javascript/ajax involved in it... unique "select" "option" works, multiple select options doesn't.
Comments
Post a Comment