Skip to content

Commit

Permalink
Add new abstraction layer for pickup location settings. Make sure to …
Browse files Browse the repository at this point in the history
…only override default shipping provider meta/settings in case meta does exist.
  • Loading branch information
dennisnissle committed Jul 24, 2024
1 parent b9547ec commit 8cee42e
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 27 deletions.
63 changes: 42 additions & 21 deletions assets/js/static/pickup-locations.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
params: {},
pickupLocations: {},
available: false,
currentProvider: '',

init: function () {
var self = germanized.shipments_pickup_locations;
self.params = wc_gzd_shipments_pickup_locations_params;

var $pickupSelect = self.getPickupLocationSelect();

if ( $pickupSelect.length > 0 && $pickupSelect.attr( 'data-locations' ) ) {
try {
self.pickupLocations = JSON.parse( $pickupSelect.attr( 'data-locations' ) );
} catch (e) {
self.pickupLocations = {};
if ( $pickupSelect.length > 0 ) {
if ( $pickupSelect.attr( 'data-locations' ) ) {
try {
self.pickupLocations = JSON.parse( $pickupSelect.attr( 'data-locations' ) );
} catch (e) {
self.pickupLocations = {};
}
}
if ( $pickupSelect.attr( 'data-provider' ) ) {
self.currentProvider = $pickupSelect.attr( 'data-provider' );
}
}

Expand Down Expand Up @@ -75,7 +81,7 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
return false;
},

disablePickupLocationDelivery: function() {
disablePickupLocationDelivery: function( withNotice = false ) {
var self= germanized.shipments_pickup_locations,
$modal = $( '.wc-gzd-modal-content[data-id="pickup-location"].active' );

Expand All @@ -85,6 +91,20 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
if ( $modal.length > 0 ) {
$modal.find( '.wc-gzd-modal-close' ).trigger( 'click' );
}

if ( withNotice ) {
var $form = $( 'form.checkout' );

if ( $form.find( '.woocommerce-NoticeGroup-updateOrderReview' ).length <= 0 ) {
$form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-updateOrderReview"></div>' );
}

$form.find( '.woocommerce-NoticeGroup-updateOrderReview' ).prepend( '<div class="woocommerce-info">' + self.params.i18n_pickup_location_delivery_unavailable + '</div>' );

var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview' );

$.scroll_to_notices( scrollElement );
}
},

onRemovePickupLocation: function() {
Expand Down Expand Up @@ -316,20 +336,33 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
afterRefreshCheckout: function( e, ajaxData ) {
var self = germanized.shipments_pickup_locations,
supportsPickupLocationDelivery = false,
oldLocations = self.pickupLocations;
oldLocations = self.pickupLocations,
oldProvider = self.currentProvider;

ajaxData = ( typeof ajaxData === 'undefined' ) ? {
'fragments': {
'.gzd-shipments-current-provider': '',
'.gzd-shipments-pickup-location-supported': false,
'.gzd-shipments-pickup-locations': JSON.stringify( self.pickupLocations ),
}
} : ajaxData;

if ( ajaxData.hasOwnProperty( 'fragments' ) ) {
if ( ajaxData.fragments.hasOwnProperty( '.gzd-shipments-current-provider' ) ) {
self.currentProvider = ajaxData.fragments['.gzd-shipments-current-provider'];
}

if ( ! self.currentProvider || oldProvider !== self.currentProvider ) {
if ( self.hasPickupLocationDelivery() ) {
self.disablePickupLocationDelivery( true );
}
}

if ( ajaxData.fragments.hasOwnProperty( '.gzd-shipments-pickup-location-supported' ) ) {
supportsPickupLocationDelivery = ajaxData.fragments['.gzd-shipments-pickup-location-supported'];
}
if ( ajaxData.fragments.hasOwnProperty( '.gzd-shipments-pickup-locations' ) && Object.keys( self.pickupLocations ).length <= 0 ) {

if ( ajaxData.fragments.hasOwnProperty( '.gzd-shipments-pickup-locations' ) ) {
self.pickupLocations = JSON.parse( ajaxData.fragments['.gzd-shipments-pickup-locations'] );

self.updatePickupLocationSelect( oldLocations );
Expand All @@ -349,19 +382,7 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku
self.available = false;

if ( self.hasPickupLocationDelivery() ) {
self.disablePickupLocationDelivery();

var $form = $( 'form.checkout' );

if ( $form.find( '.woocommerce-NoticeGroup-updateOrderReview' ).length <= 0 ) {
$form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-updateOrderReview"></div>' );
}

$form.find( '.woocommerce-NoticeGroup-updateOrderReview' ).prepend( '<div class="woocommerce-info">' + self.params.i18n_pickup_location_delivery_unavailable + '</div>' );

var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview' );

$.scroll_to_notices( scrollElement );
self.disablePickupLocationDelivery( true );
}

$( '.pickup_location_notice' ).addClass( 'hidden' ).hide();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "woocommerce-germanized-shipments",
"title": "Shipments for WooCommerce",
"version": "3.4.1",
"version": "3.5.0",
"homepage": "https://vendidero.de",
"repository": {
"type": "git",
Expand Down
6 changes: 6 additions & 0 deletions src/Admin/ProviderSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ protected static function get_section_title( $section = '' ) {
$sections = self::get_sections();
$section_label = isset( $sections[ $section ] ) ? $sections[ $section ] : '';

if ( $provider = self::get_current_provider() ) {
if ( $help_link = $provider->get_section_help_link( $section ) ) {
$section_label = $section_label . '<a class="page-title-action" href="' . esc_url( $help_link ) . '" target="_blank">' . _x( 'Learn more', 'shipments', 'woocommerce-germanized-shipments' ) . '</a>';
}
}

return $section_label;
}

Expand Down
14 changes: 13 additions & 1 deletion src/DataStores/ShippingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,19 @@ protected function read_provider_data( &$provider ) {
}

foreach ( $meta_keys as $meta_key ) {
$props[ substr( $meta_key, 1 ) ] = get_metadata( 'gzd_shipping_provider', $provider->get_id(), $meta_key, true );
if ( function_exists( 'get_metadata_raw' ) ) {
$value = get_metadata_raw( 'gzd_shipping_provider', $provider->get_id(), $meta_key, true );
} else {
$value = null;

if ( metadata_exists( 'gzd_shipping_provider', $provider->get_id(), $meta_key ) ) {
$value = get_metadata( 'gzd_shipping_provider', $provider->get_id(), $meta_key, true );
}
}

if ( ! is_null( $value ) ) {
$props[ substr( $meta_key, 1 ) ] = $value;
}
}

$provider->set_props( $props );
Expand Down
28 changes: 28 additions & 0 deletions src/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ public static function install() {
} elseif ( version_compare( $current_version, '3.0.1', '<' ) ) {
self::migrate_to_configuration_sets( 'dhl' );
}

if ( version_compare( $current_version, '3.5.0', '<' ) ) {
if ( $provider = wc_gzd_get_shipping_provider( 'dhl' ) ) {
if ( is_a( $provider, '\Vendidero\Germanized\Shipments\ShippingProvider\Auto' ) && $provider->is_activated() ) {
if ( 'yes' === $provider->get_setting( 'parcel_pickup_packstation_enable' ) || 'yes' === $provider->get_setting( 'parcel_pickup_postoffice_enable' ) || 'yes' === $provider->get_setting( 'parcel_pickup_parcelshop_enable' ) ) {
$provider->update_setting( 'pickup_locations_enable', 'yes' );
$provider->update_setting( 'pickup_locations_max_results', $provider->get_setting( 'parcel_pickup_max_results', 20 ) );
} else {
$provider->update_setting( 'pickup_locations_enable', 'no' );
}

$provider->save();
}
}

if ( $provider = wc_gzd_get_shipping_provider( 'deutsche_post' ) ) {
if ( is_a( $provider, '\Vendidero\Germanized\Shipments\ShippingProvider\Auto' ) && $provider->is_activated() ) {
if ( 'yes' === $provider->get_setting( 'parcel_pickup_packstation_enable' ) ) {
$provider->update_setting( 'pickup_locations_enable', 'yes' );
$provider->update_setting( 'pickup_locations_max_results', $provider->get_setting( 'parcel_pickup_max_results', 20 ) );
} else {
$provider->update_setting( 'pickup_locations_enable', 'no' );
}

$provider->save();
}
}
}
}

self::maybe_create_packaging();
Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/ShippingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function get_id();

public function get_help_link();

public function get_section_help_link( $section );

public function get_signup_link();

public function is_pro();
Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/ShippingProviderAuto.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function is_sandbox();

public function get_settings_help_pointers( $section = '' );

public function supports_pickup_locations();

public function supports_pickup_location_delivery( $address, $query_args = array() );

public function is_valid_pickup_location( $location_code, $address );
Expand Down
2 changes: 1 addition & 1 deletion src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Package {
*
* @var string
*/
const VERSION = '3.4.1';
const VERSION = '3.5.0';

public static $upload_dir_suffix = '';

Expand Down
8 changes: 7 additions & 1 deletion src/PickupDelivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ protected static function get_pickup_location_data( $context = 'checkout', $retr
'postcode' => '',
'address_1' => '',
),
'provider' => '',
'supports_pickup_delivery' => false,
'current_location_code' => '',
'current_location' => null,
Expand Down Expand Up @@ -195,7 +196,8 @@ protected static function get_pickup_location_data( $context = 'checkout', $retr
);
}

$result['address'] = array_replace_recursive( $result['address'], $address_args );
$result['address'] = array_replace_recursive( $result['address'], $address_args );
$result['provider'] = $provider ? $provider->get_name() : '';

if ( is_a( $provider, 'Vendidero\Germanized\Shipments\Interfaces\ShippingProviderAuto' ) ) {
if ( $provider->supports_pickup_location_delivery( $result['address'], $query_args ) ) {
Expand Down Expand Up @@ -284,6 +286,7 @@ public static function pickup_location_search_modal( $context = 'checkout' ) {
'pickup_location',
array(
'type' => 'wc_gzd_shipments_pickup_location',
'provider' => $pickup_delivery_data['provider'],
'locations' => $pickup_delivery_data['locations'],
'current_location' => $pickup_delivery_data['current_location'],
'default' => $pickup_delivery_data['current_location'] ? $pickup_delivery_data['current_location']->get_code() : '',
Expand Down Expand Up @@ -446,6 +449,7 @@ public static function register_pickup_location_field( $field, $key, $args, $val
'locations' => array(),
'id' => $key,
'priority' => '',
'provider' => '',
'required' => false,
'custom_attributes' => array(),
'current_location' => null,
Expand Down Expand Up @@ -479,6 +483,7 @@ public static function register_pickup_location_field( $field, $key, $args, $val
}

$args['custom_attributes']['data-locations'] = wp_json_encode( $args['custom_attributes']['data-locations'] );
$args['custom_attributes']['data-provider'] = $args['provider'];

if ( count( $args['options'] ) > 0 ) {
$args['hidden'] = false;
Expand Down Expand Up @@ -630,6 +635,7 @@ public static function register_order_review_fragments( $fragments ) {
}
}

$fragments['.gzd-shipments-current-provider'] = $pickup_delivery_data['provider'];
$fragments['.gzd-shipments-pickup-locations'] = wp_json_encode( $locations );
$fragments['.gzd-shipments-pickup-location-supported'] = $pickup_delivery_data['supports_pickup_delivery'];

Expand Down
Loading

0 comments on commit 8cee42e

Please sign in to comment.