Shipping Option: Cheapest Shipping Method Select by Default

Hello! I need the shopping to automatically select the cheapest available shipping option by default, rather than sorting by position. Is there a easy mod for this? Thanks in advance.

You could change the fn.cart.php code to have it sort by “rate” after the rates are determined for the location.

Note: comment your changes so you can easily replace them since changes to the core files will most likely be overwritten by future upgrades.

[quote name=‘tbirnseth’]You could change the fn.cart.php code to have it sort by “rate” after the rates are determined for the location.

Note: comment your changes so you can easily replace them since changes to the core files will most likely be overwritten by future upgrades.[/QUOTE]



Thank you, tbirnseth.



I found the solution for old version in here:

[url]http://forum.cs-cart.com/showthread.php?t=9320[/url]



However, I can’t locate same portion of code in the 2.x version.

Unfortunately, I am not a programmer …

PM me if you’d like to arrange for me to do this for you.

[quote name=‘tbirnseth’]PM me if you’d like to arrange for me to do this for you.[/QUOTE]



Thanks. Another members helped me out free of charge already. :wink:

could you share the solution?

Yes, can you please share the solution. This is a very logical setting for most customers.

johnbol1 told me to add:

[QUOTE]

function compare($x, $y) {

if ( $x[‘rates’] == $y[‘rates’] )

return 0;

else if ( $x[‘rates’] < $y[‘rates’] )

return -1;

else

return 1;

}

usort($shipping_rates, ‘compare’);

[/QUOTE]



BEHIND


[QUOTE]$cart[‘shipping_failed’] = true;[/QUOTE] in core/fncart.php



I tested it briefly, it works under 2.1.2. Don’t know how it react with other environment or conditions.

Thanks!

I would probably have used the hook they provided at the bottom of that function.

Look for the line like fn_set_hook(‘apply_cart_shipping_rates’,…)



If you used a hook, you would not have to worry about your changes being in conflict with a future upgrade.

[quote name=‘tbirnseth’]I would probably have used the hook they provided at the bottom of that function.

Look for the line like fn_set_hook(‘apply_cart_shipping_rates’,…)



If you used a hook, you would not have to worry about your changes being in conflict with a future upgrade.[/QUOTE]



Thanks for the suggestion, but I am not a programmer. Unsure how to do hook. Will spend time to look into it when time allows. Now, I need to manually resolve modified files for updating. Thanks!

This works great, but I have a “ship on your own account” option that defaults to $0.00. Now it shows free shipping as the cart default for everyone. Not good.



How could I make that code say “if it's not free shipping, then run this function” ?

The logic in the shipping modules is that if a “method” returns zero for the cost, then it is considered free shipping. If the “method” is not returned then that “method” is considered not-available.

Right, but the code snipped posted above still sorts the “method” with a cost of $0.00 as the default because it's the cheapest.



Customers see shipping: $0.00 in the cart and think, “hell yeah!, free shipping!” But when they get to step three in the checkout process they realize that it's not free; it's a method of “ship on your own account” and it requires them to fill in their personal or company shipping account details.



The cart sees it as free shipping because the cart doesn't handle the method except by passing their account details to the view-order screen. Our order-entry guys give the account details to the shipping guys and off it goes.



So my question is one of PHP. How do I make this:



function compare($x, $y) {
if ( $x['rates'] == $y['rates'] )
return 0;
else if ( $x['rates'] < $y['rates'] )
return -1;
else
return 1;
}
usort($shipping_rates, 'compare');




…ignore methods of $0.00 or put them at the end of the sort? I wish I knew more PHP.

is this code solution working in 4.x.x?

The following code is used to select first shipping method (app/functions/fn.cart.php)

foreach ($product_groups as $key_group => $group) {
                    if (!empty($group['shippings'])) {
                        $first_shipping = reset($group['shippings']);
                        $cart['chosen_shipping'][$key_group] = $first_shipping['shipping_id'];
                    }
                }

You can try to use the mentioned function in this part of code