From 56c7a9ea5da61d319d4e68046707d275244b1ad6 Mon Sep 17 00:00:00 2001 From: vendidero Date: Mon, 28 Oct 2024 15:43:37 +0100 Subject: [PATCH] Pass shipping method to pickup delivery args. Fix show/hide field logic for new shipping method modal HTML layout. Unselect the use shipping as billing field in case a pickup location has been chosen (checkout block). --- .../slotfills/pickup-location-select.js | 6 ++ assets/js/static/admin-settings.js | 19 +++-- .../static/admin-shipping-provider-method.js | 47 +++---------- src/Admin/Admin.php | 2 +- src/Order.php | 70 ++++++++++++++++++- src/Package.php | 12 ++++ src/PickupDelivery.php | 2 + src/ShippingProvider/Auto.php | 17 +++-- 8 files changed, 126 insertions(+), 49 deletions(-) diff --git a/assets/js/blocks/checkout-pickup-location-select/slotfills/pickup-location-select.js b/assets/js/blocks/checkout-pickup-location-select/slotfills/pickup-location-select.js index c801b69..9374bc3 100644 --- a/assets/js/blocks/checkout-pickup-location-select/slotfills/pickup-location-select.js +++ b/assets/js/blocks/checkout-pickup-location-select/slotfills/pickup-location-select.js @@ -299,6 +299,10 @@ const render = () => { }; } ); + const { + __internalSetUseShippingAsBilling + } = useDispatch( CHECKOUT_STORE_KEY ); + const shippingAddress = customerData.shippingAddress; const { setShippingAddress, updateCustomerData } = useDispatch( CART_STORE_KEY ); const checkoutOptions = getCheckoutData(); @@ -547,6 +551,8 @@ const render = () => { setOption( 'pickup_location', pickupLocation ); setPickupLocationSearchAddress( { 'address_1': '' } ); + __internalSetUseShippingAsBilling( false ); + const { removeNotice } = dispatch( 'core/notices' ); removeNotice( 'wc-gzd-shipments-review-shipping-address', 'wc/checkout/shipping-address' ); diff --git a/assets/js/static/admin-settings.js b/assets/js/static/admin-settings.js index 729c23f..37b163e 100644 --- a/assets/js/static/admin-settings.js +++ b/assets/js/static/admin-settings.js @@ -26,8 +26,8 @@ window.shipments.admin = window.shipments.admin || {}; objectName = '', methodName = ''; - if ( callback.substring( 0, 17 ) === 'shipments.admin.' ) { - callback = callback.slice( 17 ); + if ( callback.substring( 0, 16 ) === 'shipments.admin.' ) { + callback = callback.slice( 16 ); params = callback.split( "." ); objectName = shipments.admin[params[0]]; @@ -135,9 +135,20 @@ window.shipments.admin = window.shipments.admin || {}; } if ( meetsConditions ) { - $field.show(); + if ( $field.length === 0 ) { + // Use this markup as fallback in case field does not belong to a table, e.g. shipping method settings + $input.parents( 'fieldset' ).show(); + $input.parents( 'fieldset' ).prev( 'label' ).show(); + } else { + $field.show(); + } } else { - $field.hide(); + if ( $field.length === 0 ) { + $input.parents( 'fieldset' ).hide(); + $input.parents( 'fieldset' ).prev( 'label' ).hide(); + } else { + $field.hide(); + } } } ); }, diff --git a/assets/js/static/admin-shipping-provider-method.js b/assets/js/static/admin-shipping-provider-method.js index 91bfadd..05876e2 100644 --- a/assets/js/static/admin-shipping-provider-method.js +++ b/assets/js/static/admin-shipping-provider-method.js @@ -19,7 +19,8 @@ window.shipments.admin = window.shipments.admin || {}; $( document ) .on( 'change', 'select[id$=shipping_provider]', self.showOrHideAll ) .on( 'click', '.wc-gzd-shipping-provider-method-tabs .nav-tab-wrapper a.nav-tab', self.onChangeTab ) - .on( 'change', '.override-checkbox :input', self.onChangeOverride ); + .on( 'change', '.override-checkbox :input', self.onChangeOverride ) + .on( 'change', '.wc-gzd-shipping-provider-method-tab-content :input[id]', self.onChangeInput ); $( document.body ).on( 'wc_backbone_modal_loaded', self.onShippingMethodOpen ); @@ -28,6 +29,12 @@ window.shipments.admin = window.shipments.admin || {}; } }, + onChangeInput: function() { + var settings = shipments.admin.shipment_settings; + + settings.onChangeInput.call( $( this ) ); + }, + parseFieldId: function( fieldId ) { return fieldId.replace( '[', '_' ).replace( ']', '' ); }, @@ -58,6 +65,7 @@ window.shipments.admin = window.shipments.admin || {}; if ( $tab.length > 0 ) { $navTab.addClass( 'nav-tab-active' ); $tab.addClass( 'tab-content-active' ); + $tab.find( ':input:visible' ).trigger( 'change' ); } @@ -100,54 +108,17 @@ window.shipments.admin = window.shipments.admin || {}; showOrHideAll: function() { var self = shipments.admin.shipping_provider_method, $select = $( this ), - $providers = $select.find( 'option' ), $form = $select.parents( 'form' ); self.currentProvider = $select.val(); - $providers.each( function() { - var $provider = $( this ), - provider_setting_prefix = $provider.val(); - - if ( provider_setting_prefix.length > 0 ) { - $form.find( 'table.form-table' ).each( function() { - if ( $( this ).find( ':input[id*=_' + provider_setting_prefix + '_]' ).length > 0 ) { - self.hideTable( $( this ) ); - } - }); - } - }); - $form.find( '.wc-gzd-shipping-provider-method-tabs' ).hide(); $form.find( '.wc-gzd-shipping-provider-method-tab-content' ).removeClass( 'tab-content-active' ); if ( self.currentProvider.length > 0 ) { - $form.find( 'table.form-table' ).each( function() { - if ( $( this ).find( ':input[id*=_' + self.currentProvider + '_]' ).length > 0 ) { - self.showTable( $( this ) ); - } - }); - $form.find( '.wc-gzd-shipping-provider-method-tabs[data-provider="' + self.currentProvider + '"]' ).show(); $form.find( '.wc-gzd-shipping-provider-method-tabs[data-provider="' + self.currentProvider + '"] .nav-tab-wrapper' ).find( 'a.nav-tab:first' ).trigger( 'click' ); - - // Trigger show/hide - $form.find( ':input[id*=_' + self.currentProvider + '_]:visible' ).trigger( 'change' ); - } - }, - - hideTable: function( $table ) { - if ( $table.find( 'select[id$=shipping_provider]' ).length > 0 ) { - return false; } - - $table.prevUntil( 'table.form-table' ).hide(); - $table.hide(); - }, - - showTable: function( $table ) { - $table.prevUntil( 'table.form-table' ).show(); - $table.show(); } }; diff --git a/src/Admin/Admin.php b/src/Admin/Admin.php index 93d0562..f096048 100644 --- a/src/Admin/Admin.php +++ b/src/Admin/Admin.php @@ -1833,7 +1833,7 @@ private static function get_admin_settings_params() { $params = array(); if ( self::is_shipping_settings_request() ) { - $params['clean_input_callback'] = 'germanized.admin.shipping_provider_method.getCleanInputId'; + $params['clean_input_callback'] = 'shipments.admin.shipping_provider_method.getCleanInputId'; } return $params; diff --git a/src/Order.php b/src/Order.php index 24c51e7..eb80b59 100644 --- a/src/Order.php +++ b/src/Order.php @@ -1190,12 +1190,68 @@ public function get_returnable_item_count() { return apply_filters( 'woocommerce_gzd_shipment_order_returnable_item_count', $count, $this ); } + public function get_pickup_delivery_args() { + $args = array( + 'max_weight' => 0.0, + 'max_dimensions' => array( + 'length' => 0.0, + 'width' => 0.0, + 'height' => 0.0, + ), + 'payment_gateway' => $this->get_order()->get_payment_method(), + 'shipping_method' => $this->get_shipping_method(), + ); + + foreach ( $this->get_shippable_items() as $item ) { + if ( ! is_callable( array( $item, 'get_product' ) ) ) { + continue; + } + + if ( $product = $item->get_product() ) { + $s_product = apply_filters( 'woocommerce_gzd_shipments_order_item_product', wc_gzd_shipments_get_product( $product ), $item ); + + if ( $s_product ) { + $width = empty( $s_product->get_shipping_width() ) ? 0 : (float) wc_format_decimal( $s_product->get_shipping_width() ); + $length = empty( $s_product->get_shipping_length() ) ? 0 : (float) wc_format_decimal( $s_product->get_shipping_length() ); + $height = empty( $s_product->get_shipping_height() ) ? 0 : (float) wc_format_decimal( $s_product->get_shipping_height() ); + + $dimensions = array( + 'width' => (float) wc_get_dimension( $width, wc_gzd_get_packaging_dimension_unit() ), + 'length' => (float) wc_get_dimension( $length, wc_gzd_get_packaging_dimension_unit() ), + 'height' => (float) wc_get_dimension( $height, wc_gzd_get_packaging_dimension_unit() ), + ); + + if ( $dimensions['width'] > $args['max_dimensions']['width'] ) { + $args['max_dimensions']['width'] = $dimensions['width']; + } + + if ( $dimensions['length'] > $args['max_dimensions']['length'] ) { + $args['max_dimensions']['length'] = $dimensions['length']; + } + + if ( $dimensions['height'] > $args['max_dimensions']['height'] ) { + $args['max_dimensions']['height'] = $dimensions['height']; + } + + $weight = empty( $product->get_weight() ) ? 0 : (float) wc_format_decimal( $product->get_weight() ); + $weight = (float) wc_get_weight( $weight, wc_gzd_get_packaging_weight_unit() ); + + if ( $weight > $args['max_weight'] ) { + $args['max_weight'] = $weight; + } + } + } + } + + return $args; + } + public function supports_pickup_location() { $supports_pickup_location = false; 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' ) ); + $supports_pickup_location = $provider->supports_pickup_location_delivery( $this->get_order()->get_address( 'shipping' ), $this->get_pickup_delivery_args() ); } } @@ -1260,6 +1316,16 @@ protected function has_local_pickup() { return $has_pickup; } + /** + * @return ProviderMethod|false + */ + public function get_shipping_method() { + $shipping_method_id = wc_gzd_get_shipment_order_shipping_method_id( $this->get_order() ); + $method = MethodHelper::get_provider_method( $shipping_method_id ); + + return $method; + } + /** * @return ProviderMethod|false */ @@ -1270,7 +1336,7 @@ public function get_builtin_shipping_method() { $shipping_method_id = wc_gzd_get_shipment_order_shipping_method_id( $this->get_order() ); if ( 'shipping_provider_' === substr( $shipping_method_id, 0, 18 ) ) { - $the_method = MethodHelper::get_provider_method( $shipping_method_id ); + $the_method = $this->get_shipping_method(); if ( $the_method && ! $the_method->is_placeholder() ) { return $the_method; diff --git a/src/Package.php b/src/Package.php index bee463a..c307a49 100644 --- a/src/Package.php +++ b/src/Package.php @@ -360,6 +360,18 @@ public static function base_country_supports_export_reference_number() { return apply_filters( 'woocommerce_gzd_base_country_supports_export_reference_number', self::country_belongs_to_eu_customs_area( $base_country ) ); } + public static function get_shipping_zone( $country, $args = array() ) { + $zone = 'int'; + + if ( self::is_shipping_domestic( $country, $args ) ) { + $zone = 'dom'; + } elseif ( self::is_shipping_inner_eu_country( $country, $args ) ) { + $zone = 'eu'; + } + + return $zone; + } + public static function is_shipping_international( $country, $args = array() ) { $args = self::parse_location_data( $args ); /** diff --git a/src/PickupDelivery.php b/src/PickupDelivery.php index 0930983..1184101 100644 --- a/src/PickupDelivery.php +++ b/src/PickupDelivery.php @@ -725,6 +725,7 @@ public static function get_pickup_delivery_cart_args() { 'height' => 0.0, ), 'payment_gateway' => '', + 'shipping_method' => false, ); } @@ -804,6 +805,7 @@ public static function get_pickup_delivery_cart_args() { 'max_weight' => $max_weight, 'max_dimensions' => $max_dimensions, 'payment_gateway' => WC()->session ? WC()->session->get( 'chosen_payment_method' ) : '', + 'shipping_method' => $shipping_method, ); } diff --git a/src/ShippingProvider/Auto.php b/src/ShippingProvider/Auto.php index 8f09cc4..ac782b1 100644 --- a/src/ShippingProvider/Auto.php +++ b/src/ShippingProvider/Auto.php @@ -622,15 +622,23 @@ protected function get_pickup_location_cache_key( $location_code, $address ) { return $cache_key; } - protected function get_pickup_locations_cache_key( $address, $limit = 10 ) { + protected function get_pickup_locations_cache_key( $address, $query_args = array() ) { $address = $this->parse_pickup_location_address_args( $address ); $cache_key_values = array( - $limit, + $query_args['limit'], $address['country'], $address['postcode'], $address['address_1'], ); + if ( ! empty( $query_args['shipping_method'] ) ) { + $cache_key_values[] = 's:' . $query_args['shipping_method']->get_id() . '_' . $query_args['shipping_method']->get_instance_id(); + } + + if ( ! empty( $query_args['payment_method'] ) ) { + $cache_key_values[] = 'p:' . $query_args['payment_method']; + } + $cache_key = "woocommerce_gzd_shipments_{$this->get_name()}_pickup_locations"; foreach ( $cache_key_values as $cache_key_value ) { @@ -678,6 +686,7 @@ protected function parse_pickup_location_query_args( $query_args ) { 'max_weight' => 0.0, 'limit' => $this->get_pickup_locations_max_results(), 'payment_gateway' => '', + 'shipping_method' => false, ) ); @@ -695,9 +704,9 @@ protected function parse_pickup_location_query_args( $query_args ) { public function get_pickup_locations( $address, $query_args = array() ) { $query_args = $this->parse_pickup_location_query_args( $query_args ); - $cache_key = $this->get_pickup_locations_cache_key( $address, $query_args['limit'] ); - $pickup_locations = get_transient( $cache_key ); $address = $this->parse_pickup_location_address_args( $address ); + $cache_key = $this->get_pickup_locations_cache_key( $address, $query_args ); + $pickup_locations = get_transient( $cache_key ); if ( false === $pickup_locations && ( ! empty( $address['postcode'] ) || ! empty( $address['city'] ) ) ) { $pickup_locations = $this->fetch_pickup_locations( $address, $query_args );