WooCommerce automatically sending cancelled orders email - wordpress

I received five cancellation notification emails came, all about the same time, and none of them canceled their orders. I called each and every customer and they all told me that they did not cancel the order. The orders were not actually canceled in Wordpress but for some reason, email notifications for cancellation were sent.
Please help me to resolve this issue.
Thanks!

add_action("woocommerce_order_status_changed", "my_custom_notification");
function my_custom_notification($order_id, $checkout=null) {
global $woocommerce;
$order = new WC_Order( $order_id );
if($order->status === 'cancelled' ) {
// Create a mailer
$mailer = $woocommerce->mailer();
$message_body = __( 'Hello world!!!' );
$message = $mailer->wrap_message(
// Message head and message body.
sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message_body );
// Cliente email, email subject and message.
$mailer->send( $order->billing_email, sprintf( __( 'Order %s received' ), $order->get_order_number() ), $message );
}
}
You can set your own notification mail on order get cancelled(order status changing to cancelled)

Related

Send Email notifications when a user completes a successful order using a certain code l Woocommerce [duplicate]

How to send order notification to a business partner when a specific coupon is used?
I found a solution for the instance when the coupon is applied here :
Send an email notification when a specific coupon code is applied in WooCommerce
However, I need to find a solution for when after order is submitted, because the order is not always submitted after coupon is applied.
Each coupon will have its own email address.
First we add a setting field to admin Coupon pages, to set an email recipient for for a coupon:
// Add a custom field to Admin coupon settings pages
add_action( 'woocommerce_coupon_options', 'add_coupon_text_field', 10 );
function add_coupon_text_field() {
woocommerce_wp_text_input( array(
'id' => 'email_recipient',
'label' => __( 'Email recipient', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Send an email notification to a defined recipient' ),
'desc_tip' => true, // Or false
) );
}
// Save the custom field value from Admin coupon settings pages
add_action( 'woocommerce_coupon_options_save', 'save_coupon_text_field', 10, 2 );
function save_coupon_text_field( $post_id, $coupon ) {
if( isset( $_POST['email_recipient'] ) ) {
$coupon->update_meta_data( 'email_recipient', sanitize_text_field( $_POST['email_recipient'] ) );
$coupon->save();
}
}
Then email is sent for each applied coupon to a submitted order, if the email recipient has been set for the applied coupon.
Caution! choose only one of the following functions:
For woocommerce versions Up to 4.3 (new hook)
// For Woocommerce version 4.3+
add_action( 'woocommerce_checkout_order_created', 'custom_email_for_orders_with_applied_coupon' );
function custom_email_for_orders_with_applied_coupon( $order ){
$used_coupons = $order->get_used_coupons();
if( ! empty($used_coupons) ){
foreach ( $used_coupons as $coupon_code ) {
$coupon = new WC_Coupon( $coupon_code ); // WC_Coupon Object
$recipient = $coupon->get_meta('email_recipient'); // get recipient
if( ! empty($recipient) ) {
$subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );
wp_mail( $recipient, $subject, $content ); // Send email
}
}
}
}
Or for all WooCommerce versions (since version 3.0)
// For all Woocommerce versions (since 3.0)
add_action( 'woocommerce_checkout_update_order_meta', 'custom_email_for_orders_with_applied_coupon' );
function custom_email_for_orders_with_applied_coupon( $order_id ){
$order = wc_get_order( $order_id );
$used_coupons = $order->get_used_coupons();
if( ! empty($used_coupons) ){
foreach ( $used_coupons as $coupon_code ) {
$coupon = new WC_Coupon( $coupon_code ); // WC_Coupon Object
$recipient = $coupon->get_meta('email_recipient'); // get recipient
if( ! empty($recipient) ) {
$subject = sprintf( __('Coupon "%s" has been applied'), $coupon_code );
$content = sprintf( __('The coupon code "%s" has been applied by a customer'), $coupon_code );
wp_mail( $recipient, $subject, $content ); // Send email
}
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

retrieve_password_message is not working as i use for password retrive message

I have used the bellow code. But it is not working.
function site_change_password_mail_message( $message, $key ) {
$message = __( 'Hi ###USERNAME###,
This notice confirms that your password was changed on JDRF. If you did not change your password, please
contact JDRF Support at siteurl.com/inquiry. This email has been sent to ###EMAIL###.
Regards,
All at siteurl.com
[Application Homepage, ex. ###SITEURL###]');
return $message;
}
add_filter( 'retrieve_password_message', 'site_change_password_mail_message', 10, 3 );
Should i change this ? I am still getting this message.
Hi ranjit,
Someone has requested a new password for the following account on K Gems & Crystals:
Username: username
If you didn't make this request, just ignore this email. If you'd like to proceed:
Click here to reset your password
Thanks for reading.
If you are going to send an email after registering a user and you want to change the default email from wordpress. you should use this little code.
add_fileter("wp_new_user_notification_email", "callback",10,3);
function callback( $message, $user, $blogname) {
$message['headers'] = array('Content-Type: text/html; charset=UTF-8');
$message['subject'] = sprintf( '[%s] Site Name.', $blogname );
$message['message'] = sprintf( "%s ( %s ) has registerd to your blog %s.",
$user->user_login, $user->user_email, $blogname );
return $message;
}
If it is for the email sending the change of password
function filter_retrieve_password_message( $message, $key, $user_login, $user_data ) {
// make filter magic happen here...
return $message;
};
// add the filter
add_filter( 'retrieve_password_message', 'filter_retrieve_password_message', 10, 4 );
I can see that you are not using proper arguments to the filter what you are using. You need to use proper argument to the filter. Try this code
add_filter( 'retrieve_password_message', 'retrive_reset_password_msg', 10, 4 );
function retrive_reset_password_msg( $message, $key, $user_login, $user_data ) {
$message = __( 'Hi ###USERNAME###,
This notice confirms that your password was changed on JDRF. If you did not change your password, please
contact JDRF Support at siteurl.com/inquiry. This email has been sent to ###EMAIL###.
Regards,
All at siteurl.com
[Application Homepage, ex. ###SITEURL###]');
return $message;
}

Update WooCommerce order status via WooCommerce callback

I created a custom payment gateway plugin that caters payment for WooCommerce site orders via a third party payment gateway. Once the third party received the payment of the customer it then sends post data into your designated URL so you can process/update your database. Updating order status on WooCommerce callback via Get method works but not with Post.
This what i am working with
add_action( 'woocommerce_api_callback', array( $this, 'thirdparty_response' ));
function thirdparty_response()
{
global $woocommerce;
if(isset($_POST['order_id']) && isset($_POST['order_status'])) //Parameters sent by third party gateway
{
$order_status = $_POST['order_status'];
$order_id = $_POST['order_id'];
$order = new WC_Order( $order_id );
if ($order_status == "success")
{
$order->update_status('processing', __('Payment Received', 'woothemes'));
}
else
{
$order->update_status('failed', __('Payment Failed', 'woothemes'));
}
}
}
Thank you and I hope you can help me.
Have you created your Gateway class? This is a snippet from WC Paypal Gateway, and to activate the check_response you have to configure your 3rd party Payment Gateway service to callback your URL formed like this (you need to replace the name of your class in both callback URL and in the hook name):
$your_callback = str_replace( 'https:', 'http:', add_query_arg( 'wc-api', 'WC_Gateway_Paypal', home_url( '/' ) ) );
public function __construct( $sandbox = false, $receiver_email = '' ) {
add_action( 'woocommerce_api_wc_gateway_paypal', array( $this, 'check_response' ) );
add_action( 'valid-paypal-standard-ipn-request', array( $this, 'valid_response' ) );
$this->receiver_email = $receiver_email;
$this->sandbox = $sandbox;
}

woocommerce get 'billing_phone' field on checkout page

add_action('woocommerce_order_status_completed', 'my_order_status_completed_sms', 10, 1);
function my_order_status_completed_sms($order_id){
$order = wc_get_order($order_id);
$user_id = $order->get_customer_id();
if($user_id){
$phone_number = get_user_meta($user_id, 'billing_phone', true);
if($phone_number){
cosmosfarm_members_sms_send($phone_number, '#'.$order_id.' is complete. thank you!');
}
}
}
When someone purchases from a woocommerce, woocommerce send sms by plug-in.
but there's some issues.
If the buyer is a member, sms will be sent out normally, no problem.
However, if the buyer is not a guest, no sms will be sent.
What's the problem?
Your function is dependend upon having user_id and getting the user_meta for that user.
function my_order_status_completed_sms( $order_id ) {
// Get the order object
$order = wc_get_order( $order_id );
// Get the billing phone number from the order
$phone_number = $order->get_billing_phone();
if ( $phone_number ) {
cosmosfarm_members_sms_send( $phone_number, '#' . $order_id . ' is complete. thank you!' );
}
}
Using the order object, the biling phone is there if entered for either customer or guest.

Order status still “Pending Payment” after successful payment (Custom payment gateway)

I was develop a custom payment gateway for woocommerce, everything is good but after payment success the order still "pending payment"
In the __construct() function I check for payment provider respond
<?php
//when payment done and redirected with payment reference code
if(isset($_REQUEST['transaction_id'])){
paytabs_set_cookie('transaction_id', $_REQUEST['transaction_id']);
$order = new WC_Order($order_id);
$this->order = $order;
$this->complete_transaction($order->get_id(), $_REQUEST['transaction_id']);
}
and here is the complete_transaction() function
<?php
/*
When transaction completed it is check the status
is transaction completed or rejected
*/
function complete_transaction($order_id, $transaction_id) {
global $woocommerce;
$order = new WC_Order( $order_id );
$request_string=array(
'secret_key' => $_COOKIE['secret_key'],
'merchant_email' => $_COOKIE['merchant_email'],
'transaction_id' => $transaction_id,
'order_id' => $order_id
);
$gateway_url=$this->liveurl.'apiv2/verify_payment_transaction';
$getdataresponse=$this->sendRequest($gateway_url, $request_string);
$object=json_decode($getdataresponse, true);
if (isset($object->response_code)) {
//if get response successfull
if($object->response_code == '100'){
// thankyou and set error message
$this->msg['class'] = 'woocommerce_message';
$check=$order->payment_complete();
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
$woocommerce->cart->empty_cart();
wc_add_notice( '' . __( 'Thank you for shopping with us. Your account has been charged and your transaction is successful.
We will be shipping your order to you soon.', 'woocommerce' ), 'success' );
wp_redirect( $this->get_checkout_order_received_url( $order ) );
exit;
}else{
// Change the status to pending / unpaid
$order->update_status('failed', __('Payment Cancelled', 'error'));
// Add error for the customer when we return back to the cart
$message=$object->result;
wc_add_notice( '<strong></strong> ' . __($message, 'error' ), 'error' );
// Redirect back to the last step in the checkout process
wp_redirect( $this->get_cancel_order_url( $order ) );
exit;
}
}
}
But the order status still "pending payment".
now I need to change the order status to "Completed" after check the payment provider respond.
Fixed
The problem because of this line
wp_redirect( $this->get_checkout_order_received_url( $order ) );
it must be $order instead of $this so the line is like this
wp_redirect( $order->get_checkout_order_received_url( $order ) );

Resources