is it posible to make woo 'order_comments' field required - and show message If the field is not filled
I have tried the following code but I can not get it to work.
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['my_field_name'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
Hope there is someone who can help
Add below code in "functions.php" file of an active child theme.
// Make order notes required
add_filter( 'woocommerce_checkout_fields' , 'wc_override_checkout_fields' );
function wc_override_checkout_fields( $fields ) {
$fields['order']['order_comments']['required'] = true;
return $fields;
}
Reference https://gist.github.com/MindyPostoff/cbf34de936445972737f
Change the action from 'woocommerce_checkout_process' to 'woocommerce_after_checkout_validation'.
add_action('woocommerce_after_checkout_validation', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['order_comments'] )
wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
Related
I need to prevent two specific coupons from being used together. I successfully implemented this code, which prevents stacking these coupons on the cart page:
add_action( 'woocommerce_before_cart', 'check_coupon_stack' );
function check_coupon_stack() {
$coupon_code_1 = 'mycode1';
$coupon_code_2 = 'mycode2';
if ( WC()->cart->has_discount( $coupon_code1 ) && WC()->cart->has_discount( $coupon_code2) ) {
WC()->cart->remove_coupon( $coupon_code2 );
$notice_text = 'Discount code '.$coupon_code1.' cannot be combined with code '.$coupon_code2.'. Code '.$coupon_code2.' removed.';
wc_print_notice( $notice_text, 'error' );
wc_clear_notices();
}
}
However, this does not prevent stacking on the checkout page, which follows the cart page.
I have tried simply adding:
add_action( 'woocommerce_before_checkout_form', 'check_coupon_stack' );
But that doesn't make this work on the checkout page. What more is needed?
WooCommerce contains multiple hooks that apply to coupons, woocommerce_applied_coupon is one of them, which is very suitable for your question.
Furthermore, your current code only works in one direction, which is when $coupon_code_1 is used, $coupon_code_2 is removed. However, this is not applied in the reverse direction while you indicate in your question that you want to prevent two specific coupons from being used together.
This is taken into account in my answer, so you get:
function action_woocommerce_applied_coupon( $coupon_code ) {
// Settings
$coupon_code_1 = 'coupon1';
$coupon_code_2 = 'coupon2';
// Initialize
$combined = array( $coupon_code_1, $coupon_code_2 );
// Checks if coupon code exists in an array
if ( in_array( $coupon_code, $combined ) ) {
// Get applied coupons
$applied_coupons = WC()->cart->get_applied_coupons();
// Computes the difference of arrays
$difference = array_diff( $combined, $applied_coupons );
// When empty
if ( empty( $difference ) ) {
// Shorthand if/else - Get correct coupon to remove
$remove_coupon = $coupon_code == $coupon_code_1 ? $remove_coupon = $coupon_code_2 : $remove_coupon = $coupon_code_1;
// Remove coupon
WC()->cart->remove_coupon( $remove_coupon );
// Clear Notices
wc_clear_notices();
// Error message
$error = sprintf( __( 'Discount code "%1$s" cannot be combined with code "%2$s". Code "%2$s" removed.', 'woocommerce' ), $coupon_code, $remove_coupon );
// Show error
wc_print_notice( $error, 'error' );
}
}
}
add_action( 'woocommerce_applied_coupon', 'action_woocommerce_applied_coupon', 10, 1 );
Is there a hook to change the Total title from the WooCommerce order received page's order overview? Please see the below image to understand better
Yes, you can use the gettext WordPress filter to "translate" (rename in your case) that string of text.
Your string is inside the thankyou.php WooCommerce template:
esc_html_e( 'Total:', 'woocommerce' );
Based on WooCommerce: How to Translate / Rename Any String tutorial, the right code should be the following:
add_filter( 'gettext', 'bbloomer_translate_woocommerce_strings', 9999, 3 );
function bbloomer_translate_woocommerce_strings( $translated, $untranslated, $domain ) {
if ( ! is_admin() && 'woocommerce' === $domain ) {
switch ( $translated ) {
case 'Total:':
$translated = 'Whatever:';
break;
}
}
return $translated;
}
I have created a custom checkout field for WooCommerce. I have also added the validation. But after clicking on place order, my custom fields also checked if it is validated or not. But, I want to check if my custom field is valid or not at the end. I mean, all other required fields will be check and after that my custom field will be checked.
Below is my code structure:
function otp_func($checkout) {
.............................
}
add_action( 'woocommerce_after_checkout_billing_form', 'otp_func', 10 );
add_action('woocommerce_checkout_process','my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
..........................
}
And below is my validation code which I want to run at the end if all the other fields validate successfully:
if (!$_POST['otp_field']) {
wc_add_notice( __( 'OTP field is blank' ), 'error' );
} else if ( $_POST['otp_field'] != $otp_verify_report ) {
wc_add_notice( __( 'Invalid OTP or OTP has been expired!' ), 'error' );
}
Use below code to validate new field.
Add Below code in theme functions.php
add_action( 'woocommerce_after_checkout_validation', 'wc_check_confirm_password_matches_checkout', 10, 2 );
function wc_check_confirm_password_matches_checkout( $posted ) {
$checkout = WC()->checkout;
if ( ! empty( $posted['otp_field'] ) ) {
wc_add_notice( __( 'OTP field is blank' ), 'error' );
} else if ( $posted['otp_field'] != $otp_verify_report ) {
wc_add_notice( __( 'Invalid OTP or OTP has been expired!' ), 'error' );
}
}
You can have a try with:
function customise_checkout_field_process()
{
if (!$_POST['the_customised_field_name']) wc_add_notice(__('the warning message') , 'error');
}
I created another custom statuses to wc statuses, and I want this status to leave the order "editable", so I found this code in the file abstract-wc-order.php:
public function is_editable() {
return apply_filters( 'wc_order_is_editable', in_array( $this->get_status(), array( 'pending', 'on-hold', 'auto-draft', 'failed' ) ), $this );
}
Now if I understand currently, I need to add my custom status to the array above, but I'm not really sure how to do this - to add my status to the above array by hooking the filter wc_order_is_editable, I will really appreciate to any help!
Probably something like this will work:
function so_39193164_is_editable( $editable, $order ) {
if( $order->get_status() == 'your-new-status' ){
$editable = true;
}
return $editable;
}
add_filter( 'wc_order_is_editable', 'so_39193164_is_editable', 10, 2 );
If the order status is your new status then $editable will be true. If not, it will be whatever WooCommerce already decided in the is_editable() method.
on my baked goods site, I have WooCommerce installed and another plugin called Order Delivery Date for WooCommerce.
I installed the second plugin so my customers would be able to choose a delivery date for their items, however, I am trying to make the form field a required field. So far, I've just been able to make the field look like a required field, but have not figured out how to make sure that it is actually enforced. Any ideas?
Also, if anyone is familiar with WooCommerce, do you know how I would be able to make it so that customers receive this delivery date information in their order confirmation emails?
Thank you in advance!
My site: www.monpetitfour.com
You should try to had something like that :
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// You can make your own control here
if ( ! $_POST[ 'e_deliverydate' ] )
wc_add_notice( __( 'Please select a delivery date' ), 'error' );
}
For the email, the easiest is to save the meta value ( I think it's already done by your plugin). Then you need to copy the template email (customer-processing-order.php) on your theme and change in the template :
<?php $delivery_date = get_post_meta( $order->id, 'custom_field_date', true);
// If the plugin is well developed, you can't directly use magic getters :
// $delivery_date = $order->e_deliverydate;
// Can only by use if the post meta start with _
?>
Your delivery date is <?php echo $delivery_date ?>
You can also use
date_i18n( woocommerce_date_format(), strtotime( $delivery_date ) );
In order to format the date correctly.
On the code above, you just need to find the name of the custom field used by the plugin ( you can search easily on the table wp_postmeta searching by an existing order (should be _e_deliverydate).
Add the following code to your theme's functions.php file
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['e_deliverydate'] )
wc_add_notice( __( 'Please select a delivery date.' ), 'error' );
}
Now to get the email to show the custom field,
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['Delivery Date'] = '_e_deliverydate';
return $keys;
}
EDIT : Seems the field value isn't being saved to the database, try saving it explicitly
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['e_deliverydate'] ) ) {
update_post_meta( $order_id, '_e_deliverydate', sanitize_text_field( $_POST['e_deliverydate'] ) );
}
}