Make Woocommerce checkout phone field optional based on shipping country - php

In Woocommerce checkout, I am trying to make the phone field not required for specific shipping countries. Based on "Make checkout phone field optional for specific countries in WooCommerce" answer code, which works fine, I have tried to make some changes to get this code working for the shipping country rather than billing country.
After a lot of tries, I wasn't able to figure out how to make it work.
Any help will be awesome and greatly appreciated.

The following code will make billing phone field required only for specific "Shipping" countries.
Since Woocommerce version 3.4+, things have changed a bit on Woocommerce form fields, so additional functions and code where required.
Also I have extended the code to handle the phone field behavior in My Account > Edit Addresses, where customer can make changes to his account data.
Here is the complete code (define your country codes in the first function):
// SETTINGS: The countries codes (2 capital letters) in the array
function defined_countries_for_phone_field(){
return array( 'UK', 'BE', 'GE', 'IT', 'ES' );
}
// Remove "(optional)" from non required "Billing phone" field
add_filter( 'woocommerce_form_field' , 'remove_checkout_optional_fields_label', 10, 4 );
function remove_checkout_optional_fields_label( $field, $key, $args, $value ) {
// Get the defined countries codes
$countries = defined_countries_for_phone_field();
// Get Customer shipping country
$shipping_country = WC()->customer->get_shipping_country();
// Only on checkout page and My account > Edit address for billing phone field
if( 'billing_phone' === $key && ( ( is_wc_endpoint_url( 'edit-address' )
&& ! in_array($shipping_country, $countries) ) || is_checkout() ) ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
// Make the billing phone field optional (by default)
add_filter( 'woocommerce_billing_fields', 'filter_billing_phone_field', 10, 1 );
function filter_billing_phone_field( $fields ) {
// Get the defined countries codes
$countries = defined_countries_for_phone_field();
// Get Customer shipping country
$shipping_country = WC()->customer->get_shipping_country();
// Only on checkout page and My account > Edit address
if ( ( is_wc_endpoint_url( 'edit-address' )
&& ! in_array($shipping_country, $countries) ) || is_checkout() )
$fields['billing_phone']['required'] = false;
return $fields;
}
// Real time shipping country selection actions
add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_and_fields', 10, 1 );
function custom_checkout_scripts_and_fields( $checkout ) {
$required = esc_attr__( 'required', 'woocommerce' );
// Get the defined countries codes
$countries = defined_countries_for_phone_field();
// Hidden field for the phone number validation
echo '<input type="hidden" name="billing_phone_check" id="billing_phone_check" value="0">';
$countries_str = "'".implode( "', '", $countries )."'"; // Formatting countries for jQuery
?>
<script type="text/javascript">
(function($){
var required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>',
countries = [<?php echo $countries_str; ?>],
location = $('#shipping_country option:selected').val(),
phoneCheck = 'input#billing_phone_check',
phoneField = '#billing_phone_field';
function actionRequire( actionToDo='yes', selector='' ){
if ( actionToDo == 'yes' ) {
$(selector).addClass("validate-required");
$(selector+' label').append(required);
} else {
$(selector).removeClass("validate-required");
$(selector+' label > .required').remove();
}
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Default value Once DOM is loaded (with a 300 ms delay)
setTimeout( function(){
actionRequire( 'no', phoneField );
if( $.inArray( location, countries ) >= 0 && $(phoneCheck).val() == '0' ){
actionRequire( 'yes',phoneField );
$(phoneCheck).val('1');
}
}, 300 );
// Live value
$( 'form.checkout' ).on( 'change', '#shipping_country', function(){
var location = $('#shipping_country option:selected').val();
if ( $.inArray( location, countries ) >= 0 && $(phoneCheck).val() == 0 ) {
actionRequire( 'yes' ,phoneField );
$(phoneCheck).val('1');
} else if ( $(phoneCheck).val() == 1 ) {
actionRequire( 'no' ,phoneField );
$(phoneCheck).val('0');
}
});
})(jQuery);
</script>
<?php
}
// Phone number validation, when the field is required
add_action('woocommerce_checkout_process', 'billing_phone_field_process');
function billing_phone_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['billing_phone'] && $_POST['billing_phone_check'] == '1' )
wc_add_notice( __( 'Please enter a number phone.' ), 'error' );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works in WooCommerce from version 3.4 and above.
Related:
Make checkout phone field optional for specific countries in WooCommerce
Remove "(optional)" text from checkout fields in Woocommerce 3.4+

Huge thanks to #LoicTheAztec for the original answer, however the solution now gives erratic results and just alternately switches the phone field between required and optional states (on / off).
The original answer also does not take into account customers who are using the billing address only and have not entered a separate delivery or shipping address.
Please see the updated version below for 2019
// SETTINGS: The countries codes (2 capital letters) in the array
function defined_countries_for_phone_field(){
return array( 'GB', 'JE', 'GG', 'IM' );
}
// Remove "(optional)" from non required "Billing phone" field
add_filter( 'woocommerce_form_field' , 'remove_checkout_optional_fields_label', 10, 4 );
function remove_checkout_optional_fields_label( $field, $key, $args, $value ) {
// Get the defined countries codes
$countries = defined_countries_for_phone_field();
// Get Customer shipping country
$shipping_country = WC()->customer->get_shipping_country();
// Only on checkout page and My account > Edit address for billing phone field
if( 'billing_phone' === $key && ( ( is_wc_endpoint_url( 'edit-address' )
&& in_array($shipping_country, $countries) ) || is_checkout() ) ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
// Make the billing phone field optional (by default)
add_filter( 'woocommerce_billing_fields', 'filter_billing_phone_field', 10, 1 );
function filter_billing_phone_field( $fields ) {
// Get the defined countries codes
$countries = defined_countries_for_phone_field();
// Get Customer shipping country
$shipping_country = WC()->customer->get_shipping_country();
// Only on checkout page and My account > Edit address
if ( ( is_wc_endpoint_url( 'edit-address' )
&& in_array($shipping_country, $countries) ) || is_checkout() )
$fields['billing_phone']['required'] = false;
return $fields;
}
// Real time shipping country selection actions
add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_and_fields', 10, 1 );
function custom_checkout_scripts_and_fields( $checkout ) {
$required = esc_attr__( 'required', 'woocommerce' );
// Get the defined countries codes
$countries = defined_countries_for_phone_field();
// Hidden field for the phone number validation
echo '<input type="hidden" name="billing_phone_check" id="billing_phone_check" value="0">';
$countries_str = "'".implode( "', '", $countries )."'"; // Formatting countries for jQuery
?>
<script type="text/javascript">
(function($){
var required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>';
var countries = [<?php echo $countries_str; ?>];
if($('.shipping_address').is(':visible')) {
// ship to different country selected
var selectedcountry = $('#shipping_country option:selected').val();
} else {
var selectedcountry = $('#billing_country option:selected').val();
}
//var selectedcountry = $('#shipping_country option:selected').val();
var phoneCheck = 'input#billing_phone_check';
var phoneField = '#billing_phone_field';
function actionRequire( actionToDo='yes', selector='' ){
if ( actionToDo == 'yes' ) {
$(selector).addClass("validate-required");
$(selector+' label > .required').remove();
$(selector+' label').append(required);
} else {
$(selector).removeClass("validate-required");
$(selector+' label > .required').remove();
}
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Default value Once DOM is loaded (with a 300 ms delay)
setTimeout( function(){
if($('.shipping_address').is(':visible')) {
// ship to different country selected
var selectedcountry = $('#shipping_country option:selected').val();
} else {
var selectedcountry = $('#billing_country option:selected').val();
}
actionRequire( 'no', phoneField );
if( $.inArray( selectedcountry, countries ) == -1){
actionRequire( 'yes',phoneField );
$(phoneCheck).val('1');
}
}, 300 );
// Live value
$( 'form.checkout' ).on( 'change', '#billing_country, #shipping_country, #ship-to-different-address-checkbox', function(){
setTimeout( function(){
if($('.shipping_address').is(':visible')) {
// ship to different country selected
var selectedcountry = $('#shipping_country option:selected').val();
} else {
var selectedcountry = $('#billing_country option:selected').val();
}
if ( $.inArray( selectedcountry, countries ) == -1) {
actionRequire( 'yes' ,phoneField );
$(phoneCheck).val('1');
} else {
actionRequire( 'no' ,phoneField );
$(phoneCheck).val('0');
}
}, 300 );
});
})(jQuery);
</script>
<?php
}
// Phone number validation, when the field is required
add_action('woocommerce_checkout_process', 'billing_phone_field_process');
function billing_phone_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['billing_phone'] && $_POST['billing_phone_check'] == '1' )
wc_add_notice( __( 'Please enter a number phone.' ), 'error' );
}

Related

Add discount to woocomerce cart page INCL. tax

I am trying to modify the code based on this question:
// Display the checkout field in cart page totals section
add_action( 'woocommerce_cart_totals_before_order_total', 'display_priority_fee_checkbox_field', 20 );
function display_priority_fee_checkbox_field(){
echo '<tr class="installment-section">
<th>'.__("Priority Dispatch").'</th><td>';
woocommerce_form_field( 'priority_fee', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => __(' $20.00'),
), WC()->session->get('priority_fee') ? '1' : '' );
echo '<div class="tooltip">?
<span class="tooltiptext">'.__("By selecting this option... ").'</span>
</div></td>';
}
// Remove "(optional)" text from the field
add_filter( 'woocommerce_form_field' , 'remove_optional_txt_from_priority_fee_checkbox', 10, 4 );
function remove_optional_txt_from_priority_fee_checkbox( $field, $key, $args, $value ) {
// Only on checkout page for Order notes field
if( 'priority_fee' === $key ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
// jQuery :: Ajax script
add_action( 'wp_footer', 'priority_fee_js_script' );
function priority_fee_js_script() {
// On Order received page, remove the wc session variable if it exist
if ( is_wc_endpoint_url('order-received')
&& WC()->session->__isset('priority_fee') ) :
WC()->session->__unset('priority_fee');
// On Cart page: jQuert script
elseif ( is_cart() ) :
?>
<script type="text/javascript">
jQuery( function($){
if (typeof woocommerce_params === 'undefined')
return false;
var c = 'input[name=priority_fee]';
$(document.body).on( 'click change', c, function(){
console.log('click');
var fee = $(c).is(':checked') ? '1' : '';
$.ajax({
type: 'POST',
url: woocommerce_params.ajax_url,
data: {
'action': 'priority_fee',
'priority_fee': fee,
},
success: function (response) {
setTimeout(function(){
$(document.body).trigger('added_to_cart');
}, 500);
},
});
});
});
</script>
<?php
endif;
}
// Get Ajax request and saving to WC session
add_action( 'wp_ajax_priority_fee', 'priority_fee_ajax_receiver' );
add_action( 'wp_ajax_nopriv_priority_fee', 'priority_fee_ajax_receiver' );
function priority_fee_ajax_receiver() {
if ( isset($_POST['priority_fee']) ) {
$priority_fee = $_POST['priority_fee'] ? true : false;
// Set to a WC Session variable
WC()->session->set('priority_fee', $priority_fee );
echo $priority_fee ? '1' : '0';
die();
}
}
// Add a custom calculated fee conditionally
add_action( 'woocommerce_cart_calculate_fees', 'set_priority_fee' );
function set_priority_fee( $cart ){
if ( is_admin() && ! defined('DOING_AJAX') )
return;
if ( WC()->session->get('priority_fee') ) {
$item_count = $cart->get_cart_contents_count();
$fee_label = 'Discount';
$fee_amount = -2;
$cart->add_fee( $fee_label, $fee_amount );
}
}
But the discount is not showing up. Also, I nneed the discount to be applied to the SUBTOTALS so that tax is recalculated as well (which the code does not do, it applies the discount after taxes).
How can I set a negative fee(discount) when checkbox is checked and also have the discount applied before tax calculation?
EDIT: Okay, I got it to work with the taxes by changing the last part:
// Set the discount
add_action( 'woocommerce_cart_calculate_fees', 'checkout_set_discount', 20, 1 );
function checkout_set_discount( $cart ) {
if ( ( is_admin() && ! defined('DOING_AJAX') ) )
return;
$subtotal = WC()->cart->get_subtotal();
$percentage = 1;
$discount = $subtotal * $percentage / 100;
if( WC()->session->get('priority_fee') ) {
$cart->add_fee( sprintf( __( 'Discount', 'woocommerce') ), -$discount );
}
}
but this only works with a percentage based discount. How can I instead use a fixed discount?

Calculate fee from cart totals (subtotal + shipping) without adding it to order total value in WooCommerce

Based on Add a checkout checkbox field that enable a percentage fee in Woocommerce answer code I created a checkbox on the checkout page.
When it is checked, it applies a 15% freight forwarding fee.
// Add a custom checkbox fields before order notes
add_action( 'woocommerce_before_order_notes', 'add_custom_checkout_checkbox', 20 );
function add_custom_checkout_checkbox(){
// Add a custom checkbox field
woocommerce_form_field( 'forwarding_fee', array(
'type' => 'checkbox',
'label' => __('15% forwarding fee'),
'class' => array( 'form-row-wide' ),
), '' );
}
// jQuery - Ajax script
add_action( 'wp_footer', 'checkout_fee_script' );
function checkout_fee_script() {
// Only on Checkout
if( is_checkout() && ! is_wc_endpoint_url() ) :
if( WC()->session->__isset('enable_fee') )
WC()->session->__unset('enable_fee')
?>
<script type="text/javascript">
jQuery( function($){
if (typeof wc_checkout_params === 'undefined')
return false;
$('form.checkout').on('change', 'input[name=forwarding_fee]', function(e){
var fee = $(this).prop('checked') === true ? '1' : '';
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'enable_fee',
'enable_fee': fee,
},
success: function (result) {
$('body').trigger('update_checkout');
},
});
});
});
</script>
<?php
endif;
}
// Get Ajax request and saving to WC session
add_action( 'wp_ajax_enable_fee', 'get_enable_fee' );
add_action( 'wp_ajax_nopriv_enable_fee', 'get_enable_fee' );
function get_enable_fee() {
if ( isset($_POST['enable_fee']) ) {
WC()->session->set('enable_fee', ($_POST['enable_fee'] ? true : false) );
}
die();
}
// Add a custom dynamic 15% fee
add_action( 'woocommerce_cart_calculate_fees', 'custom_percetage_fee', 20, 1 );
function custom_percetage_fee( $cart ) {
// Only on checkout
if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || ! is_checkout() )
return;
$percent = 15;
if( WC()->session->get('enable_fee') )
$cart->add_fee( __( 'Forwarding fee', 'woocommerce')." ($percent%)", ($cart->get_subtotal() * $percent / 100) );
}
Currently, this fee is calculated from the subtotal and added up to the order total value.
I need a solution where this fee is calculated from a sum of subtotal + shipping AND IS NOT added to the order total value.
I will rename "fee" to "deposit".
Please see a screenshot:
Since you don't want to add it to the total, you can add a custom table row to the woocommerce-checkout-review-order-table instead of a cart fee. So my answer is not based on the WooCommerce fee and is completely separate from it.
The custom table row will then show/hide the percentage, based on if the checkbox is checked.
Explanation via one-line comments, added to my answer.
So you get:
// Add checkbox field
function action_woocommerce_before_order_notes( $checkout ) {
// Add field
woocommerce_form_field( 'my_id', array(
'type' => 'checkbox',
'class' => array( 'form-row-wide' ),
'label' => __( '15% and some other text', 'woocommerce' ),
'required' => false,
), $checkout->get_value( 'my_id' ));
}
add_action( 'woocommerce_before_order_notes', 'action_woocommerce_before_order_notes', 10, 1 );
// Save checkbox value
function action_woocommerce_checkout_create_order( $order, $data ) {
// Set the correct value
$checkbox_value = isset( $_POST['my_id'] ) ? 'yes' : 'no';
// Update meta data
$order->update_meta_data( '_my_checkbox_value', $checkbox_value );
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );
// Add table row on the checkout page
function action_woocommerce_before_order_total() {
// Initialize
$percent = 15;
// Get subtotal & shipping total
$subtotal = WC()->cart->subtotal;
$shipping_total = WC()->cart->get_shipping_total();
// Total
$total = $subtotal + $shipping_total;
// Result
$result = ( $total / 100 ) * $percent;
// The Output
echo '<tr class="my-class">
<th>' . __( 'My text', 'woocommerce' ) . '</th>
<td data-title="My text">' . wc_price( $result ) . '</td>
</tr>';
}
add_action( 'woocommerce_review_order_before_order_total', 'action_woocommerce_before_order_total', 10, 0 );
// Show/hide table row on the checkout page with jQuery
function action_wp_footer() {
// Only on checkout
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
jQuery( function($){
// Selector
var my_input = 'input[name=my_id]';
var my_class = '.my-class';
// Show or hide
function show_or_hide() {
if ( $( my_input ).is(':checked') ) {
return $( my_class ).show();
} else {
return $( my_class ).hide();
}
}
// Default
$( document ).ajaxComplete(function() {
show_or_hide();
});
// On change
$( 'form.checkout' ).change(function() {
show_or_hide();
});
});
</script>
<?php
endif;
}
add_action( 'wp_footer', 'action_wp_footer', 10, 0 );
// If desired, add new table row to emails, order received (thank you page) & my account -> view order
function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
// Get checkbox value
$checkbox_value = $order->get_meta( '_my_checkbox_value' );
// NOT equal to yes, return
if ( $checkbox_value != 'yes' ) return $total_rows;
// Initialize
$percent = 15;
// Get subtotal & shipping total
$subtotal = $order->get_subtotal();
$shipping_total = $order->get_shipping_total();
// Total
$total = $subtotal + $shipping_total;
// Result
$result = ( $total / 100 ) * $percent;
// Save the value to be reordered
$order_total = $total_rows['order_total'];
// Remove item to be reordered
unset( $total_rows['order_total'] );
// Add new row
$total_rows['my_text'] = array(
'label' => __( 'My text:', 'woocommerce' ),
'value' => wc_price( $result ),
);
// Reinsert removed in the right order
$total_rows['order_total'] = $order_total;
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );

Custom checkbox that adds a fee in WooCommerce cart page

Based on Display a checkbox that add a fee in Woocommerce checkout page answer code I am trying to change usage to Cart page, rather than checkout page per clients request.
So far I managed to display custom fee check box on cart page and trigger Ajax.
However Cart totals are not updated.
If someone can help on code or point me in right direction?
// Display the custom checkbox field in cart
add_action( 'woocommerce_cart_totals_before_order_total', 'fee_installment_checkbox_field', 20 );
function fee_installment_checkbox_field(){
echo '<tr class="packing-select"><th>Priority Dispatch</th><td>';
woocommerce_form_field( 'priority_fee', array(
'type' => 'checkbox',
'class' => array('installment-fee form-row-wide'),
'label' => __(' $20.00'),
'placeholder' => __(''),
), WC()->session->get('priority_fee') ? '1' : '' );
echo '<div class="tooltip">?
<span class="tooltiptext">By selecting this option... </span>
</div></td>';
}
// jQuery - Ajax script
add_action( 'wp_footer', 'woo_add_cart_fee' );
function woo_add_cart_fee() {
// Only on Checkout
if( ! is_wc_endpoint_url() ) :
if( WC()->session->__isset('priority_fee') )
WC()->session->__unset('priority_fee')
?>
<script type="text/javascript">
jQuery( function($){
//if (typeof wc_add_to_cart_params === 'undefined')
// return false;
$('tr.packing-select').on('change', 'input[name=priority_fee]', function(){
console.log('tests');
var fee = $(this).prop('checked') === true ? '1' : '';
$.ajax({
type: 'POST',
//url: wc_add_to_cart_params.ajax_url,
data: {
'action': 'priority_fee',
'priority_fee': fee,
},
success: function (response) {
$('body').trigger('added_to_cart');
},
});
});
});
</script>
<?php
endif;
}
// Get Ajax request and saving to WC session
add_action( 'wp_ajax_priority_fee', 'get_priority_fee' );
add_action( 'wp_ajax_nopriv_priority_fee', 'get_priority_fee' );
function get_priority_fee() {
if ( isset($_POST['priority_fee']) ) {
WC()->session->set('priority_fee', ($_POST['priority_fee'] ? true : false) );
}
die();
}
// Add a custom calculated fee conditionally
add_action( 'woocommerce_cart_calculate_fees', 'set_priority_fee' );
function set_priority_fee( $cart ){
if ( is_admin() && ! defined('DOING_AJAX') )
return;
if ( did_action('woocommerce_cart_calculate_fees') >= 2 )
return;
if ( 1 == WC()->session->get('priority_fee') ) {
$items_count = WC()->cart->get_cart_contents_count();
$fee_label = sprintf( __( "PRIORITY DISPATCH %s %s" ), '×', $items_count );
$fee_amount = 20;
WC()->cart->add_fee( $fee_label, $fee_amount );
}
}
add_filter( 'woocommerce_form_field' , 'remove_optional_txt_from_installment_checkbox', 10, 4 );
function remove_optional_txt_from_installment_checkbox( $field, $key, $args, $value ) {
// Only on checkout page for Order notes field
if( 'priority_fee' === $key ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
Additionally is this the way this should be done, or is there an easier way to do it?
There are some mistakes in your code. Also, on cart page, jQuery events and Ajax behavior are a bit different, so it requires some changes:
// Display the checkout field in cart page totals section
add_action( 'woocommerce_cart_totals_before_order_total', 'display_priority_fee_checkbox_field', 20 );
function display_priority_fee_checkbox_field(){
echo '<tr class="installment-section">
<th>'.__("Priority Dispatch").'</th><td>';
woocommerce_form_field( 'priority_fee', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => __(' $20.00'),
), WC()->session->get('priority_fee') ? '1' : '' );
echo '<div class="tooltip">?
<span class="tooltiptext">'.__("By selecting this option... ").'</span>
</div></td>';
}
// Remove "(optional)" text from the field
add_filter( 'woocommerce_form_field' , 'remove_optional_txt_from_priority_fee_checkbox', 10, 4 );
function remove_optional_txt_from_priority_fee_checkbox( $field, $key, $args, $value ) {
// Only on checkout page for Order notes field
if( 'priority_fee' === $key ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
// jQuery :: Ajax script
add_action( 'wp_footer', 'priority_fee_js_script' );
function priority_fee_js_script() {
// On Order received page, remove the wc session variable if it exist
if ( is_wc_endpoint_url('order-received')
&& WC()->session->__isset('priority_fee') ) :
WC()->session->__unset('priority_fee');
// On Cart page: jQuert script
elseif ( is_cart() ) :
?>
<script type="text/javascript">
jQuery( function($){
if (typeof woocommerce_params === 'undefined')
return false;
var c = 'input[name=priority_fee]';
$(document.body).on( 'click change', c, function(){
console.log('click');
var fee = $(c).is(':checked') ? '1' : '';
$.ajax({
type: 'POST',
url: woocommerce_params.ajax_url,
data: {
'action': 'priority_fee',
'priority_fee': fee,
},
success: function (response) {
setTimeout(function(){
$(document.body).trigger('added_to_cart');
}, 500);
},
});
});
});
</script>
<?php
endif;
}
// Get Ajax request and saving to WC session
add_action( 'wp_ajax_priority_fee', 'priority_fee_ajax_receiver' );
add_action( 'wp_ajax_nopriv_priority_fee', 'priority_fee_ajax_receiver' );
function priority_fee_ajax_receiver() {
if ( isset($_POST['priority_fee']) ) {
$priority_fee = $_POST['priority_fee'] ? true : false;
// Set to a WC Session variable
WC()->session->set('priority_fee', $priority_fee );
echo $priority_fee ? '1' : '0';
die();
}
}
// Add a custom calculated fee conditionally
add_action( 'woocommerce_cart_calculate_fees', 'set_priority_fee' );
function set_priority_fee( $cart ){
if ( is_admin() && ! defined('DOING_AJAX') )
return;
if ( WC()->session->get('priority_fee') ) {
$item_count = $cart->get_cart_contents_count();
$fee_label = sprintf( __( "PRIORITY DISPATCH %s" ), '× ' . $item_count );
$fee_amount = 20 * $item_count;
$cart->add_fee( $fee_label, $fee_amount );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

How to validate a custom field on the Variable product on 'add to cart' submit [duplicate]

This question already has an answer here:
Custom field validation in Woocommerce single product pages [closed]
(1 answer)
Closed 4 years ago.
I'm trying to validate the input field of a product variant.
Only this variant has a field for input.
The product works and is added to cart correctly.
My issue is that I want to make sure the input value is numeric because this value will be the product price.
The hook I have found that could support this:
woocommerce_add_to_cart_validation
The issue is it only passed the product id and not the variant id and I also cant see any data the user inputted.
So I need to validate the variable product custom input before the item is added to cart .....
my code for testing this: (just a series of echo's to see inside the vars I'm trying)
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = '', $variations= array(),$cart_item_data = array(),$item='' ) {
echo'Passed var:<br>';
echo'<pre>';
print_r($passed);
echo'</pre>';
echo'product id var:<br>';
echo'<pre>';
print_r($product_id);
echo'</pre>';
echo'quantity var:<br>';
echo'<pre>';
print_r($quantity);
echo'</pre>';
echo'variation id var:<br>';
echo'<pre>';
print_r($variation_id);
echo'</pre>';
echo'variations var:<br>';
echo'<pre>';
print_r($variations);
echo'</pre>';
echo'Cart Item data var:<br>';
echo'<pre>';
print_r($cart_item_data);
echo'</pre>';
echo' Item data var:<br>';
echo'<pre>';
print_r($item);
echo'</pre>';
die;
};
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 3 );
The output is as follows:
Passed var:
1
product id var:
1331
quantity var:
1
variation id var:
variations var:
Array
(
)
Cart Item data var:
Array
(
)
Item data var:
This is the function I have to make this field for reference :
add_action('woocommerce_before_add_to_cart_button', 'custom_product_field', 10 );
function custom_product_field() {
global $product;
if ( $product->is_type('variable') ) {
?>
<style>
.my-field-class-custom-product {
display:none;
}
.custominvisible {
display:none;
}
</style>
<script>
jQuery(document).ready(function($) {
var cols = document.getElementsByClassName('woocommerce-variation-price');
function toggleFields() {
if ($('input.variation_id').val() != "1332")
{
$("#product_custom_price_field").hide();
$(cols).css("display", "inherint");
}
else
{
$("#product_custom_price_field").show();
$(cols).css("display", "none");
}
}
$('input.variation_id').change( function(){
if( '' != $('input.variation_id').val() ) {
toggleFields();
}
});
});
</script>
<?php
echo '<br><div>';
woocommerce_form_field('product_custom_price', array(
'type' => 'text',
'class' => array( 'my-field-class-custom-product form-row-wide') ,
'label' => __('Enter the custom amount you wish to buy') ,
'placeholder' => __('') ,
'required' => false,
) , '');
echo '</div>';
}
}
Here I insert the field data into the cart item data :
function add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
if( ! empty( $_POST['product_custom_price'] ) ) {
$product = wc_get_product( $variation_id );
$price = $product->get_price();
$cart_item_data['custom_amount'] = $_POST['product_custom_price'];
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 10, 3 );
Then finally here I update the variable product with the new price:
function before_calculate_totals( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
// Iterate through each cart item
foreach( $cart_obj->get_cart() as $key=>$value ) {
if( isset( $value['custom_amount'] ) ) {
$price = $value['custom_amount'];
$value['data']->set_price( ( $price ) );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'before_calculate_totals', 10, 1 );
So a work around I have is to instead use jquery and stop the entry on input, I added this script.
<script>
jQuery(document).ready(function($) {
$('#product_custom_price').keyup( function(){
//var aaa = $('#product_custom_price').val()
//console.log(aaa);
if (isNaN($('#product_custom_price').val() / 1) == true) {
alert('Please enter numbers only for the amount');
$('#product_custom_price').val('');
}
});
});
</script>
I'm VERY open to someone showing me a better option !!
If you add this to the validation filter can you get the product variation ID?
<?php
if ($product->product_type == 'variable'):
$children = $product->get_children( $args = '', $output = OBJECT );
foreach ($children as $key=>$value) {
$product_variatons = new WC_Product_Variation($value);
if ( $product_variatons->exists() && $product_variatons-
>variation_is_visible() ) {
$variations[$value] = $product_variatons->get_variation_attributes();
}
}
endif;
Here is the perfect answer .. well for my case anyways..
function my_validate_custom_field( $passed, $product_id, $quantity ) {
$myValitation = $_POST['product_custom_price'];
if( !$myValitation ) {
// Fails validation
$passed = false;
wc_add_notice( __( 'Please enter an amount you wish to buy', 'cfwc' ), 'error' );
}elseif(!is_numeric($myValitation)){
$passed = false;
wc_add_notice( __( 'Please enter numbers only', 'cfwc' ), 'error' );
}elseif($myValitation <1){
$passed = false;
wc_add_notice( __( 'Please enter an amount greater than 1', 'cfwc' ), 'error' );
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'my_validate_custom_field', 10, 3 );

WooCommerce: update custom fields after checkout validation failure

In my project I'm customizing some of the WooCommerce features.
My "shipping methods" are:
1. delivery
2. take away
I also added a custom field in the checkout page that is a <select> populated with the valid times for delivery (case "1") or for the take away (case "2").
It may happen that a user selects 2. take away in the cart page, then selects a time valid for "2", but then changes to 1. delivery and the selected time may not be valid anymore, neither the option list and the custom field label.
Of course i'm using the woocommerce_checkout_process hook to warn the user via wc_add_notice(), but even if the woocommerce_checkout_fields hook is triggered (that's where i create the select list), the <select> values are not updated.
I think there is an AJAX call that is related only to the shipping method and doesn't update the other checkout fields, although woocommerce_checkout_fields hook is triggered.
How to update the custom fields?
Do i need some js/jquery/AJAX?
Or: can a custom field be related to a shipping method (and get updated via AJAX with it)? How?
EDIT
Custom field code:
add_filter( 'woocommerce_checkout_fields', 'fty_filter_checkout_fields' );
function my_filter_checkout_fields($fields) {
$must_deliver = WC()->cart->shipping_total > 0.0; // true=deliver, false=take away
// some complex code to calculate time lists omitted, samples array instead:
$delivery_time_list = array(
"deliver 10:00",
"deliver 11:00",
"deliver 12:00",
"deliver 13:00"
);
$takeaway_time_list = array(
"takeaway 10:00",
"takeaway 10:30",
"takeaway 11:00",
"takeaway 11:30",
"takeaway 12:00",
"takeaway 12:30",
"takeaway 13:00",
"takeaway 13:30"
);
// add the new conditional field
if($must_deliver) {
$fields['my_delivery_datetime'] = array(
'my_delivery_time' => array(
'type' => 'select',
'options' => $delivery_time_list,
'required' => true,
'label' => __('Delivery time')
)
);
} else {
$fields['my_delivery_time'] = array(
'my_delivery_time' => array(
'type' => 'select',
'options' => $takeaway_time_list,
'required' => true,
'label' => __('Take away time')
)
);
}
return $fields;
}
an idea of the validation code:
add_action('woocommerce_checkout_process', 'my_checkout_date_time_validation', 30, 1);
function my_checkout_date_time_validation($doh) {
$time = filter_input(INPUT_POST, 'my_delivery_time');
$shipping = filter_input(INPUT_POST, 'shipping_method', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
if(strpos($time, "deliver")!==FALSE && strpos($shipping[0], "local_pickup")!==FALSE) {
wc_add_notice('Please re-select take away time', 'error');
} else if(strpos($time, "takeaway")!==FALSE && strpos($shipping[0], "distance_based_rate")!==FALSE) {
wc_add_notice('Please re-select delivery time', 'error');
}
}
here's about shipping methods;
add_action( 'woocommerce_flat_rate_shipping_add_rate', 'add_distance_based_delivery_rate', 10, 2 );
function add_distance_based_delivery_rate( $method, $rate ) {
$new_rate = $rate;
$new_rate['id'] .= ':' . 'distance_based_rate';
$new_rate['label'] = 'delivery'; // Rename to 'Rushed Shipping'.
// incredibly complex code used to calculate delivery costs omitted
$dist_cost = 1000;
$new_rate['cost'] += $dist_cost;
$method->add_rate( $new_rate );
}
Thanx!
The code provided was mostly un-useful… I have make a lot of changes and optimizations. All my code is commented, it's tested on WooCommerce 3+ and perfectly works.
You will have to add your "incredibly complex code used to calculate delivery costs omitted"…
1) JAVASCRIPT FOR CONDITIONAL CHECKOUT FIELDS LIVE EVENTS
The only way to get the hand on customer live events (browser side) is javascript/jQuery. So this is not easy because WooCommerce use already a lot of javascript/jQuery/Ajax on checkout page…
I have included the javascript code into the hooked function, but you should save it in a separate file and register this script file with classic WordPress registering script function, like in this thread:
Checkout fields: Hiding and showing existing fields
2) USE EXISTING AVAILABLE SHIPPING METHODS (DYNAMIC PRICES CALCULATION):
You don't need to create any shipping rate. You can use:
local_pickup available method for your "TAKE WAY"
flat_rate available method for your "Delivery" (with dynamic price calculations)
For each of your shipping zones, enable, set and rename (label name) the 2 methods in Woocommerce > Settings > Shipping:
For the flat rate you can set any minimal amount (that will be overwritten by your calculations)…
If you make changes you need to refresh shipping cached data: disable, save and enable, save those methods for the current shipping zone.
3) SAVING THE SHIPPING TIME TO ORDER META DATA:
I have add some code for that and it's save in 2 custom meta fields:
One for the chosen shipping
The other for the time
4) DISPLAYING THE CHOSEN SHIPPING TYPE AND TIME IN A METABOX (IN ORDER EDIT PAGES):
I have also add some code for that.
FINALY HERE IS THE CODE:
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
// The 2 Options arrays in imput select
$delivery_time_list[''] = $takeaway_time_list[''] = __('Select an hour');
for($h = 10, $i = 0; $i < 8; $i++ ){
if( $i % 2 == 0 ){
$time = $h.':00';
$delivery_time_list[$time] = 'deliver '.$time;
} else {
$time = $h.':30';
$h++;
}
$takeaway_time_list[$time] = 'takeaway '.$time;
}
echo '<div id="delivery_checkout_fields"><h3>' . __('Shipping time options') . '</h3>';
woocommerce_form_field( 'delivery_time', array(
'type' => 'select',
'class' => array('delivery-time form-row-wide'),
'label' => __('Delivery time'),
'options' => $delivery_time_list,
), $checkout->get_value( 'delivery_time' ) );
woocommerce_form_field( 'takeaway_time', array(
'type' => 'select',
'class' => array('takeaway-time form-row-wide'),
'label' => __('Take away time'),
'options' => $takeaway_time_list,
), $checkout->get_value( 'takeaway_time' ) );
echo '</div>';
$required = esc_attr__( 'required', 'woocommerce' );
?>
<script>
jQuery(function($){
var choosenShipMethod = $('input[name^="shipping_method"]:checked').val().split(':')[0], // Choosen shipping method slug
required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>'; // Required html
// TESTING: displaying in console the choosen shipping
console.log('Chosen shipping: '+choosenShipMethod);
// Function that shows or hide imput select fields
function showHide( actionToDo='show', selector='' ){
if( actionToDo == 'show' )
$(selector).show(function(){
$(this).addClass("validate-required");
$(this).removeClass("woocommerce-validated");
$(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
$(selector+' label').append(required);
//console.log('Selector (show): '+selector);
});
else
$(selector).hide(function(){
$(this).removeClass("validate-required");
$(this).removeClass("woocommerce-validated");
$(this).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
$(selector+' label > .required').remove();
//console.log('Selector (hide): '+selector);
});
}
// Initialising at start (Based on the choosen shipping method)
if( choosenShipMethod == 'flat_rate' ) // Choosen "Delivery" (Hidding "Take away")
{
showHide('show','#delivery_time_field' );
showHide('hide','#takeaway_time_field' );
}
else if( choosenShipMethod == 'local_pickup' ) // Choosen "Take away" (Hidding "Delivery")
{
showHide('show','#takeaway_time_field' );
showHide('hide','#delivery_time_field' );
}
else // No shipping choosen yet (Hidding BOTH shipping dropdown hour selectors
{
showHide('hide','#delivery_time_field' );
showHide('hide','#takeaway_time_field' );
$('#delivery_checkout_fields').hide();
}
// When shipping method is changed (Live event)
$( 'form.checkout' ).on( 'change', 'input[name^="shipping_method"]', function() {
var changedShipMethod = $('input[name^="shipping_method"]:checked').val().split(':')[0];
if( changedShipMethod == 'flat_rate' )
{
// Choose "Delivery" | Show "Delivery" and Hide "Take away"
$('#delivery_checkout_fields').show();
showHide('show','#delivery_time_field' );
showHide('hide','#takeaway_time_field' );
}
else if( changedShipMethod == 'local_pickup' )
{
// Choose "Take away" | Show "Take away" and Hide "Delivery"
$('#delivery_checkout_fields').show();
showHide('show','#takeaway_time_field' );
showHide('hide','#delivery_time_field' );
}
console.log("Chosen shipping: "+changedShipMethod);
});
// When an hour is selected (LIVE event)
$('#delivery_checkout_fields select').change( function(){
if( $(this).val() != '')
$(this).parent().removeClass("validate-required");
else
$(this).parent().addClass("validate-required");
console.log("Selector value: "+$(this).val());
});
// "select.shipping_method, input[name^="shipping_method"], #ship-to-different-address input, .update_totals_on_change select, .update_totals_on_change input[type="radio"], .update_totals_on_change input[type="checkbox"]"
//"function (){t.reset_update_checkout_timer(),t.dirtyInput=!1,e(document.body).trigger("update_checkout")}"
});
</script>
<?php
}
// Process the checkout (Checking if required fields are not empty)
add_action('woocommerce_checkout_process', 'ba_custom_checkout_field_process');
function ba_custom_checkout_field_process() {
$delivery_time = $takeaway_time = 0;
if ( $_POST['delivery_time'] ) $delivery_time = 1;
if ( $_POST['takeaway_time'] ) $takeaway_time = 1;
// Only one message is possible for both
if ( ( $delivery_time + $takeaway_time ) == 0 ){
wc_add_notice( __('Please select a <strong>shipping time</strong>.' ), 'error');
}
}
## CALCULATING THE DELIVERY FEE (BASED ON COUNTING THE DIFFERENT DATES For all items) ##
add_filter( 'woocommerce_package_rates', 'custom_shipping_flat_rate_cost_calculation', 10, 2 );
function custom_shipping_flat_rate_cost_calculation( $rates, $package )
{
## --- CALCULATIONS Based on CART DATA (if needed) --- ##
foreach(WC()->cart->get_cart() as $cart_item ):
// HERE your incredibly complex code used to calculate delivery costs
endforeach;
## --- CHANGING DYNAMICALLY THE METHODS COSTS --- ##
foreach($rates as $rate_key => $rate_values):
$method_id = $rate_values->method_id;
$rate_id = $rate_values->id;
// "DELIVERY" - "local_pickup" method (if needed)
if ( 'flat_rate' === $method_id ){
// HERE your incredibly complex code used to calculate delivery costs
// Change the price cost
$price_excl_tax = $rates[$rate_id]->cost + 2.5;
$rates[$rate_id]->cost = number_format($price_excl_tax, 2);
$tax_calculation = $rates[$rate_id]->taxes[0] * 0.1;
$rates[$rate_id]->taxes[0] = number_format($tax_calculation, 2);
}
// "TAKE WAY" - "local_pickup" method (if needed)
elseif ( 'local_pickup' === $method_id )
{
// do something if needed
}
endforeach;
return $rates;
}
// Save the "shipping time" in order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'save_shipping_time_in_order_meta', 100, 1 );
function save_shipping_time_in_order_meta( $order_id ) {
// Take away time
$takeaway_time = $_POST['takeaway_time'];
if ( ! empty( $takeaway_time ) ){
add_post_meta( $order_id, '_shipping_time', $takeaway_time );
add_post_meta( $order_id, '_shipping_type', __('Take away', 'woocommerce' ) );
}
// Delivery time
$delivery_time = $_POST['delivery_time'];
if ( ! empty( $delivery_time ) ){
add_post_meta( $order_id, '_shipping_time', $delivery_time );
add_post_meta( $order_id, '_shipping_type', __('Delivery', 'woocommerce' ) );
}
}
// Adding shipping time metabox (on right side) to Order edit pages
add_action( 'add_meta_boxes', 'add_order_shipping_time_meta_boxe' );
function add_order_shipping_time_meta_boxe(){
add_meta_box(
'woocommerce-order-shipping-time-values', __( 'Shipping type and time', 'woocommerce' ),
'order_shipping_time_values', 'shop_order', 'side', 'default'
);
}
// Adding content to shipping time metabox to Order edit pages
function order_shipping_time_values(){
global $post;
$type = get_post_meta($post->ID, '_shipping_type', true);
$time = get_post_meta($post->ID, '_shipping_time', true);
echo "<p><strong>Type:</strong> $type | <strong>time:</strong> $time</p>";
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce 3+ and works.

Categories