Setting the shipping total through product attributes

This snippet has been replaced. In place of the following snippet, we now have a custom shipping code feature in FoxyCart 2.0 that allows you to modify returned live rates and provide custom rates to your customers without needing to add extra javascript to change the way the checkout works. Please use the new custom shipping code functionality instead of this snippet. Review this page for notes on migrating to the new functionality. This page will remain for reference.

In cases where each individual product in your store has a set price that it requires to be shipped. This snippet allows you to set that up.

This step comes in after you've setup your CMS or management system as required if you have one. Essentially for this script you can go as basic as a numeric field called shipping for the product. A requirement for this approach is also that each product must have a code set as well. This is used to make a unique session attribute for the product.

In your add to cart form, add a session attribute for the product shipping that looks like this:

<input type="hidden" name="h:PRODUCTCODE_shipping" value="SHIPPINGVALUE" />

Or in your add to cart link like this:

...&name=Product&price=20&h:PRODUCTCODE_shipping=SHIPPINGVALUE

In these examples, PRODUCTCODE would be replaced by that specific products code, and SHIPPINGVALUE is the cost to ship the product. As a complete example, a product add to cart form could look like this:

<form>
<input type="hidden" name="name" value="My Product" />
<input type="hidden" name="price" value="20" />
<input type="hidden" name="code" value="abc123" />
<input type="hidden" name="h:abc123_shipping" value="5" />
<input type="submit" value="Submit" />
</form>

Step 2: Setup flat rate modification snippet

In order for you to be able to set the shipping with custom flat rate amounts, you need to make use of a second snippet - the flat rate shipping modification snippet. Follow the steps on that page.

Step 3: Custom logic

If all of your products have an individual flat rate amount set - the custom shipping logic required in step 4 of the flat rate shipping snippet would look like this:

var shippingCost = 0;
for (var p = 0; p < FC.json.items.length; p++) {
  if (FC.json.custom_fields.hasOwnProperty(FC.json.items[p].code+"_shipping")) {
    shippingCost += (FC.json.custom_fields[FC.json.items[p].code+"_shipping"].value * FC.json.items[p].quantity);
  } else {
    // No shipping attribute set for this product
  }
}
 
FC.customFlatRates.add(1, shippingCost, '', 'Standard Shipping');

Step 4: Customise

The code above could also be combined with other javascript to create more complex solutions. For example:

Shipping tiers or grouping

You could quite easily add tiers to the shipping attribute similar to how the coupon tiers are specified, and based on the quantity ordered per product, get the relevant tier in the javascript and charge accordingly. For example, the tiers could look like 1:10|5:12|10:15 which would say 1-4 products would be $10, 5-9 $12, 10+ would be $15.

You could also expand your management system to have group prices based on a FoxyCart category and in your add to cart form creation check to see if there is a flat rate for the products category or not, and use that instead if required.

Site Tools