Skip to content

Commit

Permalink
Pass shipping method to pickup delivery args. Fix show/hide field log…
Browse files Browse the repository at this point in the history
…ic for new shipping method modal HTML layout. Unselect the use shipping as billing field in case a pickup location has been chosen (checkout block).
  • Loading branch information
dennisnissle committed Oct 28, 2024
1 parent b9986dc commit 56c7a9e
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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' );
Expand Down
19 changes: 15 additions & 4 deletions assets/js/static/admin-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand Down Expand Up @@ -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();
}
}
} );
},
Expand Down
47 changes: 9 additions & 38 deletions assets/js/static/admin-shipping-provider-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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( ']', '' );
},
Expand Down Expand Up @@ -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' );
}

Expand Down Expand Up @@ -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();
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
70 changes: 68 additions & 2 deletions src/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
}

Expand Down Expand Up @@ -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
*/
Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
/**
Expand Down
2 changes: 2 additions & 0 deletions src/PickupDelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ public static function get_pickup_delivery_cart_args() {
'height' => 0.0,
),
'payment_gateway' => '',
'shipping_method' => false,
);
}

Expand Down Expand Up @@ -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,
);
}

Expand Down
17 changes: 13 additions & 4 deletions src/ShippingProvider/Auto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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,
)
);

Expand All @@ -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 );
Expand Down

0 comments on commit 56c7a9e

Please sign in to comment.