The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

Custom Flat Rate for different countries

BLJBLJ Member
in Help edited October 2013
Hi,

I'm trying to implement a custom flat shipping rate which changes for international shipping and is calculated per item.

For example:
UK shipping for one item = £2.50
International shipping for one item = £4.00
UK shipping for two items = £5.00
International shipping for two items = £8.00

I've used the following code, but unfortunately it does not calculate per item... i.e. :
UK shipping for one item = £2.50

International shipping for one item = £4.00
UK shipping for two items = £2.50
International shipping for two items = £4.00

Help and tips much appreciated:

<script type="text/javascript" charset="utf-8">
FC.checkout.config.customShipping = {
onLoad: true, // Set to false if you don't want shipping calculated when the checkout loads
onLocationChange: true // Set to true if your shipping logic relies on updating whenever the shipping location for the order changes
};

function calculateShipping() {
var shippingCost = 0;

/* BEGIN CUSTOM SHIPPING LOGIC */

var country_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_country").val() : $("#customer_country").val());

switch (country_code) {
case "GB":
shippingCost = 2.50;
break;
default:
shippingCost = 4.00;
}

/* END CUSTOM SHIPPING LOGIC */

FC.checkout.config.orderFlatRateShipping = shippingCost;
FC.checkout.updateShipping(-1);
}

jQuery(document).ready(function() {
if (FC.checkout.config.customShipping.onLoad) {
runShippingLogic();
}

if (FC.checkout.config.customShipping.onLocationChange) {
FC.checkout.overload("updateTaxes", function() { runShippingLogic(); }, null);
}
});

function runShippingLogic() {
// Check to see if there are actually shippable products in the current cart before running the custom shipping (0.7.1+ only), or just run it for older carts
if ((typeof(FC.checkout.config.hasShippableProducts) === "boolean" && FC.checkout.config.hasShippableProducts) || typeof(FC.checkout.config.hasShippableProducts) === "undefined") {
calculateShipping();
}
}
</script>
Comments
  • winstonwinston FoxyCart Team
    @blj

    Are you wanting the shipping to keep going up by 2.50 for each item in the UK, and 4.00 for outside the UK? Just want to clarify before I update the logic.

    The snippet you're using just sets the shipping on the cart without concern for how many products are in the cart, so there needs to be more logic in the JS for shipping increases.
  • BLJBLJ Member
    @winston

    That's absolutely correct, big thanks for taking the time to look at this.
  • winstonwinston FoxyCart Team
    @BLJ

    Sorry for the delay in getting back to you. I think this should take care of it:

    https://gist.github.com/wnstn/6801874

    I'm just taking the amount of products in the cart and then multiplying the shipping price against that count. Let me know if that behaves as desired!
  • BLJBLJ Member
    @winston

    Awesome, that's fantastic, works perfectly
Sign In or Register to comment.