Skip to content

Commit

Permalink
Skip custom shipping address option on installation to prevent wrong …
Browse files Browse the repository at this point in the history
…shipping country issues. Added option to select whether to have a custom return address or not to improve data quality.
  • Loading branch information
dennisnissle committed Jul 16, 2024
1 parent 831c23c commit 7594757
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 44 deletions.
27 changes: 23 additions & 4 deletions includes/wc-gzd-shipment-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,18 +811,37 @@ function wc_gzd_get_shipment_setting_default_address_fields( $type = 'shipper' )
*/
function wc_gzd_get_shipment_setting_address_fields( $address_type = 'shipper' ) {
$default_address_fields = array_keys( wc_gzd_get_shipment_setting_default_address_fields( $address_type ) );
$default_address_data = array();

if ( 'return' === $address_type ) {
$default_address_data = wc_gzd_get_shipment_setting_address_fields( 'shipper' );

if ( 'no' === Package::get_setting( 'use_alternate_return' ) ) {
return apply_filters( "woocommerce_gzd_shipment_{$address_type}_address_fields", $default_address_data, $address_type );
}

$default_address_data['country'] = $default_address_data['country'] . ':' . $default_address_data['state'];
} else {
$default_address_data = array(
'company' => get_option( 'blogname', '' ),
'address_1' => get_option( 'woocommerce_store_address', '' ),
'address_2' => get_option( 'woocommerce_store_address_2', '' ),
'city' => get_option( 'woocommerce_store_city', '' ),
'postcode' => get_option( 'woocommerce_store_postcode', '' ),
'email' => get_option( 'woocommerce_email_from_address', '' ),
'country' => get_option( 'woocommerce_default_country', '' ),
);
}

foreach ( $default_address_fields as $prop ) {
$key = "woocommerce_gzd_shipments_{$address_type}_address_{$prop}";
$value = get_option( $key, '' );
$value = get_option( $key, null );

if ( '' === $value && array_key_exists( $prop, $default_address_data ) ) {
$value = $default_address_data[ $prop ];
if ( null === $value ) {
if ( array_key_exists( $prop, $default_address_data ) && ! in_array( $prop, array( 'state' ), true ) ) {
$value = $default_address_data[ $prop ];
} else {
$value = '';
}
}

$address_fields[ $prop ] = $value;
Expand Down
48 changes: 48 additions & 0 deletions src/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static function init() {
add_action( 'woocommerce_gzd_admin_settings_after_save_shipments_packaging', array( __CLASS__, 'save_packaging_list' ), 10 );

add_action( 'woocommerce_admin_field_packaging_reports', array( __CLASS__, 'output_packaging_reports' ) );
add_action( 'woocommerce_admin_field_shipments_country_select', array( __CLASS__, 'output_custom_country_select' ) );

// Menu count
add_action( 'admin_head', array( __CLASS__, 'menu_return_count' ) );
Expand Down Expand Up @@ -1116,6 +1117,53 @@ public static function output_return_reasons_field( $value ) {
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/**
* Register this custom country field as the builtin country field misses
* custom attributes (e.g. for custom show/hide logic)
*
* @param array $value
*
* @return void
*/
public static function output_custom_country_select( $value ) {
// Custom attribute handling.
$custom_attributes = array();

if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) ) {
foreach ( $value['custom_attributes'] as $attribute => $attribute_value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}

// Description handling.
$field_description = \WC_Admin_Settings::get_field_description( $value );
$description = $field_description['description'];
$tooltip_html = $field_description['tooltip_html'];

$country_setting = (string) $value['value'];

if ( strstr( $country_setting, ':' ) ) {
$country_setting = explode( ':', $country_setting );
$country = current( $country_setting );
$state = end( $country_setting );
} else {
$country = $country_setting;
$state = '*';
}
?>
<tr class="<?php echo esc_attr( $value['row_class'] ); ?>">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo wp_kses_post( $tooltip_html ); ?></label>
</th>
<td class="forminp">
<select name="<?php echo esc_attr( $value['field_name'] ); ?>" style="<?php echo esc_attr( $value['css'] ); ?>" data-placeholder="<?php echo esc_attr_x( 'Choose a country / region&hellip;', 'shipments', 'woocommerce-germanized-shipments' ); ?>" aria-label="<?php echo esc_attr_x( 'Country / Region', 'shipments', 'woocommerce-germanized-shipments' ); ?>" class="wc-enhanced-select" <?php echo implode( ' ', $custom_attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
<?php WC()->countries->country_dropdown_options( $country, $state ); ?>
</select> <?php echo wp_kses_post( $description ); ?>
</td>
</tr>
<?php
}

public static function output_packaging_reports( $value ) {
$reports = ReportHelper::get_reports();
ob_start();
Expand Down
55 changes: 24 additions & 31 deletions src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ protected static function get_address_field_type_by_prop( $prop ) {
$type = 'text';

if ( 'country' === $prop ) {
$type = 'single_select_country';
$type = 'shipments_country_select';
}

return $type;
Expand Down Expand Up @@ -330,37 +330,21 @@ protected static function get_address_settings() {
),
);

// Use WooCommerce address data as fallback/default data on install
$default_shipper_address_data = array(
'company' => get_option( 'name' ),
'address_1' => get_option( 'woocommerce_store_address' ),
'address_2' => get_option( 'woocommerce_store_address_2' ),
'city' => get_option( 'woocommerce_store_city' ),
'postcode' => get_option( 'woocommerce_store_postcode' ),
'email' => get_option( 'woocommerce_email_from_address' ),
'country' => get_option( 'woocommerce_default_country' ),
);

foreach ( $shipper_fields as $field => $value ) {
if ( in_array( $field, self::get_address_fields_to_skip(), true ) ) {
continue;
}

$default_value = '';

if ( array_key_exists( $field, $default_shipper_address_data ) ) {
$default_value = $default_shipper_address_data[ $field ];
}

$settings = array_merge(
$settings,
array(
array(
'title' => self::get_address_label_by_prop( $field ),
'type' => self::get_address_field_type_by_prop( $field ),
'id' => "woocommerce_gzd_shipments_shipper_address_{$field}",
'default' => $default_value,
'desc_tip' => self::get_address_desc_by_prop( $field ),
'title' => self::get_address_label_by_prop( $field ),
'type' => self::get_address_field_type_by_prop( $field ),
'id' => "woocommerce_gzd_shipments_shipper_address_{$field}",
'default' => 'country' === $field ? $value . ':' . $shipper_fields['state'] : $value,
'desc_tip' => self::get_address_desc_by_prop( $field ),
'skip_install' => true,
),
)
);
Expand All @@ -373,12 +357,18 @@ protected static function get_address_settings() {
'type' => 'sectionend',
'id' => 'shipments_shipper_address',
),

array(
'title' => _x( 'Return Address', 'shipments', 'woocommerce-germanized-shipments' ),
'type' => 'title',
'id' => 'shipments_return_address',
),
array(
'title' => _x( 'Alternate return?', 'shipments', 'woocommerce-germanized-shipments' ),
'desc' => _x( 'Optionally configure a separate return address', 'shipments', 'woocommerce-germanized-shipments' ),
'id' => 'woocommerce_gzd_shipments_use_alternate_return',
'default' => ! empty( get_option( 'woocommerce_gzd_shipments_return_address_address_1', '' ) ) ? 'yes' : 'no',
'type' => 'gzd_toggle',
),
)
);

Expand All @@ -391,12 +381,15 @@ protected static function get_address_settings() {
$settings,
array(
array(
'title' => self::get_address_label_by_prop( $field ),
'type' => self::get_address_field_type_by_prop( $field ),
'id' => "woocommerce_gzd_shipments_return_address_{$field}",
'default' => '',
'placeholder' => $value,
'desc_tip' => self::get_address_desc_by_prop( $field ),
'title' => self::get_address_label_by_prop( $field ),
'type' => self::get_address_field_type_by_prop( $field ),
'id' => "woocommerce_gzd_shipments_return_address_{$field}",
'default' => 'country' === $field ? $value . ':' . $return_fields['state'] : $value,
'desc_tip' => self::get_address_desc_by_prop( $field ),
'skip_install' => true,
'custom_attributes' => array(
'data-show_if_woocommerce_gzd_shipments_use_alternate_return' => '',
),
),
)
);
Expand All @@ -407,7 +400,7 @@ protected static function get_address_settings() {
array(
array(
'type' => 'sectionend',
'id' => 'shipments_return_address',
'id' => 'shipments_shipper_address',
),
)
);
Expand Down
12 changes: 3 additions & 9 deletions src/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1319,16 +1319,10 @@ protected function get_sender_address_prop( $prop, $context = 'view' ) {
$value = $provider->$getter( $context );
}
} else {
$key = "woocommerce_gzd_shipments_shipper_address_{$prop}";
$value = get_option( $key, '' );
$sender_address = wc_gzd_get_shipment_setting_address_fields();

if ( 'country' === $prop ) {
$value = wc_format_country_state_string( $value )['country'];
} elseif ( 'state' === $prop ) {
$key = 'woocommerce_gzd_shipments_shipper_address_country';
$value = get_option( $key, '' );

$value = wc_format_country_state_string( $value )['state'];
if ( array_key_exists( $prop, $sender_address ) ) {
$value = $sender_address[ $prop ];
}
}

Expand Down

0 comments on commit 7594757

Please sign in to comment.