Thêm nút hủy & xác nhận vào EMAIL cho WOO

Nguyen Eric
0
12/12/24
12/12/2024

Bỏ code này vào function child theme

// 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
        }
    }
}

Nút xác nhận bỏ vào bất cứ nơi nào

<?php global $wp;
 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() ), __('Xác nhận', 'woocommerce') );
	 return $vars
;?>

Nút xác nhận chỉ hiển thị có điều kiệu khi đơn hàng đang ở trạng thái pending, on-hold, processing

<?php global $wp;
 $statuses = array( 'pending', 'on-hold', 'processing' ); 
 if ($order->has_status($statuses) ) {
 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() ), __('Xác nhận', 'woocommerce') );
	 return $vars;
 };?>
Tags :
Tags: