Table of Contents
Flat rate shipping calculated by product shipping attributes via add to cart
Using version 2.0? There is a new snippet available for our latest version, available from here.
When using a CMS to manage products, it can sometimes be logical to define a shipping value per product within the CMS system. This would then allow store manages to update product shipping pricing alongside of the actual products.
Step 1: Update Categories
Update all categories to 'Shipped using a flat rate fee' with a value of 0 in the 'Product Delivery Option' section.
Step 2: Setup product links or forms
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:
</head> tag of your cart template to add shipping charges to the cart table:
The script above loops through the products and calculates all of the shipping charges included for the added products. If any of the products doesn't have shipping charges specified, or if any other specific requirements are/aren't met, you can set a
canShip variable to false and perform additional tasks like setting the total shipping value to “TBA” or whatever other logic you want to do. Otherwise it adds to total shipping for each product multiplied by its quantity to the total shipping table row, and also adds it to the cart total cost as well.
Step 5: Customise
This approach is fairly simplified, but it wouldn't take much to make this script much more powerful and robust. For example, you could specify other parameters in the product shipping attribute which triggers other functionality. Such as:
"We'll contact you for shipping"
If the shipping variable is set to “contact” rather than a number, set the
canShip variable to false, and on the checkout hide live payment options and show the purchase order option, using this 'hiding the purchase order payment method' snippet to change it to match this purpose.
Shipping tiers or grouping
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.