Problem/Motivation

Currently you can filter shipping services by mail class but within each mail class multiple shipping services are offered. It would be beneficial to implement additional filtering options so we can have more control over what rates are returned.

Proposed resolution

Implement processingCategory and destinationEntryFacilityType filters.

User interface changes

Add additional filtering capabilities. Ideally multi select checkboxes similar to the mail class

CommentFileSizeAuthor
#7 3568335-rate-filtering.patch12.16 KBrhovland
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

moxojo created an issue. See original summary.

tbkot made their first commit to this issue’s fork.

rhovland made their first commit to this issue’s fork.

rhovland’s picture

You're using a double negative which makes the code hard to understand.
The configuration form selection is doing the opposite of what it's supposed to be doing due to the current logic.

This code checks if the rate is not allowed.

  /**
   * Whether the rate is not allowed to use.
   *
   * @param string $rate_property
   *   The value of rate property.
   * @param array $property_options
   *   The list of allowed values.
   */
  private function isRateNotAllowed(string $rate_property, array $property_options): bool {
    return !empty($property_options) && !in_array(strtolower($rate_property), $property_options);
  }

But the code that calls it (modified to make reading and debugging easier) is evaluating if any of them are true. The end result is selected categories are allowed and ones that are not selected are skipped.

      $service_allowed = $this->isRateNotAllowed($rate['mailClass'], $this->configuration['services']);
      $processing_category_allowed = $this->isRateNotAllowed($rate['processingCategory'], $this->configuration['rate_options']['categories']);
      $destination_entry_type_allowed = $this->isRateNotAllowed($rate['destinationEntryFacilityType'], $this->configuration['rate_options']['facility_types']);
      // Filter out rates by mail service, category, or facility type.
      if ($service_allowed|| $processing_category_allowed || $destination_entry_type_allowed) {
        continue;
      }

Additionally the configuration UI for this is confusing. Services are checked to include them. The same logic should be used for category and entry facility.

Changing the logic from exclude to include also greatly simplifies the code.

The changes I made should also mostly fix the tests. I think they need to specify facility_types and categories and they aren't

rhovland’s picture

I attempted to add defaults to the config form but even when values are set for categories and facility_types when you load the form all of the boxes are checked again. Dunno if I'm trying to set the defaults wrong or if this is a known bug with the shipping method form.

  public function defaultConfiguration(): array {
    return [
      'rate_label' => '',
      'rate_description' => '',
      'api_information' => [
        'client_id' => '',
        'secret' => '',
        'mode' => 'test',
      ],
      'rate_options' => [
        'price_type' => 'retail',
        'account_type' => 'eps',
        'account_number' => '',
        'account_crid' => '',
        'categories' => ['cards', 'letters', 'flats', 'machinable', 'nonstandard', 'catalogs', 'open_and_distribute', 'returns', 'soft_pack_machinable', 'soft_pack_non_machinable'],
        'facility_types' => ['none', 'area_distribution_center', 'auxiliary_service_facility', 'destination_delivery_unit', 'destination_network_distribution_center', 'destination_sectional_center_facility', 'destination_service_hub', 'international_service_center'],
        'rate_multiplier' => 1.0,
        'round' => PHP_ROUND_HALF_UP,
      ],
      'options' => [
        'tracking_url' => 'https://tools.usps.com/go/TrackConfirmAction?tLabels=[tracking_code]',
        'log' => [],
      ],
    ] + parent::defaultConfiguration();

rhovland’s picture

StatusFileSize
new12.16 KB
rhovland’s picture

The behavior of the API changed sometime this weekend. I believe this was a bug fix for processing categories. However this changed the filtering from "it meets the need" to "the problem remains unsolved" when it comes to only offering the desired rates.
Previously FLATS was returning normal rates and MACHINABLE was returning flat rate box rates.
Now FLATS returns flat rate boxes and MACHINABLE returns normal rates, cubic rates, flat rate boxes.

In my case and I'm sure many other site admins, we only want to offer the base services without any of the discount rates such as cubic or flat rate boxes.

After looking at the API responses and API documentation I think there is one more filter type missing. rateIndicator indicates the type of rate on the response. You can see what the various codes mean here:
https://developers.usps.com/domesticpricesv3
https://developers.usps.com/internationalpricesv3

Domestic and International have different lists but any that appear in both have the same label so it's safe to assume one unified list would be safe to use as a filter for both.

In general most generic rates all have the SP (Single Piece) indicator with some of the more specific ones based on package size probably being
DN (Dimensional Nonrectangular)
DR (Dimensional Rectangular)
OS (Oversized)

I think having additional information to guide site builders on which filter options to select would be an important thing to add. Also, maybe we want to have a default set of options that gives base rates out of the box? If they want more options they can select additional items in the configuration. Shouldn't expect site builders to be USPS experts to use the module.

rhovland’s picture

Added support for rateIndicator. Code is untested, will work on verifying tomorrow.

jsacksick’s picture

Status: Active » Needs work

Updating the status to "Needs work" because of the failing tests.

  • tbkot committed d054d25d on 2.x
    Issue #3568335 by moxojo, rhovland, tbkot: Implement additional service...
tbkot’s picture

Status: Needs work » Fixed

Changes are merged.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.