add_available_gateways(); } /** * We need to add the POS gateways to the available gateways for the edit order dropdown. * It's possible another plugin could just re-init after us, but this will work for most cases. */ public function add_available_gateways() { // @phpstan-ignore-next-line if ( WC()->payment_gateways() ) { $payment_gateways = WC()->payment_gateways->payment_gateways; $settings = woocommerce_pos_get_settings( 'payment_gateways' ); // Add POS gateways to the available gateways for the edit order dropdown. foreach ( $payment_gateways as $gateway ) { if ( isset( $settings['gateways'][ $gateway->id ] ) && $settings['gateways'][ $gateway->id ]['enabled'] ) { $gateway->enabled = 'yes'; } } // Directly set the modified gateways back to the WooCommerce Payment Gateways instance. WC()->payment_gateways->payment_gateways = $payment_gateways; } } /** * Makes POS orders editable by default. * * @param bool $is_editable Whether the order is editable. * @param WC_Abstract_Order $order The order object. * * @return bool */ public function wc_order_is_editable( $is_editable, WC_Abstract_Order $order ) { if ( 'pos-open' == $order->get_status() ) { return true; } return $is_editable; } /** * Add cashier select to order page. * * @param WC_Abstract_Order $order Order object. */ public function add_cashier_select( $order ): void { // only show if order created by POS. if ( ! woocommerce_pos_is_pos_order( $order ) ) { return; } $cashier_id = $order->get_meta( '_pos_user' ); // Create nonce for security. wp_nonce_field( 'pos_cashier_select_action', 'pos_cashier_select_nonce' ); echo '

'; echo ''; echo ''; echo '

'; } /** * Save store select in order admin. * * @param int $post_id The post ID. */ public function save_cashier_select( $post_id ): void { // Security checks. if ( ! isset( $_POST['pos_cashier_select_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['pos_cashier_select_nonce'] ) ), 'pos_cashier_select_action' ) ) { return; } /** * NOTE: HPOS adds a second arg for WC_Abstract_Order, but we will make it backwards compatible. */ $order = wc_get_order( $post_id ); // Check if $order is instance of WC_Abstract_Order and $_POST['_pos_user'] is set. if ( $order instanceof WC_Abstract_Order && isset( $_POST['_pos_user'] ) ) { $new_pos_cashier = (int) sanitize_text_field( wp_unslash( $_POST['_pos_user'] ) ); $current_pos_cashier = (int) $order->get_meta( '_pos_user' ); // Update meta only if _pos_user has changed. if ( $current_pos_cashier !== $new_pos_cashier ) { $order->update_meta_data( '_pos_user', (string) $new_pos_cashier ); $order->save(); // Add an order note indicating the change. $current_cashier = get_userdata( $current_pos_cashier ); $new_cashier = get_userdata( $new_pos_cashier ); $note = sprintf( // translators: 1: old POS cashier, 2: new POS cashier. __( 'POS cashier changed from %1$s to %2$s.', 'woocommerce-pos' ), $current_cashier ? $current_cashier->display_name : /* translators: Fallback cashier name shown when the POS cashier user cannot be found. */ __( 'Unknown', 'woocommerce-pos' ), $new_cashier ? $new_cashier->display_name : /* translators: Fallback cashier name shown when the POS cashier user cannot be found. */ __( 'Unknown', 'woocommerce-pos' ) ); $order->add_order_note( $note ); } } } /** * Add an audit note before WooCommerce applies its posted customer change. * * WooCommerce 10.4.3 registers WC_Meta_Box_Order_Data::save at priority 40; * this priority-10 callback therefore reads the old customer from the order. * Core verifies its order-data nonce before firing this hook. * * @param int $post_id The order ID. */ public function add_customer_change_note( $post_id ): void { // phpcs:disable WordPress.Security.NonceVerification.Missing -- WooCommerce verifies its order-data nonce before firing this hook. if ( ! array_key_exists( 'customer_user', $_POST ) ) { return; } $order = wc_get_order( $post_id ); if ( ! $order instanceof WC_Order || ! woocommerce_pos_is_pos_order( $order ) ) { return; } $old_customer_id = $order->get_customer_id(); $new_customer_id = absint( wp_unslash( $_POST['customer_user'] ?? 0 ) ); // phpcs:enable WordPress.Security.NonceVerification.Missing if ( $old_customer_id !== $new_customer_id ) { Order_Notes::add_admin_customer_change_note( $order, $old_customer_id, $new_customer_id ); } } }