get_meta( self::META ); return Locations::exists( $id ) ? $id : 0; } /** The location stock comes from for this order: its own, or the default. */ public static function location_of( $order ): int { return self::get( $order ) ?: Locations::default_id(); } /** * Set an order's location. If stock was already taken for the order, those units * move from the old location to the new one (WooCommerce's total doesn't change). * Returns how many units moved. */ public static function change( \WC_Order $order, int $to, bool $save = true ): int { if ( ! Locations::exists( $to ) ) return 0; $from = self::location_of( $order ); $order->update_meta_data( self::META, $to ); if ( $save ) $order->save(); if ( $from === $to || ! $from ) return 0; $moved = 0; foreach ( $order->get_items() as $item ) { if ( ! $item instanceof \WC_Order_Item_Product ) continue; $taken = (int) $item->get_meta( '_reduced_stock', true ); $product = $item->get_product(); if ( $taken === 0 || ! $product || ! $product->managing_stock() ) continue; $holder = Stock::stock_holder( $product ); if ( ! Stock::is_managed( $holder ) ) continue; Stock::adjust( $holder, $from, $taken ); Stock::adjust( $holder, $to, -$taken ); $moved += $taken; } if ( $moved ) { $order->add_order_note( sprintf( /* translators: 1: number of units, 2: old location, 3: new location */ _n( 'Location changed: %1$d unit of stock moved from %2$s to %3$s.', 'Location changed: %1$d units of stock moved from %2$s to %3$s.', $moved, 'patchwork-locations' ), $moved, Locations::name( $from ), Locations::name( $to ) ) ); } do_action( 'patchwork_locations_order_location_changed', $order, $from, $to, $moved ); return $moved; } // ─── Order screen ───────────────────────────────────────────────────────── private static function order_screen(): string { $hpos = class_exists( \Automattic\WooCommerce\Utilities\OrderUtil::class ) && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled(); return $hpos ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; } /** Patchwork Core 0.6+ has the shared Patchwork box and bar: the location goes there instead of a box of its own. */ public static function in_shared_box(): bool { return method_exists( \Patchwork\Core\Module::class, 'order_bar' ); } public static function add_meta_box(): void { if ( ! Locations::all() || self::in_shared_box() ) return; add_meta_box( 'patchwork-locations-order', __( 'Location', 'patchwork-locations' ), [ __CLASS__, 'render_meta_box' ], self::order_screen(), 'side', 'high' ); } public static function render_meta_box( $post_or_order ): void { $order = $post_or_order instanceof \WC_Order ? $post_or_order : wc_get_order( $post_or_order->ID ); if ( ! $order ) return; echo self::box_html( $order ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped in box_html() } /** The location and the picker that changes it (saved with the order). */ public static function box_html( \WC_Order $order ): string { if ( ! Locations::all() ) return ''; $own = self::get( $order ); $current = $own ?: Locations::default_id(); $out = wp_nonce_field( 'patchwork_locations_order', 'patchwork_locations_order_nonce', true, false ); if ( $dot = Locations::dot( $current ) ) $out .= '
' . $dot . '' . esc_html( Locations::name( $current ) ) . '
'; $out .= ''; if ( ! $own ) { $out .= '' . esc_html__( 'No location chosen; the default location is used.', 'patchwork-locations' ) . '
'; } elseif ( $order->get_data_store()->get_stock_reduced( $order->get_id() ) ) { $out .= '' . esc_html__( 'Stock has been taken for this order. Changing the location moves it to the new location.', 'patchwork-locations' ) . '
'; } return $out; } /** * Under a product on the order screen: its stock at each location, with this order's * location first and in bold, e.g. "City Store 4 · Northside 2 · Warehouse 0". */ public static function item_html( \WC_Order_Item_Product $item, \WC_Order $order ): string { $product = $item->get_product(); if ( ! $product || ! $product->managing_stock() || count( Locations::all() ) < 2 ) return ''; $holder = Stock::stock_holder( $product ); if ( ! Stock::is_managed( $holder ) ) { return '' . esc_html__( 'Stock per location: not in a location yet', 'patchwork-locations' ) . ''; } $here = self::get( $order ) ?: Locations::default_id(); $stock = Stock::all( $holder ); $parts = []; foreach ( [ $here => $stock[ $here ] ?? 0 ] + $stock as $id => $qty ) { $label = Locations::dot( (int) $id ) . esc_html( Locations::name( (int) $id ) . ' ' . wc_stock_amount( $qty ) ); $parts[ $id ] = (int) $id === (int) $here ? '' . $label . '' : $label; } return '' . implode( ' · ', $parts ) . ''; } public static function save_meta_box( $order_id ): void { if ( ! isset( $_POST['patchwork_locations_order_nonce'], $_POST['patchwork_location'] ) ) return; if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['patchwork_locations_order_nonce'] ) ), 'patchwork_locations_order' ) ) return; $order = wc_get_order( $order_id ); if ( ! $order || ! current_user_can( 'edit_shop_orders' ) ) return; $to = absint( $_POST['patchwork_location'] ); if ( $to && $to !== self::get( $order ) ) self::change( $order, $to ); } // ─── Orders list filter ─────────────────────────────────────────────────── public static function filter_dropdown(): void { $all = Locations::all(); if ( count( $all ) < 2 ) return; $current = absint( $_GET['patchwork_location'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended echo ''; } /** Orders "at" a location: set to it, or (for the default location) with no location set. */ private static function meta_query( int $id ): array { $clause = [ [ 'key' => self::META, 'value' => (string) $id ] ]; if ( $id === Locations::default_id() ) { $clause[] = [ 'key' => self::META, 'compare' => 'NOT EXISTS' ]; $clause['relation'] = 'OR'; } return $clause; } public static function filter_hpos( array $args ): array { $id = absint( $_GET['patchwork_location'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! $id || ! Locations::exists( $id ) ) return $args; $args['meta_query'] = isset( $args['meta_query'] ) ? (array) $args['meta_query'] : []; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- only when filtering the orders list by location $args['meta_query'][] = self::meta_query( $id ); return $args; } public static function filter_posts( \WP_Query $query ): void { if ( ! is_admin() || ! $query->is_main_query() || $query->get( 'post_type' ) !== 'shop_order' ) return; $id = absint( $_GET['patchwork_location'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! $id || ! Locations::exists( $id ) ) return; $mq = (array) $query->get( 'meta_query' ); $mq[] = self::meta_query( $id ); $query->set( 'meta_query', $mq ); } }