From 4ed903d3678b6856d52092862b50154530fc8e43 Mon Sep 17 00:00:00 2001 From: vendidero Date: Thu, 18 Apr 2024 12:49:25 +0200 Subject: [PATCH] Move pickup delivery admin fields to pickup delivery class. Added Woo < 8.6.0 backwards compatibility for admin fields. --- src/Admin/Admin.php | 30 ------------------------------ src/Order.php | 6 +++--- src/PickupDelivery.php | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/Admin/Admin.php b/src/Admin/Admin.php index c7da733e..f07bca90 100644 --- a/src/Admin/Admin.php +++ b/src/Admin/Admin.php @@ -101,8 +101,6 @@ function() { add_action( 'woocommerce_admin_field_dimensions', array( __CLASS__, 'register_dimensions_field' ), 30 ); add_filter( 'woocommerce_admin_settings_sanitize_option', array( __CLASS__, 'sanitize_dimensions_field' ), 10, 3 ); - add_filter( 'woocommerce_admin_shipping_fields', array( __CLASS__, 'register_pickup_location_admin_fields' ), 10, 3 ); - add_filter( 'woocommerce_debug_tools', array( __CLASS__, 'register_remove_duplicate_provider_meta_tool' ), 10, 1 ); } @@ -138,34 +136,6 @@ public static function remove_duplicate_provider_meta() { } } - public static function register_pickup_location_admin_fields( $fields, $order = null, $context = 'edit' ) { - if ( ! $order instanceof \WC_Order ) { - return $fields; - } - - $shipment_order = wc_gzd_get_shipment_order( $order ); - - if ( $shipment_order->supports_pickup_location() ) { - $fields['pickup_location_code'] = array( - 'label' => _x( 'Pickup location', 'shipments', 'woocommerce-germanized-shipments' ), - 'type' => 'text', - 'id' => '_pickup_location_code', - 'show' => false, - 'value' => $shipment_order->get_pickup_location_code(), - ); - - $fields['pickup_location_customer_number'] = array( - 'label' => _x( 'Pickup customer number', 'shipments', 'woocommerce-germanized-shipments' ), - 'show' => false, - 'id' => '_pickup_location_customer_number', - 'type' => 'text', - 'value' => $shipment_order->get_pickup_location_customer_number(), - ); - } - - return $fields; - } - public static function sanitize_toggle_field( $value, $option, $raw_value ) { if ( Package::is_integration() ) { return $value; diff --git a/src/Order.php b/src/Order.php index ed6af99d..2414df8e 100644 --- a/src/Order.php +++ b/src/Order.php @@ -188,7 +188,7 @@ public function get_return_status() { } public function get_default_return_shipping_provider() { - $default_provider_instance = wc_gzd_get_order_shipping_provider( $this->get_order() ); + $default_provider_instance = $this->get_shipping_provider(); $default_provider = $default_provider_instance ? $default_provider_instance->get_name() : ''; $shipments = $this->get_simple_shipments(); @@ -282,7 +282,7 @@ public function create_shipments( $default_status = 'processing' ) { } else { $available_packaging = wc_gzd_get_packaging_list(); - if ( $provider = wc_gzd_get_order_shipping_provider( $this ) ) { + if ( $provider = $this->get_shipping_provider() ) { $available_packaging = wc_gzd_get_packaging_list( array( 'shipping_provider' => $provider->get_name() ) ); } @@ -1127,7 +1127,7 @@ public function get_returnable_item_count() { public function supports_pickup_location() { $supports_pickup_location = false; - if ( $provider = wc_gzd_get_order_shipping_provider( $this ) ) { + if ( $provider = $this->get_shipping_provider() ) { if ( is_a( $provider, 'Vendidero\Germanized\Shipments\Interfaces\ShippingProviderAuto' ) ) { $supports_pickup_location = $provider->supports_pickup_location_delivery( $this->get_order()->get_address( 'shipping' ) ); } diff --git a/src/PickupDelivery.php b/src/PickupDelivery.php index e728a26c..f46bdac3 100644 --- a/src/PickupDelivery.php +++ b/src/PickupDelivery.php @@ -27,6 +27,44 @@ public static function init() { add_filter( 'woocommerce_form_field_wc_gzd_shipments_pickup_location', array( __CLASS__, 'register_pickup_location_field' ), 10, 4 ); add_filter( 'woocommerce_form_field_wc_gzd_shipments_pickup_location_customer_number', array( __CLASS__, 'register_pickup_location_customer_number_field' ), 10, 4 ); + + add_filter( 'woocommerce_admin_shipping_fields', array( __CLASS__, 'register_pickup_location_admin_fields' ), 10, 3 ); + } + + public static function register_pickup_location_admin_fields( $fields, $order = null, $context = 'edit' ) { + if ( is_null( $order ) && version_compare( wc()->version, '8.6.0', '<' ) ) { + global $theorder; + + if ( isset( $theorder ) ) { + $order = $theorder; + } + } + + if ( ! $order instanceof \WC_Order ) { + return $fields; + } + + if ( $shipment_order = wc_gzd_get_shipment_order( $order ) ) { + if ( $shipment_order->supports_pickup_location() ) { + $fields['pickup_location_code'] = array( + 'label' => _x( 'Pickup location', 'shipments', 'woocommerce-germanized-shipments' ), + 'type' => 'text', + 'id' => '_pickup_location_code', + 'show' => false, + 'value' => $shipment_order->get_pickup_location_code(), + ); + + $fields['pickup_location_customer_number'] = array( + 'label' => _x( 'Pickup customer number', 'shipments', 'woocommerce-germanized-shipments' ), + 'show' => false, + 'id' => '_pickup_location_customer_number', + 'type' => 'text', + 'value' => $shipment_order->get_pickup_location_customer_number(), + ); + } + } + + return $fields; } public static function register_pickup_location_customer_number_field( $field, $key, $args, $value ) {