Right now the FedEx, Flat right, UPS and USPS modules output the shipping options in alphabetical order. so "2 day" shipping is at the top and "ground" is in the middle. I really think this should be sorted by price because most consumers are familiar with it being ordered that way. Always have the least expensive option on top so that it defaults to what the user is most likely going to want - because right now the default is "2 day" which is my most expensive option of shipping.

It would also be nice to order or set the weights of all shipping options so I can say I want my "free shipping" flat rate on the top (and default) and then have FedEx next, then UPS. or intermingle FedEx and UPS so they gradually increase in price and expedition.

Is there a way to do this?

Comments

deggertsen’s picture

It appears this may have been fixed at one time? #1324646: Sort shipping-rules by weight

However, I can't find where to set the weights, so maybe it's broken again.

davidwhthomas’s picture

Perhaps it's because the rules are now components, which don't have a weight property per se.

I found there's a hook you can use to alter the commerce shipping option ordering.

hook_commerce_shipping_service_rate_options_alter

For example, I prefixed the machine name of the shipping rule with a number

e.g

001_courier_1
004_courier_2
008_courier_3

And then did a sort on those with this code:

/**
 * Implements hook_commerce_shipping_service_rate_options_alter
 * Sort order the list of shipping options
 */
function MODULE_commerce_shipping_service_rate_options_alter(&$options, $order){
  // Natural sort
  natsort($options);
}

HTH,

Cheers,
DT

googletorp’s picture

Status: Active » Fixed
deggertsen’s picture

Status: Fixed » Active

Why would this be fixed? It seems to me that at least the code portion of #2 should actually be committed to Commerce Shipping instead of it being in a custom module. I might be willing to create a patch if necessary, but it seems like it would be simple to add natsort($options); to the shipping rate options as the default, then if people wanted to override it they could.

davidwhthomas’s picture

I was thinking a code patch that added a menu tab to the shipping config area for sorting shipping options would be nice.
Ideally a drag/drop table as per other core sorting interfaces.
Save the form to set a $conf variable with the weight ordering.
Apply that weight in hook_commerce_shipping_service_rate_options_alter
That would do it.

mrpeanut’s picture

Issue summary: View changes

Sorting by price can be done with this code (from #1537394-22: Change Shipping Services Row Weights):

/**
* Implements hook_commerce_shipping_service_rate_options_alter()
*/
function [module_name]_commerce_shipping_service_rate_options_alter(&$options, $order) {
    // If there is more than one shipping service available, sort them by line item unit price
    if (count($order->shipping_rates) > 1) {
        // Collect rates from line items
        $service_prices = array();
        foreach ($order->shipping_rates as $service => $line_item) {
            $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
            $unit_price = $line_item_wrapper->commerce_unit_price->value();
          $service_prices[$service] = $unit_price['amount'];
        }
        // array_multisort can be used here because $options and $order->shipping_rates contain the same number of elements (i.e. services).
        array_multisort($service_prices, SORT_NUMERIC, SORT_ASC, $options);
    }
}

However, it would be nice to have weights for more control.

googletorp’s picture

Status: Active » Fixed
deggertsen’s picture

Title: Sort the order of shipping options » Sort the order of shipping options (add weight)
Category: Support request » Feature request
Status: Fixed » Active

Please don't mark as fixed without comment or justification. As of right now it appears that this needs to be changed to a feature request to add weights to the shipping options. It appears that there is already a weight option for fixed Flat Rate Shipping (see #1537394: Change Shipping Services Row Weights, but not for all shipping options in general. I believe that is the request here.

googletorp’s picture

Status: Active » Closed (won't fix)

Given the lack of interest in this (no comments in 2 years) I'm closing this (won't fix).

If any one want to see this happen and actually want to work on this (have a patch) they are most welcome to reopen. I'll be happy to review the patch, but won't actually want to develop this myself.