При смене статуса заказа в WooCommerce есть «хук» — woocommerce_order_status_changed на который можно повесить действие.
Пример:
add_action(‘woocommerce_order_status_changed’, ‘orderStatusChanged’, 10, 4);
Пример функции обработчика с параметрами:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * Возникает при изменении заказа * @param int $order_id ИД заказа * @param string $old_status Предыдущий статус заказа * @param string $new_status Новый статус заказа * @param string $order объект класса WC_Order */ function orderStatusChanged($order_id, $old_status, $new_status, $order) { /** * Здесь какие либо действия которые нужно инициировать при изменении статуса заказа */ return $order_id; } |
В WooCommerce хук определён в файле/wp-content/plugins/woocommerce/includes/class-wc-order.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/** * Handle the status transition. */ protected function status_transition() { $status_transition = $this->status_transition; // Reset status transition variable. $this->status_transition = false; if ( $status_transition ) { try { do_action( 'woocommerce_order_status_' . $status_transition['to'], $this->get_id(), $this ); if ( ! empty( $status_transition['from'] ) ) { /* translators: 1: old order status 2: new order status */ $transition_note = sprintf( __( 'Order status changed from %1$s to %2$s.', 'woocommerce' ), wc_get_order_status_name( $status_transition['from'] ), wc_get_order_status_name( $status_transition['to'] ) ); do_action( 'woocommerce_order_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this ); do_action( 'woocommerce_order_status_changed', $this->get_id(), $status_transition['from'], $status_transition['to'], $this ); } else { /* translators: %s: new order status */ $transition_note = sprintf( __( 'Order status set to %s.', 'woocommerce' ), wc_get_order_status_name( $status_transition['to'] ) ); } // Note the transition occurred. $this->add_order_note( trim( $status_transition['note'] . ' ' . $transition_note ), 0, $status_transition['manual'] ); } catch ( Exception $e ) { $logger = wc_get_logger(); $logger->error( sprintf( 'Status transition of order #%d errored!', $this->get_id() ), array( 'order' => $this, 'error' => $e, ) ); $this->add_order_note( __( 'Error during status transition.', 'woocommerce' ) . ' ' . $e->getMessage() ); } } } |