Show/Hide an additional checkout field when ticking a checkbox in Woocommerce - php

My WooCommerce store has 2 different products.
In check out page, if customer buy a normal product, he only fill normal billing fields.
But if this customer buy a second product, the customer must be tick a checkbox that will make appear an additional field.
Anyone could help me solve this problem?

You will have to set in the function your 2 product IDS and to put the correct texts for your custom checkout fields. The code below will conditionally display a checkbox when both products are in cart. When customer will tick the checkbox an additional text field will appear:
// Add fields to the checkout
add_action( 'woocommerce_after_order_notes', 'custom_checkout_fields' );
function custom_checkout_fields( $checkout ) {
// Set HERE below your different product IDs
$product_1_id = 37; // Normal product
$product_2_id = 67; // Specific product (second product)
$has_id_1 = $has_id_2 = false;
// Check if products are in cart
foreach( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['product_id'] == $product_1_id ) $has_id_1 = true;
if( $cart_item['product_id'] == $product_2_id ) $has_id_2 = true;
}
// Display conditionally Custom checkout Fields (when both products are in cart)
if( $has_id_1 && $has_id_2 ){
echo '<div id="custom_checkout_fields">';
// The Check box
woocommerce_form_field( 'my_checkbox', array(
'type' => 'checkbox',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'my_checkbox' ));
// The hidden field
woocommerce_form_field( 'my_text_field', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'my_text_field' ));
echo '</div>';
$required = esc_attr__( 'required', 'woocommerce' );
// The jQuery Script
?>
<script>
jQuery(function($){
// Initialising variables
var textField = 'p#my_text_field_field',
checkboxField = 'input[name^="my_checkbox"]',
required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>'; // Required html
// Initialising at start (Hidding the text field)
$(textField).hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
if( $(textField+' > label > abbr').html() != undefined )
$(textField+' label > .required').remove();
});
// When Checkbox is checked / unchecked (Live event)
$( checkboxField ).click( function() {
if( $(this).prop('checked') == true )
$(textField).show(function(){
$(this).addClass("validate-required");
$(this).removeClass("woocommerce-validated");
$(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
if( $(textField+' > label > abbr').html() == undefined )
$(textField+' label').append(required);
});
else
$(textField).hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
if( $(textField+' > label > abbr').html() != undefined )
$(textField+' label > .required').remove();
});
});
});
</script>
<?php
}
}
// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['my_text_field'] ) ) {
update_post_meta( $order_id, __('My Field'), sanitize_text_field( $_POST['my_text_field'] ) );
}
}
Code goes in function.php file of your active child theme (active theme or in any plugin file).
Tested and Works in WooCommerce 3+
Related official developer documentation: Customizing checkout fields using actions and filters

Related

Want to add a drop down to select local pickup location at checkout [duplicate]

Inspired from Shipping carrier custom fields validation in Woocommerce checkout page answer code, I use following code which displays a select field with shipping companies (this field is displayed only when I choose a specific shipping method):
add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 20, 2 );
function carrier_custom_fields( $method, $index ) {
if( ! is_checkout()) return; // Only on the checkout page
$customer_carrier_method = 'flat_rate:14';
if( $method->id != $customer_carrier_method ) return; // Mostrar solo para "flat_rate:14"
$chosen_method_id = WC()->session->chosen_shipping_methods[ $index ];
// If the chosen shipping method is 'flat_rate: 14', we will show
if($chosen_method_id == $customer_carrier_method ):
echo '<div class="custom-carrier2">';
woocommerce_form_field( 'carrier_name1', array(
'type' => 'select',
'class' => array('carrier_name2-class form-row-wide'),
'label' => __('<strong>Shipping Company</strong>'),
'required' => 'true',
'options' => array(
'1' => '', // no data means that the field is not selected
'Shipping Company 1' => 'Shipping Company 1',
'Shipping Company 2' => 'Shipping Company 2',
'Shipping Company 3' => 'Shipping Company 3',
'Shipping Company 4' => 'Shipping Company 4'
)
), WC()->checkout->get_value( 'carrier_name1' ) );
echo '</div>';
endif;
}
// Validate the custom selection field
add_action('woocommerce_checkout_process', 'carrier_checkout_process');
function carrier_checkout_process() {
if( isset( $_POST['carrier_name1'] ) && empty( $_POST['carrier_name1'] ) )
wc_add_notice( ( "<strong>Shipping Company</strong> it is a required field." ), "error" );
}
// Save custom fields to sort metadata
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
function carrier_update_order_meta( $order_id ) {
if( isset( $_POST['carrier_name1'] ))
update_post_meta( $order_id, 'carrier_name1', sanitize_text_field( $_POST['carrier_name1'] ) );
}
The problem is that it's only displayed on the checkout page and I would like it to show it in cart page, keeping the selected vale on cart page to checkout page.
I think I have found something that says that this transfer of the selected data between the cart and the payment page is done through Ajax, but I am not skilled with Ajax and I don't know how to make that work.
To make that work on cart and checkout pages, you will need some additional code using jQuery, Ajax and WC Session variable:
Final update - To make the code more dynamic, we start with a custom function that will handle all required settings:
// Custom function that handle your settings
function carrier_settings(){
return array(
'targeted_methods' => array('flat_rate:14'), // Your targeted shipping method(s) in this array
'field_id' => 'carrier_name', // Field Id
'field_type' => 'select', // Field type
'field_label' => '', // Leave empty value if the first option has a text (see below).
'label_name' => __("Carrier company","woocommerce"), // for validation and as meta key for orders
'field_options' => array(
// The option displayed at first ( or keep an empty value '',)
__("Choose a carrier company", "woocommerce"),
// The carrier companies below (one by line)
'Company name 1',
'Company name 2',
'Company name 3',
'Company name 4',
),
);
}
Then we can load that settings on any function where it's needed.
Now the Select field with carrier companies displayed for a specific shipping method on cart and checkout pages:
// Display the custom checkout field
add_action( 'woocommerce_after_shipping_rate', 'carrier_company_custom_select_field', 20, 2 );
function carrier_company_custom_select_field( $method, $index ) {
extract( carrier_settings() ); // Load settings and convert them in variables
$chosen = WC()->session->get('chosen_shipping_methods'); // The chosen methods
$value = WC()->session->get($field_id);
$value = WC()->session->__isset($field_id) ? $value : WC()->checkout->get_value('_'.$field_id);
$options = array(); // Initializing
if( ! empty($chosen) && $method->id === $chosen[$index] && in_array($method->id, $targeted_methods) ) {
echo '<div class="custom-carrier">';
// Loop through field otions to add the correct keys
foreach( $field_options as $key => $option_value ) {
$option_key = $key == 0 ? '' : $key;
$options[$option_key] = $option_value;
}
woocommerce_form_field( $field_id, array(
'type' => $field_type,
'label' => '', // Not required if the first option has a text.
'class' => array('form-row-wide ' . $field_id . '-' . $field_type ),
'required' => true,
'options' => $options,
), $value );
echo '</div>';
}
}
The Ajax part: The jQuery sender + PHP WordPress admin Ajax receiver code for the selected carrier company:
// jQuery code (client side) - Ajax sender
add_action( 'wp_footer', 'carrier_company_script_js' );
function carrier_company_script_js() {
// Only cart & checkout pages
if( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ):
// Load settings and convert them in variables
extract( carrier_settings() );
$js_variable = is_cart() ? 'wc_cart_params' : 'wc_checkout_params';
// jQuery Ajax code
?>
<script type="text/javascript">
jQuery( function($){
if (typeof <?php echo $js_variable; ?> === 'undefined')
return false;
$(document.body).on( 'change', 'select#<?php echo $field_id; ?>', function(){
var value = $(this).val();
$.ajax({
type: 'POST',
url: <?php echo $js_variable; ?>.ajax_url,
data: {
'action': 'carrier_name',
'value': value
},
success: function (result) {
console.log(result); // Only for testing (to be removed)
}
});
});
});
</script>
<?php
endif;
}
// The Wordpress Ajax PHP receiver
add_action( 'wp_ajax_carrier_name', 'set_carrier_company_name' );
add_action( 'wp_ajax_nopriv_carrier_name', 'set_carrier_company_name' );
function set_carrier_company_name() {
if ( isset($_POST['value']) ){
// Load settings and convert them in variables
extract( carrier_settings() );
if( empty($_POST['value']) ) {
$value = 0;
$label = 'Empty';
} else {
$value = $label = esc_attr( $_POST['value'] );
}
// Update session variable
WC()->session->set( $field_id, $value );
// Send back the data to javascript (json encoded)
echo $label . ' | ' . $field_options[$value];
die();
}
}
Then on checkout page, the field validation and saving the chosen carrier company to the order:
// Conditional function for validation
function has_carrier_field(){
$settings = carrier_settings();
return array_intersect(WC()->session->get( 'chosen_shipping_methods' ), $settings['targeted_methods']);
}
// Validate the custom selection field
add_action('woocommerce_checkout_process', 'carrier_company_checkout_validation');
function carrier_company_checkout_validation() {
// Load settings and convert them in variables
extract( carrier_settings() );
if( has_carrier_field() && isset( $_POST[$field_id] ) && empty( $_POST[$field_id] ) )
wc_add_notice(
sprintf( __("Please select a %s as it is a required field.","woocommerce"),
'<strong>' . $label_name . '</strong>'
), "error" );
}
// Save custom field as order meta data
add_action( 'woocommerce_checkout_create_order', 'save_carrier_company_as_order_meta', 30, 1 );
function save_carrier_company_as_order_meta( $order ) {
// Load settings and convert them in variables
extract( carrier_settings() );
if( has_carrier_field() && isset( $_POST[$field_id] ) && ! empty( $_POST[$field_id] ) ) {
$order->update_meta_data( '_'.$field_id, $field_options[esc_attr($_POST[$field_id])] );
WC()->session->__unset( $field_id ); // remove session variable
}
}
Display the selected carrier on admin order pages, on customer orders and email notifications:
// Display custom field in admin order pages
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'admin_order_display_carrier_company', 30, 1 );
function admin_order_display_carrier_company( $order ) {
// Load settings and convert them in variables
extract( carrier_settings() );
$carrier = $order->get_meta( '_'.$field_id ); // Get carrier company
if( ! empty($carrier) ) {
// Display
echo '<p><strong>' . $label_name . '</strong>: ' . $carrier . '</p>';
}
}
// Display carrier company after shipping line everywhere (orders and emails)
add_filter( 'woocommerce_get_order_item_totals', 'display_carrier_company_on_order_item_totals', 1000, 3 );
function display_carrier_company_on_order_item_totals( $total_rows, $order, $tax_display ){
// Load settings and convert them in variables
extract( carrier_settings() );
$carrier = $order->get_meta( '_'.$field_id ); // Get carrier company
if( ! empty($carrier) ) {
$new_total_rows = [];
// Loop through order total rows
foreach( $total_rows as $key => $values ) {
$new_total_rows[$key] = $values;
// Inserting the carrier company under shipping method
if( $key === 'shipping' ) {
$new_total_rows[$field_id] = array(
'label' => $label_name,
'value' => $carrier,
);
}
}
return $new_total_rows;
}
return $total_rows;
}
All code goes on functions.php file of your active child theme (or theme). Tested and works.
Other related threads:
Enable delivery time options for a specific state in Woocommerce checkout
Update cart shipping data with AJAX in WooCommerce
On cart page (for chosen specific shipping method):
On checkout page page (for chosen specific shipping method):
On customer orders (email notifications and admin order pages too):

Select field that add / remove a custom fee in WooCommerce [duplicate]

I am using Update fee dynamically based on radio buttons in Woocommerce checkout answer code solution that worked very well for me to add checkbox fields with a different price for each one, and the price changes are reflected in the checkout.
But I need some help: When I select a type of packaging with additional tax, it appears in the backend in the order area, but only shows the price, and I would like to show the title as well.
The checkbox options have:
'options' => array (
'bag' => __ ('In a bag' .wc_price (3.00), $ domain),
    'box' => __ ('In a gift box' .wc_price (9.00), $ domain),
),
How to make it show the name on the order?
Also if it's possible to change the checkboxes to select field instead?
I have made some changes to the original code that will:
Display a custom select field (instead of radio buttons input fields)
Display a custom error notice if customer has not selected a packing option
Display the selected packing type everywhere (on orders and email notifications)
The code:
// Add a custom select fields for packing option fee
add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_form_packing_addition', 20 );
function checkout_shipping_form_packing_addition( ) {
$domain = 'woocommerce';
echo '<tr class="packing-select"><th>' . __('Packing options', $domain) . '</th><td>';
$chosen = WC()->session->get('chosen_packing');
// Add a custom checkbox field
woocommerce_form_field( 'chosen_packing', array(
'type' => 'select',
'class' => array( 'form-row-wide packing' ),
'options' => array(
'' => __("Choose a packing option ...", $domain),
'bag' => sprintf( __("In a bag (%s)", $domain), strip_tags( wc_price(3.00) ) ),
'box' => sprintf( __("In a gift box (%s)", $domain), strip_tags( wc_price(9.00) ) ),
),
'required' => true,
), $chosen );
echo '</td></tr>';
}
// jQuery - Ajax script
add_action( 'wp_footer', 'checkout_shipping_packing_script' );
function checkout_shipping_packing_script() {
// Only checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
WC()->session->__unset('chosen_packing');
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'select#chosen_packing', function(){
var p = $(this).val();
console.log(p);
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'woo_get_ajax_data',
'packing': p,
},
success: function (result) {
$('body').trigger('update_checkout');
console.log('response: '+result); // just for testing | TO BE REMOVED
},
error: function(error){
console.log(error); // just for testing | TO BE REMOVED
}
});
});
});
</script>
<?php
endif;
}
// Php Ajax (Receiving request and saving to WC session)
add_action( 'wp_ajax_woo_get_ajax_data', 'woo_get_ajax_data' );
add_action( 'wp_ajax_nopriv_woo_get_ajax_data', 'woo_get_ajax_data' );
function woo_get_ajax_data() {
if ( isset($_POST['packing']) ){
$packing = sanitize_key( $_POST['packing'] );
WC()->session->set('chosen_packing', $packing );
echo json_encode( $packing );
}
die(); // Alway at the end (to avoid server error 500)
}
// Add a custom dynamic packaging fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee', 20, 1 );
function add_packaging_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$domain = "woocommerce";
$packing_fee = WC()->session->get( 'chosen_packing' ); // Dynamic packing fee
if ( $packing_fee === 'bag' ) {
$label = __("Bag packing fee", $domain);
$cost = 3.00;
} elseif ( $packing_fee === 'box' ) {
$label = __("Gift box packing fee", $domain);
$cost = 9.00;
}
if ( isset($cost) )
$cart->add_fee( $label, $cost );
}
// Field validation, as this packing field is required
add_action('woocommerce_checkout_process', 'packing_field_checkout_process');
function packing_field_checkout_process() {
// Check if set, if its not set add an error.
if ( isset($_POST['chosen_packing']) && empty($_POST['chosen_packing']) )
wc_add_notice( __( "Please choose a packing option...", "woocommerce" ), 'error' );
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
The error message when customer hasn't chosen a packing option:

show a required text field when a checkbox is checked in WooCommerce checkout

I working on a solution in woocommerce registration with custom checkbox.
The plan is when somebody select the custom checkbox an additional textfield opens and have to be required.
The part that works:
// add the special customer role
add_action('admin_init', 'uiwc_new_role');
function uiwc_new_role() {
add_role(
'kundenkarte',
"Kundenkarte",
array(
'read' => true,
'delete_posts' => false
)
);
}
add_action( 'woocommerce_after_order_notes', 'custom_checkout_field_with_wholesale_option' );
function custom_checkout_field_with_wholesale_option( $checkout ) {
if( current_user_can( 'wholesale_customer' ) ) return; // exit if it is "wholesale customer"
echo '<div id="wholesale_checkbox_wrap">';
woocommerce_form_field('wholesale_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Do you have a Customer Card?'),
'placeholder' => __('card'),
'required' => false,
'value' => true
), '');
echo '</div>';
}
// Conditionally change customer user role
add_action( 'woocommerce_checkout_update_order_meta', 'wholesale_option_update_user_meta' );
function wholesale_option_update_user_meta( $order_id ) {
if ( isset($_POST['wholesale_checkbox']) ) {
$user_id = get_post_meta( $order_id, '_customer_user', true ); // Get user ID
if( $user_id > 0 ){
$user = new WP_User($user_id);
$user->remove_role('customer');
$user->add_role('kundenkarte');
}
}
}
My PHP knowledge is very low.
Updated (tested and works)
With the code below, when your checkbox is checked a custom required text field is visible (with validation and save as user meta data and order meta data):
add_action( 'woocommerce_after_order_notes', 'custom_checkout_field_with_wholesale_option' );
function custom_checkout_field_with_wholesale_option( $checkout ) {
if( current_user_can( 'wholesale_customer' ) ) return; // exit if it is "wholesale customer"
echo '<style> #wholesale_card_field.hidden { display:none; }</style>
<div id="wholesale_checkbox_wrap">';
woocommerce_form_field('wholesale_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Do you have a Customer Card?'),
'placeholder' => __('card'),
'required' => false,
'value' => true
), '');
woocommerce_form_field('wholesale_card', array(
'type' => 'text',
'class' => array('hidden'),
'placeholder' => __('Customer card Id'),
'required' => true,
), '');
echo '</div>';
?>
<script>
jQuery(function($){
$('#wholesale_checkbox_field input').click(function(){
if( $(this).is(':checked')) {
$('#wholesale_card_field').css('display', 'none').removeClass('hidden').show();
} else if ( ! $(this).is(':checked') && $('#wholesale_card_field').css('display') !== 'none' ) {
$('#wholesale_card_field').hide();
}
});
});
</script>
<?php
}
// Validation
add_action( 'woocommerce_checkout_process', 'wholesale_option_validation' );
function wholesale_option_validation() {
if ( isset($_POST['wholesale_checkbox']) && isset($_POST['wholesale_card']) && empty($_POST['wholesale_card']) ) {
wc_add_notice( __("Please fill in your customer card Id"), "error" );
}
}
// Conditionally change customer user role and add customer card as order and user meta
add_action( 'woocommerce_checkout_update_order_meta', 'wholesale_option_update_meta' );
function wholesale_option_update_meta( $order_id ) {
if ( isset($_POST['wholesale_checkbox']) ) {
$user_id = get_post_meta( $order_id, '_customer_user', true ); // Get user ID
if( $user_id > 0 ){
$user = new WP_User($user_id);
$user->remove_role('customer');
$user->add_role('kundenkarte');
}
// Add customer card Id as order metadata
if ( isset($_POST['wholesale_card']) ) {
update_post_meta( $order_id, 'wholesale_card', sanitize_text_field( $_POST['wholesale_card'] ) );
if( $user_id > 0 )
update_user_meta( $user_id, 'wholesale_card', sanitize_text_field( $_POST['wholesale_card'] ) );
}
}
}
// Display customer card on admin order edit page under billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_wholesale_option_admin_order', 10, 1 );
function display_wholesale_option_admin_order( $order ){
echo '<p><strong>'.__('Card Id').':</strong> ' . $order->get_meta( 'wholesale_card' ) . '</p>';
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Override woocommerce cart item price with product custom field value

In woocommerce I am using Advanced Custom Fields and trying to get a custom field value in each product as price instead of the default product price. this custom field is called 'custom_price'.
How can I change this hard coded value to use that instead?
Here is my code:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price'
);
function add_custom_price( $cart_object ) {
$custom_price = 10;
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->set_price($custom_price);
}
}
Update 3: Here is the complete solution with all custom fields and the cart item price change.
You will need to add some jQuery code to make your product price calculation, display calculated price on product page and set this calculated price on a hidden field.
Once product will be added to cart, the code will catch the calculated price and will set it in the corresponding cart item…
The code:
// The product custom field - Frontend
add_action( 'woocommerce_before_add_to_cart_button', 'custom_discount_price_product_field' );
function custom_discount_price_product_field() {
global $product;
$curs = get_woocommerce_currency_symbol(); // Currency symbol
// Get the discounted value (from product backend)
$discount = (float) get_post_meta( $product->get_id(), '_price_discount', true );
// jQuery will get the discount here for calculations
echo '<input type="hidden" name="price_discount" value="'.$discount.'">';
echo '<div>';
woocommerce_form_field( 'select_price', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Discount'),
'options' => array(
'' => __( 'Select your discount', 'woocommerce' ),
'5' => $curs . '5',
'10' => $curs . '10',
'15' => $curs . '15',
'20' => $curs . '20',
),
), '' );
// This field will be used to send the calculated price
// jQuery will set the calculated price on this field
echo '<input type="hidden" name="custom_price" value="52">'; // 52 is a fake value for testing purpose
echo '</div><br>';
// BELOW your jquery code to calculate price and update "custom_price" hidden field
?>
<script type="text/javascript">
jQuery( function($){
// Here
});
</script>
<?php
}
// Add a custom field to product in backend
add_action( 'woocommerce_product_options_pricing', 'add_field_product_options_pricing' );
function add_field_product_options_pricing() {
global $post;
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => '_price_discount',
'label' => __('Discount price', 'woocommerce') . ' (%)',
'placeholder' => __('Set the Discount price…', 'woocommerce'),
'description' => __('Enter the custom value here.', 'woocommerce'),
'desc_tip' => 'true',
));
echo '</div>';
}
// Save product custom field to database when submitted in Backend
add_action( 'woocommerce_process_product_meta', 'save_product_options_custom_fields', 30, 1 );
function save_product_options_custom_fields( $post_id ){
// Saving custom field value
if( isset( $_POST['_price_discount'] ) ){
update_post_meta( $post_id, '_price_discount', sanitize_text_field( $_POST['_price_discount'] ) );
}
}
// Add custom calculated price conditionally as custom data to cart items
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_price_to_cart_item_data', 20, 2 );
function add_custom_price_to_cart_item_data( $cart_item_data, $product_id ){
if( ! isset($_POST['custom_price']) )
return $cart_item_data;
$cart_item_data['custom_price'] = (float) sanitize_text_field( $_POST['custom_price'] );
$cart_item_data['unique_key'] = md5( microtime() . rand() ); // Make each item unique
return $cart_item_data;
}
// Set conditionally a custom item price
add_action('woocommerce_before_calculate_totals', 'set_cutom_cart_item_price', 20, 1);
function set_cutom_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
if ( isset( $cart_item['custom_price'] ) )
$cart_item['data']->set_price( $cart_item['custom_price'] );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works (but you will need to make your own calculations with jquery)

Required custom WooCommerce checkout fields don't validate entered value

I'm adding WooCommerce custom checkout fields in a Storefront child theme functions.php file.
They have a "required" attribute.
The aim is to have these fields appear at the top of the page, before the billing fields.
when clicking the submit button to proceed to payment, I'm getting the required custom field validation error ('please fill in your name') and can't continue with payment, even though filling the field with valid data.
Any clue how to fix this or where to start debugging ?
here is the code in functions.php:
add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' );
function my_custom_checkout_fields( $checkout ) {
echo '<div id="my_custom_checkout_field" class="col4-set"><h2>' . __('name') . '</h2>';
woocommerce_form_field( 'developer_name', array(
'type' => 'text',
'class' => array('developer_name-class form-row form-row-first'),
'label' => __('name'),
'placeholder' => __('fill in your name'),
'required' => true,
), $checkout->get_value( 'developer_name' ));
echo '</div>';
}
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['developer_name'] )
wc_add_notice( __( 'please fill in your name' ), 'error' );
}
I tried the following but none of them helped:
1. changing:
if ( ! $_POST['developer_name'] )
to
if ( empty( $_POST['developer_name']) )
2. changing the trigger from:
add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' );
to
add_action( 'woocommerce_after_checkout_form', 'my_custom_checkout_fields' );
3. updating to latest Woocomerce 3.0.5 version
I'm running Wordpress 4.7.4
additional related active plugins:
Uni CPO - WooCommerce Options and Price Calculation Formulas
As you can read in woocommerce_before_checkout_form hook, it's before checkout form (so outside the checkout form). For this reason this custom field can't work in this hook.
You can use instead woocommerce_checkout_update_order_meta action hook, making some little changes in your code as there is no $checkout argument available in it.
This will display the field "at the top of the page, before the billing fields"…
So your complete code should be now:
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_checkout_before_customer_details', 'my_custom_checkout_fields' );
function my_custom_checkout_fields() {
echo '<div id="my_custom_checkout_field" class="col4-set"><h2>' . __('name') . '</h2>';
woocommerce_form_field( 'developer_name', array(
'type' => 'text',
'class' => array('developer_name-class form-row form-row-first'),
'label' => __('name'),
'placeholder' => __('fill in your name'),
'required' => true,
), WC()->checkout->get_value( 'developer_name' ));
echo '</div>';
}
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['developer_name'] )
wc_add_notice( __( 'Please fill in your name.' ), 'error' );
}
// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['developer_name'] ) ) {
update_post_meta( $order_id, 'Developer name', sanitize_text_field( $_POST['developer_name'] ) );
}
}
// Display the custom-field in orders view
add_action( 'woocommerce_order_details_after_customer_details', 'display_my_custom_field_in_orde_details', 10, 1 );
function display_my_custom_field_in_orde_details( $order ) {
$developer_name = get_post_meta( $order->get_id(), 'Developer name', true );
if ( ! empty( $developer_name ) ):
?>
<table class="woocommerce-table woocommerce-table--customer-details shop_table customer_details">
<tbody><tr>
<th>Developer name:</th>
<td><?php echo $developer_name; ?></td>
</tr></tbody>
</table>
<?php
endif;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce version 3.0+
You try this code. Tested Ok
add_action( 'woocommerce_billing_fields', 'my_custom_checkout_fields' );
function my_custom_checkout_fields( $fields ) {
$fields['billing_developer_name'] = array(
'label' => __('Developer name', 'woocommerce'),
'placeholder' => _x('Developer name', 'placeholder', 'woocommerce'),
'required' => TRUE,
'clear' => false,
'type' => 'text',
'class' => array('my-css')
);
return $fields;
}
You can arrange it with this snippet
add_filter("woocommerce_checkout_fields", "order_fields");
function order_fields($fields) {
$order = array(
"billing_developer_name",
"billing_first_name",
"billing_last_name",
"billing_company",
"billing_address_1",
"billing_address_2",
"billing_postcode",
"billing_country",
"billing_email",
"billing_phone"
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
See screenshot
**/* If You Have Created Your Custom Field at the checkout page */**
add_action( 'woocommerce_after_checkout_validation', 'shipping_time_optionss', 9999, 2);
function shipping_time_optionss( $fields, $errors ){
// if any validation errors
if ( empty( $_POST['woo_shipping_time'] ) ) {
$errors->add( 'woocommerce_password_error', __( 'Please Select Shipping Time Option.' ) );
} `enter code here`
}

Categories