-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathallow-a-customer-to-update-order-status-from-my-account-in-woocommerce.code-snippets.json
1 lines (1 loc) · 2.75 KB
/
allow-a-customer-to-update-order-status-from-my-account-in-woocommerce.code-snippets.json
1
{"generator":"Code Snippets v2.14.0","date_created":"2020-07-24 16:35","snippets":[{"name":"Allow a customer to update order status from My Account in WooCommerce","desc":"<a href=\"https:\/\/stackoverflow.com\/questions\/63066590\/allow-customer-to-change-order-status-in-woocommerce\">https:\/\/stackoverflow.com\/questions\/63066590\/allow-customer-to-change-order-status-in-woocommerce<\/a>","tags":["order","status","update","my account","woocommerce"],"scope":"global","code":"function customer_order_confirm_args( $order_id ) {\n return array(\n 'url' => wp_nonce_url( add_query_arg( 'complete_order', $order_id ) , 'wc_complete_order' ),\n 'name' => __( 'Approve order', 'woocommerce' )\n );\n}\n\n\/\/ Add a custom action button to processing orders (My account > Orders)\nadd_filter( 'woocommerce_my_account_my_orders_actions', 'complete_action_button_my_accout_orders', 50, 2 );\nfunction complete_action_button_my_accout_orders( $actions, $order ) {\n if ( $order->has_status( 'processing' ) ) {\n $actions['order_confirmed'] = customer_order_confirm_args( $order->get_id() );\n }\n return $actions;\n}\n\n\/\/ Add a custom button to processing orders (My account > View order)\nadd_action( 'woocommerce_order_details_after_order_table', 'complete_action_button_my_accout_order_view' );\nfunction complete_action_button_my_accout_order_view( $order ){\n \/\/ Avoiding displaying buttons on email notification\n if( is_wc_endpoint_url( 'view-order' ) ) {\n $data = customer_order_confirm_args( $order->get_id() );\n\n echo '<div style=\"margin:16px 0 24px;\">\n <a class=\"button\" href=\"'.$data['url'].'\">'.$data['name'].'<\/a>\n <\/div>';\n }\n}\n\n\/\/ Change order status and display a message\nadd_action( 'template_redirect', 'action_complete_order_status' );\nfunction action_complete_order_status( $query ) {\n if ( ( is_wc_endpoint_url( 'orders' )\n || is_wc_endpoint_url( 'view-order' ) )\n && isset( $_GET['complete_order'] )\n && $_GET['complete_order'] > 1\n && isset($_GET['_wpnonce'])\n && wp_verify_nonce($_GET['_wpnonce'], 'wc_complete_order') )\n {\n $order = wc_get_order( absint($_GET['complete_order']) );\n\n if ( is_a($order, 'WC_Order') ) {\n \/\/ Change order status to \"completed\"\n $order->update_status( 'completed', __('Approved by the customer', 'woocommerce') ) ;\n\n \/\/ Add a notice (optional)\n wc_add_notice( sprintf( __( 'Order #%s has been approved', 'woocommerce' ), $order->get_id() ) );\n\n \/\/ Remove query args\n wp_redirect( esc_url( remove_query_arg( array( 'complete_order', '_wpnonce' ) ) ) );\n exit();\n }\n }\n}","priority":"10"}]}