I am unsetting some checkout fields in Woocommerce via code and I am a little stumped on how to unset some custom checkout fields (not default Woocommerce checkout fields). Note that I am using Custom WooCommerce Checkout Fields Editor and Delivery & Pickup Date Time for WooCommerce third party plugins.
Below is the code I am writing that unsets some of the woocommerce related fields (billing, shipping and order):
/**
Remove following checkout fields
**/
function wc_remove_checkout_fields( $fields ) {
// Billing fields
unset( $fields['billing']['billing_company'] );
unset( $fields['billing']['billing_state'] );
// Shipping fields
unset( $fields['shipping']['shipping_company'] );
unset( $fields['shipping']['shipping_state'] );
unset( $fields['shipping']['shipping_country'] );
// Order fields
unset( $fields['order']['order_comments'] );
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'wc_remove_checkout_fields' );
HTML of delivery data field:
<input type="text" class="input-text flatpickr-input active" name="coderockz_woo_delivery_date_field" id="coderockz_woo_delivery_date_datepicker" placeholder="Delivery Date" value="" data-selectable_dates="365" data-disable_week_days="[]" data-date_format="F j, Y" data-disable_dates="[]" data-week_starts_from="0" data-default_date="1" readonly="readonly">
But how do I unset some custom checkout fields added via a third party plugin?
••• Edit •••
$hide_pickup_date_time = array('coderockz_woo_delivery_delivery_date_field');
foreach($hide_pickup_date_time as $pickup_date_time ) {
if ($chosen_shipping_pickup == $shipping_method_pickup) {
$fields_pickup['order'][$pickup_date_time]['required'] = false;
}
}
Plugin screenshot
Related
I'm trying to do a special logic for my custom plugin. If the user has added a specific product type in their cart, in the checkout page there must be radio inputs that determine whether the user wants the specific product type to be shipped or stored in vault. I've done everything for the frontend part (creating the radio inputs, built the JavaScript logic to remove from the DOM what's not necessary and so on...) but I now need to programatically remove the shipping from the order and remove the "Shipping" row inside the order preview in the checkout page. I tried the following filter
add_filter( 'woocommerce_cart_shipping_method_full_label', 'remove_shipping_labels', 10, 2 );
function remove_shipping_labels( $label, $method ) {
return '';
}
But it's removing just the label text "Free Shipping" but not the entire shipping row inside the order preview in the checkout page. How can I programatically remove the shipping availability from an order through AJAX and update the user interface inside the checkout page?
function hide_shipping_based_on_prod_type( $rates ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach ( $items as $item => $values ) {
$product_id = $values['data']->get_id();
$product_type = WC_Product_Factory::get_product_type( $values['data']->get_id() );
if ( 'simple' === $product_type ) { //check product types of woocommerce to add more conditions
unset( $rates['free_shipping:1'] );
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_prod_type', 100 );
add_action(
'after_setup_theme',
function() {
WC_Cache_Helper::get_transient_version( 'shipping', true );
}
);
I'm trying to change the shipping label based on whether any of the products in cart has an addon.
For that purpose, I am using How can I modify a Woocommerce shipping label answer code, with some changes to suit my needs.
The HTML:
<input type="hidden" name="shipping_method[0]" data-index="0" id="shipping_method_0_flat_rate5" value="flat_rate:5" class="shipping_method">
<label class="shipping__list_label" for="shipping_method_0_flat_rate5">Standard Shipping (2-3 days): <span class="woocommerce-Price-amount amount"><bdi>3,95<span class="woocommerce-Price-currencySymbol">€</span></bdi></span></label>
So the label Standard Shipping (2-3 days) must display Shipping with customization (4-6 days) if a certain condition is met (in this case: the field custom_text is not empty).
The functions.php:
//Change Standard Shipping Label if product is customized
add_filter( 'woocommerce_cart_shipping_method_full_label', 'change_shipping_label', 10, 2 );
function change_shipping_label( $full_label, $method ){
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( !empty( $cart_item['custom_text'] ) ) {
$full_label = str_replace( "Standard Shipping (2-3 days)", "Shipping with customization (4-6 days)", $full_label );
}
}
return $full_label;
}
This was a success. If the condition is met the label changes to Shipping with customization (4-6 days) prior checkout.
However, the label change does not apply after checkout (thank you page and emails, it displays the original "Standard Shipping (2-3 days)".
Can I display this label change also on thank you page and emails?
Update 2
Based on the provided code and informations, to change specific shipping label on orders, using shipping rate Id to target the desired shipping method:
add_action( 'woocommerce_checkout_create_order_shipping_item', 'action_wc_checkout_create_order_shipping_item', 10, 4 );
function action_wc_checkout_create_order_shipping_item( $item, $package_key, $package, $order ) {
// Targeting "flat_rate:5" by its instance ID
if( $item->get_instance_id() == 5 ) {
$item->set_method_title( "Shipping with customization (4-6 days)" );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related: Change specific shipping method title on WooCommerce orders after checkout
I would like to add a custom note automatically per order, based on if certain payment options are selected at checkout.
This would be a new line below the customers order notes.
Add the follows code snippet in your active theme's functions.php to achieve the above -
function modify_woocommerce_checkout_posted_data_comments( $posted_data ){
// Add extra custom notes for COD payment
if( isset( $posted_data['order_comments'] ) && isset( $posted_data['payment_method'] ) && $posted_data['payment_method'] == 'cod' ) {
$extra_note = __('Your custom notes goes here for COD payments.', 'textdomain' );
$posted_data['order_comments'] = nl2br( $posted_data['order_comments'] . "\n". $extra_note );
}
return $posted_data;
}
add_filter( 'woocommerce_checkout_posted_data', 'modify_woocommerce_checkout_posted_data_comments', 99 );
Here I added extra notes based on Cash on Delivery payment method. Change this as per your requirements.
I have conditionally unset the billing address fields on the woocommerce checkout page, but when submitting place order, woocommerce displays error:
Please enter an address to continue.
I then tried adding a filter on woocommerce_default_address_fields to make the fields optional - which only seems to work if the fields aren't unset.
// make address fields optional - this works fine without the next filter
add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );
function filter_default_address_fields( $address_fields ) {
// Only on checkout page
if( ! is_checkout() ) return $address_fields;
// All field keys in this array
$fields = array('country','company','address_1','address_2','city','state','postcode');
// Loop through each address fields (billing and shipping)
foreach( $fields as $key_field )
$address_fields[$key_field]['required'] = false;
return $address_fields;
}
//conditionally unset fields
add_filter( 'woocommerce_checkout_fields' , 'simplify_checkout' );
function simplify_checkout( $fields ) {
$customField = false;
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if cart item has attribute
if ( ! empty ($cart_item['custom_attribute']) ) $registry = true;
}
if( $customField ) {
unset($fields['billing']['billing_company']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['billing_address_2']);
unset($fields['billing']['billing_city']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_state']);
}
return $fields;
}
I'm hoping to still be able to conditionally hide / unset the fields along with a successful checkout submission.
I had this problem with the validation message two days ago. I only sell to one country, so I created following workaround. Maybe if you have fewer countries to sell this could work for you too:
Uncomment/Remove unset($fields['billing']['billing_country']);
Go to WooCommerce -> Settings -> Sell to specific countries and
enter your countries.
Now the validation message is inactive, as the Country field is
mandatory. You can remove it now with CSS.
#billing_country_field {
display: none;
}
Just change the action name and try it:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
add_action( 'woocommerce_after_shop_loop_item_title', 'filter_default_address_fields' );
Woocommerce Local pickup plus plugin is used with "Hide the shipping address" option. When some required shipping address fields are hidden they are still set as required and the form cannot be completed.
So how to unset the required shipping fields option in case of local pickup is selected?
Had the same issue, fixed it adding this code to functions.php:
add_filter('woocommerce_checkout_fields', 'plus_remove_shipping_checkout_fields');
function plus_remove_shipping_checkout_fields($fields) {
$shipping_method ='local_pickup_plus'; // Set the desired shipping method to hide the checkout field(s).
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ($chosen_shipping == $shipping_method) {
unset($fields['shipping']['shipping_address_1']); // Add/change filed name to be hide
unset($fields['shipping']['shipping_address_2']);
}
return $fields;
}