log( 'location_changed', sprintf( /* translators: 1: order number, 2: old location, 3: new location, 4: units moved */ __( 'Order #%1$s moved from %2$s to %3$s (%4$d units of stock moved).', 'patchwork-locations' ), $order->get_order_number(), Locations::name( $from ) ?: '—', Locations::name( $to ), $moved ), 'order', $order->get_id() ); }, 10, 4 ); add_action( 'patchwork_locations_location_removed', function( $removed, $to, $products, $units ) { $this->log( 'location_removed', sprintf( /* translators: 1: units, 2: products, 3: location name */ __( 'A location was removed: its stock (%1$d units across %2$d products) moved to %3$s.', 'patchwork-locations' ), $units, $products, Locations::name( $to ) ) ); }, 10, 4 ); } // ─── Identity ───────────────────────────────────────────────────────────── public function id(): string { return 'locations'; } public function name(): string { return 'Patchwork Locations'; } public function version(): string { return PATCHWORK_LOCATIONS_VERSION; } public function plugin_file(): string { return PATCHWORK_LOCATIONS_FILE; } public function icon(): string { return 'dashicons-location'; } public function supports(): array { return Orders::in_shared_box() ? [ 'orders_column', 'order_panel' ] : [ 'orders_column' ]; } /** The order's location and its picker, in the shared Patchwork box (core 0.6.0+). */ public function order_panel( \WC_Order $order ): string { return Orders::in_shared_box() ? Orders::box_html( $order ) : ''; } /** Stock at each location under each product line (core 0.6.0+). */ public function order_item_info( \WC_Order_Item_Product $item, \WC_Order $order ): string { return Orders::item_html( $item, $order ); } public function description(): string { return __( 'Stock per location, a location choice at checkout, and stock that comes off the right location.', 'patchwork-locations' ); } public function settings_capability(): string { return 'manage_woocommerce'; } public function freemius() { return function_exists( 'patchwork_locations_fs' ) ? patchwork_locations_fs() : null; } public function links(): array { return []; // locations are listed at the top of the settings tab } // ─── Settings ───────────────────────────────────────────────────────────── public function settings_sections(): array { return [ 'locations' => __( 'Your locations', 'patchwork-locations' ), 'stock' => __( 'Stock', 'patchwork-locations' ), 'checkout' => __( 'Checkout', 'patchwork-locations' ), ]; } public function settings_fields(): array { $locations = Locations::all(); return [ [ 'id' => 'patchwork_locations_list', 'section' => 'locations', 'type' => 'custom', 'label' => __( 'Locations', 'patchwork-locations' ), 'render' => [ $this, 'render_list' ], 'save' => [ $this, 'save_list' ], ], [ 'id' => Locations::OPT_DEFAULT, 'section' => 'stock', 'type' => 'select', 'default' => (string) Locations::default_id(), 'label' => __( 'Default location', 'patchwork-locations' ), 'options' => $locations ? array_map( 'strval', $locations ) : [ '' => __( 'Add a location first', 'patchwork-locations' ) ], 'description' => __( 'Used for orders with no location chosen, and for stock changes that don\'t come from an order (imports, other plugins, the REST API).', 'patchwork-locations' ), ], [ 'id' => 'patchwork_locations_assign', 'section' => 'stock', 'type' => 'custom', 'label' => __( 'Products not in a location yet', 'patchwork-locations' ), 'render' => [ $this, 'render_assign' ], 'save' => [ $this, 'save_assign' ], ], [ 'id' => Checkout::OPT_MODE, 'section' => 'checkout', 'type' => 'select', 'default' => 'required', 'label' => __( 'Ask customers to choose a location', 'patchwork-locations' ), 'options' => [ 'required' => __( 'Yes, required', 'patchwork-locations' ), 'optional' => __( 'Yes, optional', 'patchwork-locations' ), 'off' => __( 'No', 'patchwork-locations' ), ], 'description' => __( 'Only asked when more than one location is open to customers. With one, it\'s chosen automatically.', 'patchwork-locations' ), ], [ 'id' => Checkout::OPT_LABEL, 'section' => 'checkout', 'type' => 'text', 'default' => '', 'label' => __( 'Field label', 'patchwork-locations' ), 'description' => __( 'What customers see, e.g. "Pickup store" or "Ship from". Leave empty for "Location".', 'patchwork-locations' ), ], ]; } public function render_list( array $field ): void { $add = admin_url( 'post-new.php?post_type=' . Locations::POST_TYPE ); $all = Locations::all(); if ( ! $all ) { echo '
' . esc_html__( 'No locations yet. Add each shop, warehouse or store room you keep stock in.', 'patchwork-locations' ) . '
'; } else { $default = Locations::default_id(); echo '| ' . esc_html__( 'Location', 'patchwork-locations' ) . ' | ' . esc_html__( 'Address', 'patchwork-locations' ) . ' | ' . esc_html__( 'At checkout', 'patchwork-locations' ) . ' | ' . esc_html__( 'Colour', 'patchwork-locations' ) . ' |
|---|---|---|---|
| %s%s | %s | %s | %s |
' . esc_html__( 'Colours mark each location\'s orders and stock in the admin. Customers don\'t see them. Save settings to keep a change.', 'patchwork-locations' ) . '
'; } printf( '', esc_url( $add ), esc_html__( 'Add location', 'patchwork-locations' ), esc_url( admin_url( 'edit.php?post_type=' . Locations::POST_TYPE ) ), esc_html__( 'Manage, reorder or remove locations', 'patchwork-locations' ) ); } /** Colours changed in the list. The settings page has checked its nonce and the capability. */ public function save_list( array $field ): void { $posted = $_POST['patchwork_location_color'] ?? null; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput -- sanitised per value below if ( ! is_array( $posted ) ) return; foreach ( array_keys( Locations::all() ) as $id ) { if ( ! isset( $posted[ $id ] ) || ! current_user_can( 'edit_post', $id ) ) continue; Locations::set_color( $id, sanitize_text_field( wp_unslash( (string) $posted[ $id ] ) ) ); } } public function render_assign( array $field ): void { echo ''; $ids = Stock::unmanaged_ids(); $count = count( $ids ); if ( ! Locations::all() ) { echo '' . esc_html__( 'Add a location first.', 'patchwork-locations' ) . '
'; return; } if ( ! $count ) { echo '✓ ' . esc_html__( 'Every product that tracks stock is in a location. Nothing to do here.', 'patchwork-locations' ) . '
'; return; } $default = Locations::name( Locations::default_id() ); echo '' . esc_html( sprintf( /* translators: %d: number of products and variations */ _n( '%d product hasn\'t been put in a location yet.', '%d products haven\'t been put in a location yet.', $count, 'patchwork-locations' ), $count ) ) . '
'; echo '' . esc_html__( 'WooCommerce still has the right stock for them, but Patchwork Locations doesn\'t know which shop it\'s in. So each location shows 0 for them, and an order can\'t take them from a shop.', 'patchwork-locations' ) . '
'; $show = array_slice( $ids, 0, 8 ); echo '' . esc_html__( 'What to do:', 'patchwork-locations' ) . ' '; echo esc_html( sprintf( /* translators: %s: default location name */ __( 'tick the box below and click Save settings to put them all in %s, your default location. Or set each one on its product page (Inventory tab), e.g. 10 at one shop and 8 at another.', 'patchwork-locations' ), $default ) ) . '
'; echo ''; echo '' . esc_html__( 'Only these products change, and WooCommerce\'s stock numbers stay exactly the same. You can move stock between shops on a product page at any time.', 'patchwork-locations' ) . '
'; echo '