diff --git a/assets/js/admin-settings.js b/assets/js/admin-settings.js index 97611a4e..eec374c1 100644 --- a/assets/js/admin-settings.js +++ b/assets/js/admin-settings.js @@ -10,7 +10,7 @@ window.germanized.admin = window.germanized.admin || {}; self.params = wc_gzd_admin_shipment_settings_params; $( document ) - .on( 'click', 'a.woocommerce-gzd-input-toggle-trigger', self.onInputToggleClick ) + .on( 'click', 'a.woocommerce-gzd-shipment-input-toggle-trigger', self.onInputToggleClick ) .on( 'change gzd_shipments_show_or_hide_fields', 'table.form-table :input[id]', self.onChangeInput ); $( 'table.form-table :input[id]' ).trigger( 'gzd_shipments_show_or_hide_fields' ); diff --git a/includes/wc-gzd-shipment-functions.php b/includes/wc-gzd-shipment-functions.php index 6966421d..040fa592 100644 --- a/includes/wc-gzd-shipment-functions.php +++ b/includes/wc-gzd-shipment-functions.php @@ -1313,6 +1313,15 @@ function wc_gzd_get_order_shipping_provider( $order ) { $provider = $method->get_shipping_provider_instance(); } + if ( ! $provider ) { + foreach ( array_reverse( wc_gzd_get_shipment_order( $order )->get_shipments() ) as $shipment ) { + if ( $shipment->get_shipping_provider_instance() ) { + $provider = $shipment->get_shipping_provider_instance(); + break; + } + } + } + /** * Filters the shipping provider detected for a specific order. * diff --git a/src/Admin/Admin.php b/src/Admin/Admin.php index cf7abbf7..0273c0fc 100644 --- a/src/Admin/Admin.php +++ b/src/Admin/Admin.php @@ -89,6 +89,108 @@ function() { add_filter( 'bulk_actions-' . ( 'shop_order' === self::get_order_screen_id() ? 'edit-shop_order' : self::get_order_screen_id() ), array( __CLASS__, 'define_order_bulk_actions' ), 10, 1 ); } ); + + add_action( 'woocommerce_admin_field_gzd_toggle', array( __CLASS__, 'register_toggle_field' ), 30 ); + add_filter( 'woocommerce_admin_settings_sanitize_option', array( __CLASS__, 'sanitize_toggle_field' ), 10, 3 ); + } + + public static function sanitize_toggle_field( $value, $option, $raw_value ) { + if ( Package::is_integration() ) { + return $value; + } + + $option = wp_parse_args( + $option, + array( + 'type' => '', + ) + ); + + if ( 'gzd_toggle' === $option['type'] ) { + $value = '1' === $raw_value || 'yes' === $raw_value ? 'yes' : 'no'; + } + + return $value; + } + + public static function register_toggle_field( $value ) { + if ( Package::is_integration() ) { + return; + } + + // Description handling. + $field_description_data = \WC_Admin_Settings::get_field_description( $value ); + + if ( ! isset( $value['value'] ) ) { + $value['value'] = \WC_Admin_Settings::get_option( $value['id'], $value['default'] ); + } + + if ( ! isset( $value['checkboxgroup'] ) || 'start' === $value['checkboxgroup'] ) { + ?> + + + + + +
+ +
+ + +
+ + + + '', + 'css' => '', + 'value' => '', + 'class' => '', + 'name' => '', + 'suffix' => '', + 'desc_tip' => false, + 'desc' => '', + 'custom_attributes' => array(), + ) + ); + $args['value'] = wc_bool_to_string( $args['value'] ); + $args['name'] = empty( $args['name'] ) ? $args['id'] : $args['name']; + // Description handling. + $field_description_data = \WC_Admin_Settings::get_field_description( $args ); + ?> + + + + + $attribute_value ) { + echo esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '" '; + } + } + ?> + /> + get_shipping_providers(); + public static function migrate_to_configuration_sets( $providers_to_migrate = array() ) { + $providers = empty( $providers_to_migrate ) ? Helper::instance()->get_shipping_providers() : $providers_to_migrate; + $provider_ids = array(); foreach ( $providers as $provider ) { if ( is_a( $provider, '\Vendidero\Germanized\Shipments\ShippingProvider\Auto' ) ) { + $provider_ids[] = $provider->get_id(); + if ( is_callable( array( $provider, 'get_configuration_sets' ) ) ) { $config_data = array(); @@ -173,6 +176,10 @@ public static function maybe_migrate_to_configuration_sets() { $provider_name = $settings['shipping_provider']; if ( $provider = wc_gzd_get_shipping_provider( $provider_name ) ) { + if ( ! in_array( $provider->get_id(), $provider_ids, true ) ) { + continue; + } + $settings_prefix = "{$provider_name}_"; $config_data = array(); @@ -202,10 +209,6 @@ public static function maybe_migrate_to_configuration_sets() { } } - public static function get_excluded_methods() { - return apply_filters( 'woocommerce_gzd_shipments_get_methods_excluded_from_provider_settings', array( 'pr_dhl_paket', 'flexible_shipping_info' ) ); - } - private static function update_providers() { $providers = Helper::instance()->get_shipping_providers(); diff --git a/src/Interfaces/ShippingProvider.php b/src/Interfaces/ShippingProvider.php index c725905f..80bcb52e 100644 --- a/src/Interfaces/ShippingProvider.php +++ b/src/Interfaces/ShippingProvider.php @@ -76,7 +76,7 @@ public function get_setting( $key, $default = null ); public function update_settings( $section = '', $data = null, $save = true ); - public function get_settings( $section = '', $for_shipping_method = false ); + public function get_settings( $section = '' ); public function update_setting( $setting, $value ); diff --git a/src/Labels/ConfigurationSet.php b/src/Labels/ConfigurationSet.php index 240c6043..299ab0d1 100644 --- a/src/Labels/ConfigurationSet.php +++ b/src/Labels/ConfigurationSet.php @@ -302,7 +302,12 @@ public function has_setting( $id ) { $id = $details['suffix'] . '_' . $details['service_meta']; } - if ( array_key_exists( $id, $this->get_settings() ) ) { + $all_settings = $this->get_settings(); + $the_setting = array_key_exists( $id, $all_settings ) ? $all_settings[ $id ] : null; + + if ( 'product' === $id && '' === $the_setting ) { + return false; + } elseif ( ! is_null( $the_setting ) ) { return true; } diff --git a/src/Labels/ConfigurationSetTrait.php b/src/Labels/ConfigurationSetTrait.php index 3436d990..7f994bb3 100644 --- a/src/Labels/ConfigurationSetTrait.php +++ b/src/Labels/ConfigurationSetTrait.php @@ -235,7 +235,7 @@ public function reset_configuration_sets( $args = array() ) { $configuration_sets = $this->get_configuration_sets( 'edit' ); foreach ( $configuration_sets as $set_id => $set ) { - if ( substr( $set_id, 0, strlen( $id_prefix ) ) === $id_prefix ) { + if ( strstr( $set_id, $id_prefix ) ) { unset( $configuration_sets[ $set_id ] ); } } diff --git a/src/Package.php b/src/Package.php index 3891ee2f..d89fa331 100644 --- a/src/Package.php +++ b/src/Package.php @@ -153,6 +153,12 @@ public static function is_packing_supported() { return version_compare( phpversion(), '7.1', '>=' ) && apply_filters( 'woocommerce_gzd_enable_rucksack_packaging', true ); } + public static function is_integration() { + $gzd_installed = class_exists( 'WooCommerce_Germanized' ); + + return $gzd_installed; + } + /** * @return int[] */ diff --git a/src/ShippingMethod/ShippingMethod.php b/src/ShippingMethod/ShippingMethod.php index 35a8fe48..eda110c2 100644 --- a/src/ShippingMethod/ShippingMethod.php +++ b/src/ShippingMethod/ShippingMethod.php @@ -209,12 +209,19 @@ public function init_form_fields() { 'default' => $this->method_title, 'desc_tip' => true, ), + 'free_title' => array( + 'title' => _x( 'Title (free shipping)', 'shipments', 'woocommerce-germanized-shipments' ), + 'type' => 'text', + 'description' => _x( 'This controls the title which the user sees during checkout in case a free shipping option is available.', 'shipments', 'woocommerce-germanized-shipments' ), + 'default' => sprintf( _x( 'Free shipping (via %1$s)', 'shipments', 'woocommerce-germanized-shipments' ), $this->method_title ), + 'desc_tip' => true, + ), 'shipping_rules_title' => array( 'title' => _x( 'Shipping Rules', 'shipments', 'woocommerce-germanized-shipments' ), 'type' => 'title', 'id' => 'shipping_rules_title', 'default' => '', - 'description' => sprintf( _x( 'Configure shipping costs per packaging option. Within cart, a rucksack algorithm will automatically fit the items in the packaging option(s) available and calculate it\'s cost.
Some important hints on the calculation logic:
  1. The from value (e.g. 3) is expected to be inclusive (greater or equal 3). The to value (e.g. 5) is expected to be exclusive (smaller than 5).
  2. Leave the to value empty for your last packaging rule to match all subsequent values.
  3. In case a free shipping rule is available, the conditional logic automatically stops.
  4. The all remaining packaging rules will be used for available packaging options without custom rules and serve as fallback in case no applicable rule was found.
  5. In case no all remaining packaging rule exists, only packaging options with custom rules will be used for packing.
', 'shipments', 'woocommerce-germanized-shipments' ) ), + 'description' => sprintf( _x( 'Configure shipping costs per packaging option. Within cart, a rucksack algorithm will automatically fit the items in the packaging option(s) available and calculate it\'s cost.
Some important hints on the calculation logic:
  1. The from value (e.g. 3) is expected to be inclusive (greater or equal 3). The to value (e.g. 5) is expected to be exclusive (smaller than 5).
  2. Leave the to value empty for your last packaging rule to match all subsequent values.
  3. All conditions must be met for the shipping rule to apply.
  4. In case a free shipping rule is available, the conditional logic automatically stops.
  5. The all remaining packaging rules will be used for available packaging options without custom rules and serve as fallback in case no applicable rule was found.
  6. In case no all remaining packaging rule exists, only packaging options with custom rules will be used for packing.
', 'shipments', 'woocommerce-germanized-shipments' ) ), ), 'multiple_shipments_cost_calculation_mode' => array( 'title' => _x( 'Multiple packages', 'shipments', 'woocommerce-germanized-shipments' ), @@ -363,10 +370,10 @@ public function get_condition_type( $type ) { } public function get_rate_label( $costs ) { - $label = $this->get_method_title(); + $label = $this->get_title(); if ( 0.0 === $costs ) { - $label = sprintf( _x( 'Free Shipping (via %1$s)', 'shipments', 'woocommerce-germanized-shipments' ), $this->get_shipping_provider()->get_title() ); + $label = $this->get_instance_option( 'free_title', sprintf( _x( 'Free shipping (via %1$s)', 'shipments', 'woocommerce-germanized-shipments' ), $this->get_method_title() ) ); } return $label; diff --git a/src/ShippingProvider/Auto.php b/src/ShippingProvider/Auto.php index d2d616d2..5f1b7c60 100644 --- a/src/ShippingProvider/Auto.php +++ b/src/ShippingProvider/Auto.php @@ -23,8 +23,6 @@ abstract class Auto extends Simple implements ShippingProviderAuto { use ConfigurationSetTrait; protected $extra_data = array( - 'label_default_shipment_weight' => '', - 'label_minimum_shipment_weight' => '', 'label_print_format' => '', 'label_auto_enable' => false, 'label_auto_shipment_status' => 'gzd-processing', @@ -35,17 +33,7 @@ abstract class Auto extends Simple implements ShippingProviderAuto { ); public function get_label_default_shipment_weight( $context = 'view' ) { - $weight = $this->get_prop( 'label_default_shipment_weight', $context ); - - if ( 'view' === $context && '' === $weight ) { - $weight = $this->get_default_label_default_shipment_weight(); - } - - return $weight; - } - - protected function get_default_label_default_shipment_weight() { - return 0; + return apply_filters( "{$this->get_hook_prefix()}label_default_shipment_weight", 0.5, $this ); } protected function get_default_label_default_print_format() { @@ -53,17 +41,7 @@ protected function get_default_label_default_print_format() { } public function get_label_minimum_shipment_weight( $context = 'view' ) { - $weight = $this->get_prop( 'label_minimum_shipment_weight', $context ); - - if ( 'view' === $context && '' === $weight ) { - $weight = $this->get_default_label_minimum_shipment_weight(); - } - - return $weight; - } - - protected function get_default_label_minimum_shipment_weight() { - return 0.5; + return apply_filters( "{$this->get_hook_prefix()}label_minimum_shipment_weight", 0.01, $this ); } /** @@ -128,14 +106,6 @@ public function is_sandbox() { return false; } - public function set_label_default_shipment_weight( $weight ) { - $this->set_prop( 'label_default_shipment_weight', ( '' === $weight ? '' : wc_format_decimal( $weight ) ) ); - } - - public function set_label_minimum_shipment_weight( $weight ) { - $this->set_prop( 'label_minimum_shipment_weight', ( '' === $weight ? '' : wc_format_decimal( $weight ) ) ); - } - public function set_label_auto_enable( $enable ) { $this->set_prop( 'label_auto_enable', wc_string_to_bool( $enable ) ); } @@ -232,10 +202,9 @@ public function get_label_fields_html( $shipment ) { protected function get_printing_settings() { $settings = array( array( - 'title' => _x( 'Label Format', 'shipments', 'woocommerce-germanized-shipments' ), - 'allow_override' => true, - 'type' => 'title', - 'id' => 'shipping_provider_label_format_options', + 'title' => _x( 'Label Format', 'shipments', 'woocommerce-germanized-shipments' ), + 'type' => 'title', + 'id' => 'shipping_provider_label_format_options', ), ); @@ -286,13 +255,12 @@ protected function get_printing_settings() { return $settings; } - protected function get_automation_settings( $for_shipping_method = false ) { + protected function get_automation_settings() { $settings = array( array( - 'title' => _x( 'Automation', 'shipments', 'woocommerce-germanized-shipments' ), - 'allow_override' => true, - 'type' => 'title', - 'id' => 'shipping_provider_label_auto_options', + 'title' => _x( 'Automation', 'shipments', 'woocommerce-germanized-shipments' ), + 'type' => 'title', + 'id' => 'shipping_provider_label_auto_options', ), ); @@ -422,50 +390,8 @@ protected function get_config_set_return_label_settings() { return $this->get_label_settings_by_shipment_type( 'return' ); } - protected function get_label_settings( $for_shipping_method = false ) { - $settings = array(); - - $settings = array_merge( - $settings, - array( - array( - 'title' => '', - 'type' => 'title', - 'id' => 'shipping_provider_label_options', - ), - - array( - 'title' => _x( 'Default content weight (kg)', 'shipments', 'woocommerce-germanized-shipments' ), - 'type' => 'text', - 'desc' => _x( 'Choose a default shipment content weight to be used for labels if no weight has been applied to the shipment.', 'shipments', 'woocommerce-germanized-shipments' ), - 'desc_tip' => true, - 'id' => 'label_default_shipment_weight', - 'css' => 'max-width: 60px;', - 'class' => 'wc_input_decimal', - 'default' => $this->get_default_label_default_shipment_weight(), - 'value' => wc_format_localized_decimal( $this->get_setting( 'label_default_shipment_weight' ) ), - ), - - array( - 'title' => _x( 'Minimum weight (kg)', 'shipments', 'woocommerce-germanized-shipments' ), - 'type' => 'text', - 'desc' => _x( 'Choose a minimum weight to be used for labels e.g. to prevent low shipment weight errors.', 'shipments', 'woocommerce-germanized-shipments' ), - 'desc_tip' => true, - 'id' => 'label_minimum_shipment_weight', - 'css' => 'max-width: 60px;', - 'class' => 'wc_input_decimal', - 'default' => $this->get_default_label_minimum_shipment_weight(), - 'value' => wc_format_localized_decimal( $this->get_setting( 'label_minimum_shipment_weight' ) ), - ), - - array( - 'type' => 'sectionend', - 'id' => 'shipping_provider_label_options', - ), - ) - ); - - return $settings; + protected function get_label_settings() { + return array(); } /** @@ -518,7 +444,7 @@ protected function get_label_settings_by_zone( $configuration_set ) { 'type' => 'select', 'id' => $product_setting_id, 'default' => 'shipping_provider' === $configuration_set->get_setting_type() ? $default_product : $this->get_setting( $product_setting_id, $default_product ), - 'value' => $configuration_set->get_product() ? $configuration_set->get_product() : $default_product, + 'value' => $configuration_set->get_product() ? $configuration_set->get_product() : null, 'desc' => sprintf( _x( 'Select the default service for %1$s shipments.', 'shipments', 'woocommerce-germanized-shipments' ), wc_gzd_get_shipping_label_zone_title( $configuration_set->get_zone() ) ), 'options' => $select, 'class' => 'wc-enhanced-select', diff --git a/src/ShippingProvider/Simple.php b/src/ShippingProvider/Simple.php index b87f2a9b..abcbefa1 100644 --- a/src/ShippingProvider/Simple.php +++ b/src/ShippingProvider/Simple.php @@ -561,13 +561,22 @@ public function set_supports_guest_returns( $supports ) { $this->set_prop( 'supports_guest_returns', wc_string_to_bool( $supports ) ); } + protected function setting_supports_default_update( $setting ) { + $type = isset( $setting['type'] ) ? $setting['type'] : 'title'; + + if ( in_array( $type, array( 'title', 'sectionend', 'html' ), true ) || ! isset( $setting['id'] ) || empty( $setting['id'] ) ) { + return false; + } + + return true; + } + public function update_settings_with_defaults() { foreach ( $this->get_all_settings() as $section => $settings ) { foreach ( $settings as $setting ) { - $type = isset( $setting['type'] ) ? $setting['type'] : 'title'; $default = isset( $setting['default'] ) ? $setting['default'] : null; - if ( in_array( $type, array( 'title', 'sectionend', 'html' ), true ) || ! isset( $setting['id'] ) || empty( $setting['id'] ) ) { + if ( ! $this->setting_supports_default_update( $setting ) ) { continue; } @@ -828,7 +837,7 @@ protected function get_general_hook_prefix() { } } - protected function get_general_settings( $for_shipping_method = false ) { + protected function get_general_settings() { $settings = array( array( 'title' => '', @@ -994,15 +1003,15 @@ public function update_setting( $setting, $value ) { } } - public function get_settings( $section = '', $for_shipping_method = false ) { + public function get_settings( $section = '' ) { $settings = array(); if ( '' === $section || 'general' === $section ) { - $settings = $this->get_general_settings( $for_shipping_method ); + $settings = $this->get_general_settings(); } elseif ( 'returns' === $section ) { - $settings = $this->get_return_settings( $for_shipping_method ); + $settings = $this->get_return_settings(); } elseif ( is_callable( array( $this, "get_{$section}_settings" ) ) ) { - $settings = $this->{"get_{$section}_settings"}( $for_shipping_method ); + $settings = $this->{"get_{$section}_settings"}(); } /** @@ -1019,10 +1028,10 @@ public function get_settings( $section = '', $for_shipping_method = false ) { * @since 3.0.6 * @package Vendidero/Germanized/Shipments */ - return apply_filters( $this->get_hook_prefix() . 'settings', $settings, $section, $this, $for_shipping_method ); + return apply_filters( $this->get_hook_prefix() . 'settings', $settings, $section, $this ); } - protected function get_return_settings( $for_shipping_method = false ) { + protected function get_return_settings() { $settings = array( array( 'title' => '', @@ -1098,12 +1107,12 @@ protected function get_return_settings( $for_shipping_method = false ) { return $settings; } - protected function get_all_settings( $for_shipping_method = false ) { + protected function get_all_settings() { $settings = array(); $sections = array_keys( $this->get_setting_sections() ); foreach ( $sections as $section ) { - $settings[ $section ] = $this->get_settings( $section, $for_shipping_method ); + $settings[ $section ] = $this->get_settings( $section ); } return $settings;