Change woocommerce 'coupon does not exist! error message - php

I would like to change the default woocommerce error message that I get when client enters a coupon that does not exist Coupon ā€œ%sā€ does not exist!. Is it possible to do this directly in the function.php file of my child theme? If so it would be greatly appreciated to get some suggestions as to the code to use and where. I have tried to add the below code to the funtion.php file, but it didn't work.
add_filter( 'woocommerce_coupon_error','coupon_error_message_change', 10, 3 );
public function coupon_error_message_change( $err, $err_code, $parm )
{
switch ( $err_code ) {
case 105:
/* translators: %s: coupon code */
$err = sprintf( __( 'Coupon "%s" does not test!', 'woocommerce' ), $parm->get_code() );
break;
}
return $err;
}

add_filter( 'woocommerce_coupon_error','coupon_error_message_change',10,3 );
function coupon_error_message_change($err, $err_code, $WC_Coupon) {
switch ( $err_code ) {
case $WC_Coupon::E_WC_COUPON_NOT_EXIST:
$err = 'your message';
}
return $err;
}

Related

Overwrite code in includes folder of woocommerce [duplicate]

I am using this to remove the "Cart is updated" message in WooCommerce:
add_filter( 'woocommerce_add_message', '__return_false');
But the "Shipping costs updated" message is still displayed. How can I remove this message?
In your specific case, in shortcodes/class-wc-shortcode-cart.php we find:
wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
Which in turn calls the wc_add_notice() function:
...
// Backward compatibility.
if ( 'success' === $notice_type ) {
$message = apply_filters( 'woocommerce_add_message', $message );
}
$message = apply_filters( 'woocommerce_add_' . $notice_type, $message );
...
(This code was partly copied from: includes/wc-notice-functions.php)
So instead of using the woocommerce_add_message filter hook you can use the woocommerce_add_"$notice_type" filter hook.
$notice_type = the name of the notice type - either error, success (default) or notice.
In your case, $notice_type
corresponds with notice. So then just compare the message, if this is sufficient, we return false:
function filter_woocommerce_add( $message ) {
// Equal to (Must be exactly the same).
// If the message is displayed in another language, adjust where necessary!
if ( $message == 'Shipping costs updated.' ) {
return false;
}
return $message;
}
add_filter( 'woocommerce_add_notice', 'filter_woocommerce_add', 10, 1 );
// OR - Use the desired filter, depending on the name of the notice type (default = success)
// add_filter( 'woocommerce_add_error', 'filter_woocommerce_add', 10, 1 );
// add_filter( 'woocommerce_add_success', 'filter_woocommerce_add', 10, 1 );

Remove or hide "wc_add_notice" messages in WooCommerce

I am using this to remove the "Cart is updated" message in WooCommerce:
add_filter( 'woocommerce_add_message', '__return_false');
But the "Shipping costs updated" message is still displayed. How can I remove this message?
In your specific case, in shortcodes/class-wc-shortcode-cart.php we find:
wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
Which in turn calls the wc_add_notice() function:
...
// Backward compatibility.
if ( 'success' === $notice_type ) {
$message = apply_filters( 'woocommerce_add_message', $message );
}
$message = apply_filters( 'woocommerce_add_' . $notice_type, $message );
...
(This code was partly copied from: includes/wc-notice-functions.php)
So instead of using the woocommerce_add_message filter hook you can use the woocommerce_add_"$notice_type" filter hook.
$notice_type = the name of the notice type - either error, success (default) or notice.
In your case, $notice_type
corresponds with notice. So then just compare the message, if this is sufficient, we return false:
function filter_woocommerce_add( $message ) {
// Equal to (Must be exactly the same).
// If the message is displayed in another language, adjust where necessary!
if ( $message == 'Shipping costs updated.' ) {
return false;
}
return $message;
}
add_filter( 'woocommerce_add_notice', 'filter_woocommerce_add', 10, 1 );
// OR - Use the desired filter, depending on the name of the notice type (default = success)
// add_filter( 'woocommerce_add_error', 'filter_woocommerce_add', 10, 1 );
// add_filter( 'woocommerce_add_success', 'filter_woocommerce_add', 10, 1 );

Woocommerce Login: If user account does not exist, display error

Currently when a user attempts to login with an email/username which does not exist, this default woocommerce error is displayed:
I am trying check: If email/username DOES NOT exist, display different message.
e.g. "No account exists with this email, please create one."
The hook woocommerce_registration_error_email_exists checks if a user tried to register with an email which already exists, so I am trying to reverse this with !email_exists - Can this be applied to the login field?
My code below is not triggering and still displays the default message:
add_filter( 'woocommerce_registration_error_email_exists', 'no_account_found' );
function no_account_found($email, $username = '', $password = '' ){
if ( !email_exists( $email ) ) {
return new WP_Error( 'registration-error-email-exists', apply_filters( 'woocommerce_registration_error_email_exists', __( 'No account found with this email. Please create one.', 'woocommerce' ), $email ) );
}
}
email_exists( string $email ) returns the user's ID on success, and false on failure.
So try change your condition like this :
<?php
add_filter( 'woocommerce_registration_error_email_exists', 'no_account_found' );
function no_account_found($email, $username = '', $password = '' ){
if (email_exists($email) ) {
//the email exists do something
}else{
//email does not exist
return new WP_Error( 'registration-error-email-exists', apply_filters( 'woocommerce_registration_error_email_exists', __( 'No account found with this email. Please create one.', 'woocommerce' ), $email ) );
}
}

Adding custom field in WooCommerce new user registration email

I wrote a custom user registration form that handles login and registration for my WordPress Woocommerce web site.
When a user registers via my custom form handler I triggered Woocommerce to send the new user an email. But I need to add an activation code on the email.
Is it possible to do so?
Thanks
First you need to register this activation code in the user metadata.
For example using in your code something like:
// Set your activation code in the user meta
$activation_code = 'dHu12548-oh$r' // example for a generated activation code
// Saving the activation code in user meta data.
update_user_meta( $user_id, 'activation_code', $activation_code );
Then you can use a custom function hooked in woocommerce_email_header action hook:
add_action( 'woocommerce_email_header', 'custom_email_new_account', 100, 2 );
function custom_email_new_account( $email_heading, $email ) {
if ( 'customer_new_account' === $email->id ){
$user_id = $email->object->ID;
$activation_code = get_user_meta( $user_id, 'activation_code', $true );
// Displaying the activation code
printf( __( 'Here is your activation code: %s', 'woocommerce' ), '<strong>' . esc_html( $activation_code ) . '</strong>' );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Or you can insert in the WooCommerce template customer-new-account.php this similar code:
<?php
if ( 'customer_new_account' === $email->id ){
$user_id = $email->object->ID;
$activation_code = get_user_meta( $user_id, 'activation_code', $true );
// Displaying the activation code
printf( __( 'Here is your activation code: %s', 'woocommerce' ), '<strong>' . esc_html( $activation_code ) . '</strong>' );
}
?>

How to login with only email and password in WooCommerce

Plugin Name: WooCommerce
plugins\woocommerce\templates\global\form-login.php
<label for="username"><?php _e( 'Email', 'woocommerce' ); ?> <span class="required">*</span></label>
It display email instead of username or email which i wanted
includes\class-wc-form-handler.php
if ( empty( $username ) ) {
throw new Exception( '<strong>' . __( 'Error:', 'woocommerce' ) . '</strong> ' . __( 'Email is required.', 'woocommerce' ) );
}
It display validation for email correct but it login with username also
I want it to login only with email and password before checkout
If you look closely at
wp-content/plugins/woocommerce/includes/class-wc-form-handler.php at
line num 878 there is a filter
woocommerce_process_login_errors, which accepts validations
error, username and password. So you can overwrite this by this filter
help.
Here is the code:
// define the woocommerce_process_login_errors callback
function filter_woocommerce_process_login_errors($validation_error, $post_username, $post_password)
{
//if (strpos($post_username, '#') == FALSE)
if (!filter_var($post_username, FILTER_VALIDATE_EMAIL)) //<--recommend option
{
throw new Exception( '<strong>' . __( 'Error', 'woocommerce' ) . ':</strong> ' . __( 'Please Enter a Valid Email ID.', 'woocommerce' ) );
}
return $validation_error;
}
// add the filter
add_filter('woocommerce_process_login_errors', 'filter_woocommerce_process_login_errors', 10, 3);
Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works. in WooCommerce 2.6.X
Hope this helps!

Categories