diff --git a/assets/js/static/pickup-locations.js b/assets/js/static/pickup-locations.js index a9e72cb..7f5fbcd 100644 --- a/assets/js/static/pickup-locations.js +++ b/assets/js/static/pickup-locations.js @@ -12,6 +12,7 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku params: {}, pickupLocations: {}, available: false, + currentProvider: '', init: function () { var self = germanized.shipments_pickup_locations; @@ -19,11 +20,16 @@ window.germanized.shipments_pickup_locations = window.germanized.shipments_picku 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' ); } } @@ -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' ); @@ -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( '
' ); + } + + $form.find( '.woocommerce-NoticeGroup-updateOrderReview' ).prepend( '
' + self.params.i18n_pickup_location_delivery_unavailable + '
' ); + + var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview' ); + + $.scroll_to_notices( scrollElement ); + } }, onRemovePickupLocation: function() { @@ -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 ); @@ -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( '
' ); - } - - $form.find( '.woocommerce-NoticeGroup-updateOrderReview' ).prepend( '
' + self.params.i18n_pickup_location_delivery_unavailable + '
' ); - - var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview' ); - - $.scroll_to_notices( scrollElement ); + self.disablePickupLocationDelivery( true ); } $( '.pickup_location_notice' ).addClass( 'hidden' ).hide(); diff --git a/package.json b/package.json index 0c757be..3f00056 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/Admin/ProviderSettings.php b/src/Admin/ProviderSettings.php index 93afb93..452fe7f 100644 --- a/src/Admin/ProviderSettings.php +++ b/src/Admin/ProviderSettings.php @@ -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 . '' . _x( 'Learn more', 'shipments', 'woocommerce-germanized-shipments' ) . ''; + } + } + return $section_label; } diff --git a/src/DataStores/ShippingProvider.php b/src/DataStores/ShippingProvider.php index 8f2ca29..d2b406c 100644 --- a/src/DataStores/ShippingProvider.php +++ b/src/DataStores/ShippingProvider.php @@ -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 ); diff --git a/src/Install.php b/src/Install.php index fa23324..05f401c 100644 --- a/src/Install.php +++ b/src/Install.php @@ -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(); diff --git a/src/Interfaces/ShippingProvider.php b/src/Interfaces/ShippingProvider.php index 80bcb52..2b721e8 100644 --- a/src/Interfaces/ShippingProvider.php +++ b/src/Interfaces/ShippingProvider.php @@ -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(); diff --git a/src/Interfaces/ShippingProviderAuto.php b/src/Interfaces/ShippingProviderAuto.php index b50f481..31da1a1 100644 --- a/src/Interfaces/ShippingProviderAuto.php +++ b/src/Interfaces/ShippingProviderAuto.php @@ -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 ); diff --git a/src/Package.php b/src/Package.php index ec0c122..f0f0adc 100644 --- a/src/Package.php +++ b/src/Package.php @@ -18,7 +18,7 @@ class Package { * * @var string */ - const VERSION = '3.4.1'; + const VERSION = '3.5.0'; public static $upload_dir_suffix = ''; diff --git a/src/PickupDelivery.php b/src/PickupDelivery.php index 1af77b4..a7bd289 100644 --- a/src/PickupDelivery.php +++ b/src/PickupDelivery.php @@ -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, @@ -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 ) ) { @@ -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() : '', @@ -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, @@ -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; @@ -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']; diff --git a/src/ShippingProvider/Auto.php b/src/ShippingProvider/Auto.php index ca7705b..2bd7e77 100644 --- a/src/ShippingProvider/Auto.php +++ b/src/ShippingProvider/Auto.php @@ -31,6 +31,8 @@ abstract class Auto extends Simple implements ShippingProviderAuto { 'label_return_auto_shipment_status' => 'gzd-processing', 'label_auto_shipment_status_shipped' => false, 'label_references' => array(), + 'pickup_locations_enable' => true, + 'pickup_locations_max_results' => 20, 'configuration_sets' => array(), ); @@ -99,6 +101,18 @@ public function get_label_auto_shipment_status( $context = 'view' ) { return $this->get_prop( 'label_auto_shipment_status', $context ); } + public function get_pickup_locations_enable( $context = 'view' ) { + return $this->get_prop( 'pickup_locations_enable', $context ); + } + + public function get_pickup_locations_max_results( $context = 'view' ) { + return $this->get_prop( 'pickup_locations_max_results', $context ); + } + + public function enable_pickup_location_delivery() { + return $this->get_pickup_locations_enable(); + } + public function get_label_references( $context = 'view' ) { $references = array_filter( (array) $this->get_prop( 'label_references', $context ) ); @@ -272,6 +286,14 @@ public function set_label_return_auto_shipment_status( $status ) { $this->set_prop( 'label_return_auto_shipment_status', $status ); } + public function set_pickup_locations_enable( $enable ) { + $this->set_prop( 'pickup_locations_enable', wc_string_to_bool( $enable ) ); + } + + public function set_pickup_locations_max_results( $max_results ) { + $this->set_prop( 'pickup_locations_max_results', absint( $max_results ) ); + } + public function get_label_classname( $type ) { $classname = '\Vendidero\Germanized\Shipments\Labels\Label'; @@ -348,6 +370,16 @@ public function get_label_fields_html( $shipment ) { return apply_filters( "{$this->get_hook_prefix()}label_fields_html", $html, $shipment, $this ); } + public function get_section_help_link( $section ) { + $help_link = parent::get_section_help_link( $section ); + + if ( 'pickup_locations' === $section ) { + $help_link = 'https://vendidero.de/dokument/lieferung-an-eine-abholstation'; + } + + return $help_link; + } + protected function get_printing_settings() { $settings = array( array( @@ -404,6 +436,49 @@ protected function get_printing_settings() { return $settings; } + protected function get_pickup_locations_settings() { + $settings = array( + array( + 'title' => _x( 'Pickup Locations', 'shipments', 'woocommerce-germanized-shipments' ), + 'type' => 'title', + 'id' => 'shipping_provider_pickup_locations_options', + ), + array( + 'title' => _x( 'Enable', 'shipments', 'woocommerce-germanized-shipments' ), + 'desc' => _x( 'Allow customers to choose a pickup location within checkout.', 'shipments', 'woocommerce-germanized-shipments' ), + 'id' => 'pickup_locations_enable', + 'type' => 'gzd_toggle', + 'default' => 'yes', + 'value' => wc_bool_to_string( $this->get_setting( 'pickup_locations_enable', 'yes' ) ), + ), + array( + 'title' => _x( 'Limit results', 'shipments', 'woocommerce-germanized-shipments' ), + 'type' => 'number', + 'id' => 'pickup_locations_max_results', + 'value' => $this->get_setting( 'pickup_locations_max_results', 20 ), + 'desc_tip' => _x( 'Limit the number of pickup locations presented to the customer.', 'shipments', 'woocommerce-germanized-shipments' ), + 'default' => 20, + 'css' => 'max-width: 60px;', + 'custom_attributes' => array( + 'max' => 50, + 'data-show_if_pickup_locations_enable' => '', + ), + ), + ); + + $settings = array_merge( + $settings, + array( + array( + 'type' => 'sectionend', + 'id' => 'shipping_provider_pickup_locations_options', + ), + ) + ); + + return $settings; + } + protected function get_automation_settings() { $settings = array( array( @@ -490,6 +565,10 @@ public function get_settings_help_pointers( $section = '' ) { return array(); } + public function supports_pickup_locations() { + return false; + } + public function supports_pickup_location_delivery( $address, $query_args = array() ) { return false; } @@ -596,7 +675,7 @@ protected function parse_pickup_location_query_args( $query_args ) { array( 'max_dimensions' => array(), 'max_weight' => 0.0, - 'limit' => 10, + 'limit' => $this->get_pickup_locations_max_results(), 'payment_gateway' => '', ) ); @@ -914,6 +993,15 @@ public function get_setting_sections() { ) ); + if ( $this->supports_pickup_locations() ) { + $sections = array_merge( + $sections, + array( + 'pickup_locations' => _x( 'Pickup Locations', 'shipments', 'woocommerce-germanized-shipments' ), + ) + ); + } + $sections = array_replace_recursive( $sections, parent::get_setting_sections() ); return $sections; diff --git a/src/ShippingProvider/Simple.php b/src/ShippingProvider/Simple.php index 83353e3..9ef6313 100644 --- a/src/ShippingProvider/Simple.php +++ b/src/ShippingProvider/Simple.php @@ -126,6 +126,10 @@ public function get_help_link() { return ''; } + public function get_section_help_link( $section ) { + return ''; + } + public function get_signup_link() { return ''; } diff --git a/woocommerce-germanized-shipments.php b/woocommerce-germanized-shipments.php index 8e60e6a..af06787 100644 --- a/woocommerce-germanized-shipments.php +++ b/woocommerce-germanized-shipments.php @@ -5,7 +5,7 @@ * Description: The Germanized Shipments integration, installed as a feature plugin for development and testing purposes. * Author: vendidero * Author URI: https://vendidero.de - * Version: 3.4.1 + * Version: 3.5.0 * Requires PHP: 5.6 * License: GPLv3 *