Thêm nút xác nhận vào My Accout
add_filter( 'woocommerce_my_account_my_orders_actions', 'ts_confirm_order_my_account_orders_actions', 10, 2 );
function ts_confirm_order_my_account_orders_actions( $actions, $order ) {
if ( $order->has_status( 'processing' ) ) {
$actions['confirm-order'] = array(
'url' => wp_nonce_url( add_query_arg( array( 'confirm_order' => $order->get_id() ) ), 'woocommerce-confirm-order' ),
'name' => __( 'Confirm Order', 'woocommerce' )
);
}
return $actions;
}
add_action( 'template_redirect', 'ts_on_confirm_order_click_complete_order' );
function ts_on_confirm_order_click_complete_order( $order_id ) {
if ( isset( $_GET['confirm_order'], $_GET['_wpnonce'] ) && is_user_logged_in() && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'woocommerce-confirm-order' ) ) {
$order = wc_get_order( $_GET['confirm_order'] );
$order->update_status( 'completed', 'Order confirmed by customer' );//Thay đổi completed thành bất kỳ trạng thái nào muốn
}
}
Thêm nút xác nhận vào View Order
add_action( 'template_redirect', 'ts_on_confirm_order_click_complete_order' );
function ts_on_confirm_order_click_complete_order( $order_id ) {
if ( isset( $_GET['confirm_order'], $_GET['_wpnonce'] ) && is_user_logged_in() && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'woocommerce-confirm-order' ) ) {
$order = wc_get_order( $_GET['confirm_order'] );
$order->update_status( 'completed', 'Order confirmed by customer' );
}
}
add_action( 'woocommerce_order_details_before_order_table', 'ts_confirm_order_my_account_orders_actions' );
function ts_confirm_order_my_account_orders_actions( $order ) {
if ( $order->has_status( 'processing' ) ) {
$url = wp_nonce_url(
add_query_arg(
array( 'confirm_order' => $order->get_id() ),
wc_get_endpoint_url( 'view-order', $order->get_id(), wc_get_page_permalink( 'myaccount' ) )
),
'woocommerce-confirm-order'
);
echo '<a href="' . esc_url( $url ) . '" class="button confirm-order-button">' . __( 'Confirm Order', 'woocommerce' ) . '</a>';
}
}
Thêm nút XÁC NHẬN ở vị trí khác tùy chỉnh.
<?php global $wp;
if ( $order->has_status( 'processing' ) ) {
$url = wp_nonce_url(
add_query_arg(
array( 'confirm_order' => $order->get_id() ),
wc_get_endpoint_url( 'view-order', $order->get_id(), wc_get_page_permalink( 'myaccount' ) )
),
'woocommerce-confirm-order'
);
echo '<a href="' . esc_url( $url ) . '" class="button confirm-order-button">' . __( 'Confirm Order', 'woocommerce' ) . '</a>';
}
;?>
