Ecommerce Shopify WordPress Discussion

Update order status from order confirmation email in Woocommerce

I am trying to add 2 buttons in the order confirmation email: "Confirm order" and "Cancel order". When one of these buttons is pressed, the customer should be taken to a confirmation URL on my website and the status of the order should be updated accordingly ("completed" for confirmed orders and "canceled" for canceled). What I tried: Created a custom endpoint to handle the action triggered by the buttons: // Add this code to your theme's functions.php file or create a custom plugin. add_action('init', 'custom_order_actions'); function custom_order_actions() { add_rewrite_endpoint('confirm-order', EP_ROOT); add_rewrite_endpoint('cancel-order', EP_ROOT); } Edited order confirmation email template (buttons do not show up) <a href="<?php echo esc_url(wc_get_order_url($order->get_id()) . 'confirm-order'); ?>" class="button" style="color:#ffffff; background-color:#4CAF50; text-decoration: none;">Confirm Order</a> <a href="<?php echo esc_url(wc_get_order_url($order->get_id()) . 'cancel-order'); ?>" class="button" style="color:#ffffff; background-color:#ff0000; text-decoration: none;">Cancel Order</a> Handle button actions // Confirm Order add_action('template_redirect', 'confirm_order_action'); function confirm_order_action() { global $wp; if (isset($wp->query_vars['confirm-order'])) { // Update order status to completed $order_id = absint($_GET['order_id']); $order = wc_get_order($order_id); $order->update_status('completed'); // Redirect to confirmation page wp_redirect(home_url('/order-confirmed')); exit; } } // Cancel Order add_action('template_redirect', 'cancel_order_action'); function cancel_order_action() { global $wp; if (isset($wp->query_vars['cancel-order'])) { // Update order status to cancelled $order_id = absint($_GET['order_id']); $order = wc_get_order($order_id); $order->update_status('cancelled'); // Redirect to confirmation page wp_redirect(home_url('/order-cancelled')); exit; } } Is my logic correct or is there an easier way to accomplish this? Currently, I can't get the buttons to show in the confirmation email. I have added the code in woocommerce/emails/templates.
There are some mistakes and missing things in your code… Also, instead of overriding the template file customer-processing-order.php, always try to use available hooks… Try the following replacement code instead (for "Customer processing Order" notification): // Add custom endpoints add_action('init', 'custom_wc_order_endpoints'); function custom_wc_order_endpoints() { add_rewrite_endpoint('confirm-order', EP_ROOT ); add_rewrite_endpoint('cancel-order', EP_ROOT ); } // Set custom endpoints as query variables add_filter( 'query_vars', 'custom_wc_order_query_vars', 0 ); function custom_wc_order_query_vars( $vars ) { $vars[] = 'confirm-order'; $vars[] = 'cancel-order'; return $vars; } // Flush rewrite rules for those custom endpoints add_action( 'wp_loaded', 'flush_rewrite_rules_enable_custom_endpoints' ); function flush_rewrite_rules_enable_custom_endpoints() { flush_rewrite_rules(); } // Display 2 custom buttons in "Customer processing Order" email notification add_action('woocommerce_email_order_meta', 'add_linked_buttons_after_email_order_details', 5, 4 ); function add_linked_buttons_after_email_order_details( $order, $sent_to_admin, $plain_text, $email ) { // Target "Customer processing Order" notification if( $email->id !== 'customer_processing_order' ) return; printf( '<a href="%s" class="button" style="color:#ffffff; background-color:#4CAF50; text-decoration:none;">%s</a>', wc_get_endpoint_url('confirm-order', $order->get_id(), site_url() ), __('Confirm Order', 'woocommerce') ); printf( '<a href="%s" class="button" style="color:#ffffff; background-color:#ff0000; text-decoration:none;">%s</a>', wc_get_endpoint_url('cancel-order', $order->get_id(), site_url() ), __('Cancel Order', 'woocommerce') ); } // Update order status once customer has clicked on a custom button add_action('template_redirect', 'custom_order_actions'); function custom_order_actions() { global $wp; if ( isset($wp->query_vars['confirm-order']) ) { $order_id = absint($wp->query_vars['confirm-order']); $order_status = 'completed'; } elseif ( isset($wp->query_vars['cancel-order']) ) { $order_id = absint($wp->query_vars['cancel-order']); $order_status = 'cancelled'; } if ( isset($order_status, $order_id) && $order_id > 0 ) { $order = wc_get_order($order_id); if ( is_a($order, 'WC_Order') ) { $order->update_status($order_status); // Update order status } } } Code goes in functions.php file of your child theme (or in a plugin). Tested and works. IMPORTANT: To get this code working may require to refresh WordPress Permalinks by going to: Settings > Permalinks and click on "Save changes" button. Useful: Target a specific email notification with the email id in WooCommerce

January 7, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.