Skip to content

Commit

Permalink
Fix config set reset. Remove min/default weight from shipping provide…
Browse files Browse the repository at this point in the history
…r (leave filters). Refactoring.
  • Loading branch information
dennisnissle committed Nov 20, 2023
1 parent 11b6dda commit ee72e70
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 111 deletions.
2 changes: 1 addition & 1 deletion assets/js/admin-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
9 changes: 9 additions & 0 deletions includes/wc-gzd-shipment-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
102 changes: 102 additions & 0 deletions src/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) {
?>
<tr valign="top">
<th scope="row" class="titledesc">
<span class="wc-gzd-label-wrap"><?php echo esc_html( $value['title'] ); ?><?php echo wp_kses_post( $field_description_data['tooltip_html'] ); ?></span>
</th>
<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>">
<fieldset>
<?php
} else {
?>
<fieldset>
<?php
}
?>
<?php self::render_toggle( $value ); ?>
</fieldset>
<?php
if ( ! isset( $value['checkboxgroup'] ) || 'end' === $value['checkboxgroup'] ) {
?>
</td>
</tr>
<?php
}
}

public static function render_toggle( $args ) {
$args = wp_parse_args(
$args,
array(
'id' => '',
'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 );
?>
<a href="#" class="woocommerce-gzd-shipment-input-toggle-trigger">
<span id="<?php echo esc_attr( $args['id'] ); ?>-toggle" class="woocommerce-gzd-input-toggle woocommerce-input-toggle woocommerce-input-toggle--<?php echo esc_attr( 'yes' === $args['value'] ? 'enabled' : 'disabled' ); ?>"><?php echo ( ( 'yes' === $args['value'] ) ? esc_html_x( 'Yes', 'shipments', 'woocommerce-germanized-shipments' ) : esc_html_x( 'No', 'shipments', 'woocommerce-germanized-shipments' ) ); ?></span>
</a>
<input
name="<?php echo esc_attr( $args['name'] ); ?>"
id="<?php echo esc_attr( $args['id'] ); ?>"
type="checkbox"
style="display: none; <?php echo esc_attr( $args['css'] ); ?>"
value="1"
class="<?php echo esc_attr( $args['class'] ); ?>"
<?php checked( $args['value'], 'yes' ); ?>
<?php
if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) {
foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) {
echo esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '" ';
}
}
?>
/><?php echo esc_html( $args['suffix'] ); ?><?php echo wp_kses_post( $field_description_data['description'] ); ?>
<?php
}

public static function register_admin_connected_pages( $is_connected, $current_page ) {
Expand Down
17 changes: 10 additions & 7 deletions src/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static function install() {
self::maybe_create_return_reasons();

if ( ! is_null( $current_version ) && version_compare( $current_version, '3.0.0', '<' ) ) {
self::maybe_migrate_to_configuration_sets();
self::migrate_to_configuration_sets();
}

self::maybe_create_packaging();
Expand Down Expand Up @@ -125,11 +125,14 @@ protected static function create_configuration_set_from_migration_data( $config_
return true;
}

public static function maybe_migrate_to_configuration_sets() {
$providers = Helper::instance()->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();

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/ShippingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
7 changes: 6 additions & 1 deletion src/Labels/ConfigurationSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Labels/ConfigurationSetTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] );
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand Down
13 changes: 10 additions & 3 deletions src/ShippingMethod/ShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/> Some important hints on the calculation logic: <ol><li>The <i>from</i> value (e.g. 3) is expected to be inclusive (greater or equal 3). The <i>to</i> value (e.g. 5) is expected to be exclusive (smaller than 5).</li><li>Leave the <i>to</i> value empty for your last packaging rule to match all subsequent values.</li><li>In case a free shipping rule is available, the conditional logic automatically stops.</li><li>The <i>all remaining packaging</i> rules will be used for available packaging options without custom rules and serve as fallback in case no applicable rule was found.</li><li>In case no <i>all remaining packaging</i> rule exists, only packaging options with custom rules will be used for packing.</li></ol>', '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.<br/> Some important hints on the calculation logic: <ol><li>The <i>from</i> value (e.g. 3) is expected to be inclusive (greater or equal 3). The <i>to</i> value (e.g. 5) is expected to be exclusive (smaller than 5).</li><li>Leave the <i>to</i> value empty for your last packaging rule to match all subsequent values.</li><li>All conditions must be met for the shipping rule to apply.</li><li>In case a free shipping rule is available, the conditional logic automatically stops.</li><li>The <i>all remaining packaging</i> rules will be used for available packaging options without custom rules and serve as fallback in case no applicable rule was found.</li><li>In case no <i>all remaining packaging</i> rule exists, only packaging options with custom rules will be used for packing.</li></ol>', 'shipments', 'woocommerce-germanized-shipments' ) ),
),
'multiple_shipments_cost_calculation_mode' => array(
'title' => _x( 'Multiple packages', 'shipments', 'woocommerce-germanized-shipments' ),
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit ee72e70

Please sign in to comment.