Case 1: Conditionally display checkout field based on day & time
If you like to display a checkout field on a specific time in a specific day in week, you can use below code snippet.
The below code snippet will show a checkout field named as field_name
in between 7 AM & 6 PM for all days except weekends (Sunday & Saturday).
function thwcfe_show_checkout_field($show, $field_name){
if($field_name == 'field_name'){
$show = false;
$currentDateTime = current_time('H:i:s');
$current_time = strtotime($currentDateTime);
$start_time = strtotime('07:00:00');
$end_time = strtotime('18:00:00');
$weekend = array('Sun','Sat');
$day = current_time('D');
if($current_time > $start_time && $current_time < $end_time && !in_array($day, $weekend)){
$show = true;
}
}
return $show;
}
add_filter('thwcfe_show_field', 'thwcfe_show_checkout_field', 10, 2);
Case 2: Display checkout section based on saved user data
If you are using the Checkout Editor plugin, you can save the checkout field data as user meta.
In a few situations, we need to display checkout page sections based on these saved user data. Remember that this is only possible for logged in users.
Use the below code snippet in your theme’s or child theme’s functions.php file. Update the user meta key, values, and section identifier in the code snippet with your details.
function thwcfe_conditional_section_hide($show, $section_name){
if(is_user_logged_in()){
$user_id = get_current_user_id();
$user_meta = get_user_meta($user_id, 'billing_first_name', true);
if($user_meta === 'saved_user_data'){
if($section_name == 'section_name'){
$show = false;
}
}
}
return $show;
}
add_filter('thwcfe_show_section', 'thwcfe_conditional_section_hide', 10, 2);
Case 3: Display checkout field if the cart quantity is more than one
This code snippet is useful to display the checkout field if the cart quantity is more than one using WooCommerce Checkout Field Editor Pro plugin.
Use the below code snippet in your theme’s or child theme’s functions.php file. Update the string {field_name} in the filter tag in the code snippet with your field name.
function thwcfe_show_field_condition($show){
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
if($count < 1){
$show = false;
}
return $show;
}
add_filter('thwcfe_show_field_{field_name}', 'thwcfe_show_field_condition');
Case 4: Conditionally display checkout fields
There are already too many inbuilt conditions available in the WooCommerce Checkout Field Editor plugin.
But if you have some amazing conditions that are not covered by our plugin, here is a simple code snippet for you to display or hide checkout fields based on your conditions.
Use the below code snippet in your theme’s or child theme’s functions.php file. Update the string {field_name} in the filter tag in the code snippet with your field name.
function thwcfe_show_field_condition($show){
// Your conditions here
// $show Variable have the value True or False as per the field settings in your backend
// Return True to display the field
// Return False to hide the field
}
add_filter('thwcfe_show_field_{field_name}', 'thwcfe_show_field_condition');
Case 5: Add a fee based on selected date by date-picker custom checkout field
First create new checkout field with date picker using Checkout Field Editor plugin. You must add a price for that input field.
Then use below code snippet in your theme’s or child theme’s functions.php
file. Update the string {field_name}
in filter tag in the code snippet with your field name.
function thwcfe_change_total_price($price, $value){
// Get today date
$current_time = date( 'd/m/Y', current_time( 'timestamp', 0 ) );
$today = DateTime::createFromFormat('d/m/Y', $current_time);
$today->setTime( 0, 0, 0 );
// Get selected date using date picker
$selected_date = DateTime::createFromFormat('d/m/Y', $value);
$selected_date->setTime( 0, 0, 0 );
// Find the difference between dates
$diff = $today->diff( $selected_date );
$diffDays = (integer)$diff->format( "%R%a" ); // Extract days count in interval
// Conditionally add fee using switch statement
switch( $diffDays ) {
case 0: // for Today
$price = '';
break;
case -1: // for Yesterday
$price = '';
break;
case +1: // for Tomorrow
$price = $value;
break;
default: // for Sometime
$price = '';
}
return $price;
}
add_filter('thwcfe_checkout_field_extra_cost_{field_name}', 'thwcfe_change_total_price',10,2);
Case 6: Show checkout field based on cart items shipping class
If you like to show a checkout field based on cart items shipping class, you can use below code snippet.
function th3er4_show_field_based_on_shipping_class($show, $field_name){
if(!is_checkout()){
return;
}
if($field_name != 'your_field_name'){
return $show;
}
global $woocommerce;
$show = false;
$cart_object = WC()->cart->get_cart();
$shipping_class='test_shipping_class';
if($cart_object){
foreach($cart_object as $cart_item ) {
$product = $cart_item['data'];
$class = $product->get_shipping_class();
if($class === $shipping_class){
$show = true;
break;
}
}
}
return $show;
}
add_filter('thwcfe_show_field', 'th3er4_show_field_based_on_shipping_class', 99, 2);
Comments
0 comments
Please sign in to leave a comment.