WooCommerce conditionally show order comments and make it required - php

On Checkout page, I would like to show the comment field, only if a coupon code is applied. In this case this comment field should be a required field.
The example below works except for the required status to optional.
I made the comments required as a default and then I assumed that after unsetting them the required status would be ignored.
This is the snippet that makes the comments required:
$fields['order']['order_comments']['required'] = true;
This snippet looks for a coupon code and then shows a message. I dont need the message so I left that blank, and then I added the lines that hides the comments:
add_action( 'woocommerce_before_checkout_form' , 'product_checkout_custom_content' );
function product_checkout_custom_content() {
global $woocommerce;
$msgs = array('mycouponcode'=>'');
$applied_coupon = $woocommerce->cart->applied_coupons;
if( ! array_key_exists($applied_coupon[0], $msgs) ) {
// Hides the order comments
unset( $fields['order']['order_comments'] );
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// Here I need to make the order_comments optional, not required
// echo $msgs[$applied_coupon[0]];
}
}
How can I make the order comments optional within the same action?

To make that work, you don't need function product_checkout_custom_content(). Instead you have to make some change in the function where is included $fields['order']['order_comments']['required'] = true;.
I suppose that is a function hooked in woocommerce_checkout_fields. So in that function you will have to replace $fields['order']['order_comments']['required'] = true;, by the code inside the function:
// CHECKOUT PAGE - CUSTOMIZING comment field (conditional behavior).
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
//Set your coupon slug here:
$coupon = 'coupon_slug';
// Coupon is applied: Changing Comment field Label, placeholder and setting "REQUIRED"
if ( in_array( '$coupon, WC()->cart->applied_coupons ) ){
$fields['order']['order_comments']['label'] = __('Your comment label…', 'my_theme_slug');
$fields['order']['order_comments']['placeholder'] = __('Enter here something', 'my_theme_slug');
$fields['order']['order_comments']['required'] = true;
} else {
// Removes the comment field + block title
unset($fields['order']['order_comments']);
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
}
return $fields;
}
You don't need anything else…
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.
Reference: Remove the Additional Information and Order Notes fields in WooCommerce

Related

Change "No shipping method has been selected.…" from WooCommerce checkout notice

I am trying to tackle an issue by renaming the default message below when there are no shipping methods available, either set by rule based plugins or there simply isn't any methods set in WooCommerce.
No shipping method has been selected. Please double check your address, or contact us if you need any help.
I have used the below function using the snippets plugin and I can confirm it changes the message in the cart and checkout underneath the shipping label.
However, if someone then attempts to checkout they get a red WooCommerce error message at the top of the screen which is still the default message above.
How do I also change this error message ? I would like to change it to something different than what my code below shows to give me the flexibility to put a short message in the cart totals box and a longer more informational message as to why no methods are available.
My Function:
add_filter( 'woocommerce_cart_no_shipping_available_html', 'no_shipping_available_html' );
add_filter( 'woocommerce_no_shipping_available_html', 'no_shipping_available_html' );
function no_shipping_available_html( $message ) {
$country = WC()->customer->get_shipping_country();
if ( !empty( $country ) ) {
$all_countries = WC()->countries->get_countries();
return sprintf( 'Orders delivered into the EU are limited to €150 (inc shipping) or a max weight of 2kg. The items in you cart exceed this limit. Please remove an item or reduce the quantity required to bring the order value/weight within the permitted values.', $all_countries[ $country ] );
}
return 'Orders delivered into the EU are limited to €150 (inc shipping) or a max weight of 2kg. The items in you cart exceed this limit. Please remove an item or reduce the quantity required to bring the order value/weight within the permitted values.';
}
This text is located in WC_Checkout Class inside validate_checkout() method. There are no filters to make changes to it, but you can use WordPress gettext filter for that as follows:
add_filter( 'gettext', 'change_checkout_no_shipping_method_text', 10, 3 );
function change_checkout_no_shipping_method_text( $translated_text, $text, $domain ) {
if ( is_checkout() && ! is_wc_endpoint_url() ) {
$original_text = 'No shipping method has been selected. Please double check your address, or contact us if you need any help.';
$new_text = 'Here your own text replacement.';
if ( $text === $original_text ) {
$translated_text = $new_text;
}
}
return $translated_text;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
You could use a plugin like Loco translate if you want, that makes easier to do your job.. but you could try this code, similar to #LoicTheAztec inside functions.php (either on your theme or child theme file)
add_filter( 'gettext', 'ds_translate_woocommerce_strings', 999, 3 );
function ds_translate_woocommerce_strings( $translated,
$untranslated, $domain ) {
if ( ! is_admin() ) {
switch ($translated) {
case 'the message you want to be translated exactly as you see it in WP':
$translated = 'your new message';
break;
}
}
return $translated;
}

Remove phone from billing/shipping fields everywhere in Woocommerce?

I don't need to collect customer phone numbers in woocommerce so I've used the following code to remove them from the checkout process:
add_filter( 'woocommerce_checkout_fields', 'pbj_woocommerce_checkout_fields' );
function pbj_woocommerce_checkout_fields( $fields ) {
unset($fields['billing']['billing_phone']);
unset($fields['shipping']['shipping_phone']);
$fields['billing']['billing_email']['class'] = array('form-row-wide');
return $fields;}
(sorry for hideous code pasting, first time!)
That works great on the checkout page but once in the "my account" area, the user is still required to add a phone number if they edit their billing or shipping address. I have added this code:
function pbj_remove_billingphone($fields) {
unset( $fields ['billing_phone'] );
return $fields;}
add_filter( 'woocommerce_billing_fields', 'pbj_remove_billingphone' );
which has removed it from billing, but I can't get anything to work to remove it from shipping. Any help on either something that universally removes the phone number requirement everywhere, or tips on what to filter/unset to remove the shipping phone number on the account page? Thank you!
Here is the complete way to do it in account and checkout pages (for both pages):
// Remove billing phone (and set email field class to wide)
add_filter( 'woocommerce_billing_fields', 'remove_billing_phone_field', 20, 1 );
function remove_billing_phone_field($fields) {
$fields ['billing_phone']['required'] = false; // To be sure "NOT required"
$fields['billing_email']['class'] = array('form-row-wide'); // Make the field wide
unset( $fields ['billing_phone'] ); // Remove billing phone field
return $fields;
}
// Remove shipping phone (optional)
add_filter( 'woocommerce_shipping_fields', 'remove_shipping_phone_field', 20, 1 );
function remove_shipping_phone_field($fields) {
$fields ['shipping_phone']['required'] = false; // To be sure "NOT required"
unset( $fields ['shipping_phone'] ); // Remove shipping phone field
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works
Normally shipping email and phone fields doesn't exist by default in WooCommmerce

Add value to custom column on customer's orders page in woocommerce

I was trying to add new column to the orders page of the customer's recent orders table, the new column should show the details of the product like product name & description.
Tried different solution, was fortunate to add column name but not able to add the value to the column.
Here is my code:
add_filter( 'woocommerce_account_orders_columns', 'new_orders_columns' );
function new_orders_columns( $columns = array() ) {
// Hide the columns
if( isset($columns['order-total']) ) {
unset( $columns['order-status'] );
unset( $columns['order-total'] );
unset( $columns['order-actions'] );
}
// Add new columns
//this is my custom column order details
$columns['order-detail'] = __( 'Details', 'woocommerce' );
$columns['order-status'] = __( 'Status', 'woocommerce' );
$columns['order-total'] = __( 'Total', 'woocommerce' );
$columns['order-actions'] = __( ' ');
return $columns;
}
Screenshot of the result:
You are very near…
1) Insert a new column in a specific location (another way):
add_filter( 'woocommerce_account_orders_columns', 'add_custom_account_orders_column', 10, 1 );
function add_custom_account_orders_column( $columns ) {
$ordered_columns = array();
// Inserting a new column in a specific location
$ordered_columns['order-number'] = $columns['order-number'];
$ordered_columns['order-date'] = $columns['order-date'];
$ordered_columns['order-details'] = __( 'Details', 'woocommerce' ); // <== New column
$ordered_columns['order-status'] = $columns['order-status'];
$ordered_columns['order-total'] = $columns['order-total'];
$ordered_columns['order-actions'] = $columns['order-actions'];
return $ordered_columns;
}
Code goes in function.php file of your active child theme (or active theme).
tested and works.
2) Display data in this new column (editing myaccount/orders.php template):
You will need to override through your active child theme, the myaccount/orders.php WooCommerce template.
At line 56 (just after 'order-number' code) you will insert the following and you will be able to add some specific content:
<?php elseif ( 'order-details' === $column_id ) : ?>
<?php
// HERE GOES YOUR CUSTOM CODE (DISPLAYED CONTENT)
?>
You can use the available WC_Order object $order…
How to get WooCommerce order details
Accepted answer is only partially correct. Adding the column via filter is fine, but populating the column is better done via a action hook. This is clean and matches best with column being added via filter. While editing the orders.php page via child theme works, it will lead to future effort to maintain after updates to woocommerce change this page making child theme version out of date.
To add your content to the column create an action hook along side the filter in functions.php. (or better yet in a plugin!)
add_action( 'woocommerce_my_account_my_orders_column_order-detail' , 'your_function_name2',10,1 );
//'woocommerce_my_account_my_orders_column_<column_id>'
function your_function_name2( $order ) {
// do stuff, ex: get_post_meta( $post->ID, 'key', true );
}
add_filter( 'woocommerce_account_orders_columns', 'new_orders_columns',10,1 );
function new_orders_columns( $columns) {
// Add new column. Kept simple to play nice with other filters.
$columns['order-detail'] = __( 'Details', 'woocommerce' );
return $columns;
}
see this similar question, best answer by Misha
Try to use this code
<?php
// ------------------
// 1. Register new endpoint to use for My Account page
// Note: Resave Permalinks or it will give 404 error
function bbloomer_add_premium_support_endpoint() {
add_rewrite_endpoint( 'premium-support', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'bbloomer_add_premium_support_endpoint' );
// ------------------
// 2. Add new query var
function bbloomer_premium_support_query_vars( $vars ) {
$vars[] = 'premium-support';
return $vars;
}
add_filter( 'query_vars', 'bbloomer_premium_support_query_vars', 0 );
// ------------------
// 3. Insert the new endpoint into the My Account menu
function bbloomer_add_premium_support_link_my_account( $items ) {
$items['premium-support'] = 'Premium Support';
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'bbloomer_add_premium_support_link_my_account' );
// ------------------
// 4. Add content to the new endpoint
function bbloomer_premium_support_content() {
echo '<h3>Premium WooCommerce Support</h3><p>Welcome to the WooCommerce support area. As a premium customer, you can submit a ticket should you have any WooCommerce issues with your website, snippets or customization. <i>Please contact your theme/plugin developer for theme/plugin-related support.</i></p>';
echo do_shortcode( ' /* your shortcode here */ ' );
}
add_action( 'woocommerce_account_premium-support_endpoint', 'bbloomer_premium_support_content' );

Removing the password field in woocommerce checkout page

I am trying to remove the password field in the checkout page.
Here is what I have tried (code is in my functions.php theme file):
// Hook in
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {
$checkout_fields['account']['required'] = false;
return $checkout_fields;
}
But it didn't work.
What is the hook for removing the password field in woocommerce checkout page?
Thanks.
The hook woocommerce_default_address_fields is only used for default address fields and not for the account fields.
To make the passwords fields optional you should need using woocommerce_checkout_fields hook, this way:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
// There is 2 password fields
fields['account']['account_password']['required'] = false;
fields['account']['account_password-2']['required'] = false;
return $fields;
}
To remove those passwords fields you should need to use unset() PHP function this way:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['account']['account_password']);
unset($fields['account']['account_password-2']);
return $fields;
}
But I am not sure that is really possible, as it's something mandatory in WooCommerce checkout process, when you have enabled the option to register in checkout page…
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Official Reference: Customizing checkout fields using actions and filters

Woocommerce remove asterisk from required fields on checkout page

Is there a way to remove the required asterisk from a billing field on the checkout page in php? I have the following code which isn't working.
add_filter( 'woocommerce_checkout_fields' , 'customize_fields' );
function customize_fields( $fields ) {
$fields['billing']['billing_address_2']['required'] = false;
return $fields;
}
It would probably be easier to do with CSS, and be cleaner and better for screen-readers:
.woocommerce-checkout abbr.required {
display: none;
}
The .woocommerce-checkout is a body class that is only appended to the checkout page so it won't affect any other woo page that might have the .required class in an abbr element.
To make the field not required with a function:
// Hook in
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {
$address_fields['address_2']['required'] = false;
return $address_fields;
}
There are two filters to manage checkout fields. You can use woocommerce_checkout_fields filter to make field "not-required", but the red asterisk will not be removed.
When dealing with default address fields with woocommerce_checkout_fields filter, some of your changes will not take effect, because woocommerce_default_address_fields filter and its default values may override your changes.
Only partly functional code:
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_address_1']['required'] = false ;
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
Address field (#1) is not required anymore, but still has red asterisk.
Fully functional code:
function custom_override_default_address_fields( $address_fields ) {
$address_fields['address_1'][ 'required' ] = false;
return $address_fields;
}
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
Now this field is not required, and red asterisk is gone.
Documentation says that "In some specific cases you will need to use the woocommerce_default_address_fields filter. This filter is applied to all billing and shipping default fields."
You have to add the following code to your functions.php file. The example below is for postal code:
/* Seteaza campul Cod postal ne-obligatoriu */
add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_postcode', 10, 1 );
function wc_npr_filter_postcode( $address_fields ) {
$address_fields['billing_postcode']['required'] = false;
return $address_fields;
}
/* End - Seteaza campul Cod postal ne-obligatoriu */

Categories