How To Add Cancel Order Button In Woocommerce

Add Cancellation Button For Customers After Payment In Woocommerce

How To Add Cancel Order Button In Woocommerce

Like today’s e-commerce platforms, allowing customers to cancel orders after payment can help improve the shopping experience. While this may in the short term reduce your revenue, it contributes to an increase in conversion rates because customers will then trust more.

Today, flatsomea-z will show you how to add a “Cancel Order” button in your customer’s account page with a very simple code.

Add Cancellation Button For Customers After Payment In Woocommerce
Add Cancel Order Button For Customers After Payment In Woocommerce

You just need to copy and paste the following code into the functions.php file and save it!

//Allow customers to cancel orders after payment
add_filter( 'woocommerce_valid_order_statuses_for_cancel', 'custom_valid_order_statuses_for_cancel', 10, 2 );
function custom_valid_order_statuses_for_cancel( $statuses, $order ){

// Set the statuses that you allow customers to cancel
$custom_statuses = array( 'completed', 'pending', 'processing', 'on-hold', 'failed' );

// Set the number of days to be allowed to cancel since placing an order
$duration = 3; // 3 days

// Get Order ID and WC_Order
if( isset($_GET['order_id']))
$order = wc_get_order( absint( $_GET['order_id'] ) );

$delay = $duration*24*60*60; // (duration in seconds)
$date_created_time = strtotime($order->get_date_created()); // Creation date time stamp
$date_modified_time = strtotime($order->get_date_modified()); // Modified date time stamp
$now = strtotime("now"); // Now time stamp

// Using Creation date time stamp
if ( ( $date_created_time + $delay ) >= $now ) return $custom_statuses;
else return $statuses;
}

Good luck!

2 thoughts on “How To Add Cancel Order Button In Woocommerce

  1. Tien Dung says:

    Bác có hướng dẫn nào để thay đổi form theo dõi đơn hàng từ mặc định là email thành số điện thoại không ạ?

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *