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.

Help with updateShippingOptionCost()

mbowzeylombowzeylo Member
in Help edited February 2012
Hi Everyone,

I am trying to modify my shipping category costs on checkout depending on the country and I have followed the instructions in http://wiki.foxycart.com/snippets/shipping/multiple_flat_rates.

My shipping logic is simple:

If the shipment is going to Canada then set the prices in each category to certain values.

If the shipment is going to USA then set the prices in each category to different values.

Here is my custom shipping logic code:
(When the country changes on the checkout form, the alerts work fine...but any items in the cart that belong to those categories do not get their shipping costs updated.)
function customShippingLogic() 
{
	/* BEGIN CUSTOM SHIPPING LOGIC */
	
	if (typeof(country_code) === "undefined") 
	{
		country_code = "";
	}
	new_country_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_country").val() : jQuery("#customer_country").val());
	if (country_code != new_country_code) // The shipping country has changed!
	{ 
		country_code = new_country_code;
		//removeCustomShippingContainer();  // This call will make sure that when it updates, it starts fresh.
		
		if (country_code == "CA") 
		{
			updateShippingOptionCost(1, 10.00);
			updateShippingOptionCost(2, 15.00);
			updateShippingOptionCost(3, 20.00);
			alert('Ship to Canada');
		} 
		if (country_code == "US")
		{
			updateShippingOptionCost(1, 20.00);
			updateShippingOptionCost(2, 25.00);
			updateShippingOptionCost(3, 30.00);
			alert('Ship to USA');
		}
	} 
	/* END CUSTOM SHIPPING LOGIC */
}

My question is this... how/and where do I set the "code" parameter for the updateShippingOptionCost(code, cost) function.

Where do I set codes 1,2,&3? In my product links? Are these the actual "Category Codes" from my FoxyCart categories? Is this a custom parameter I need to include in my product links?

I have tried including it in my product links.
<a href="https://mygreatsite.foxycart.com/cart?name=My Great Product
&price=55.00
&category=1
&code=1
&productID=285">
<img id="buyNowButton" src="/images/global/buyNowButton.jpg" />
</a>

The docs say this needs to be an Integer...but when I use FireBug to look at fc_jason.products.code the value is listed as a string.


I think my problem is simply that I am not accessing the code parameter correctly.

Any help is appreciated.

Unfortunately I cannot provide access to the live site as it's behind a private development area.
Tagged:
Comments
  • fc_adamfc_adam FoxyCart Team
    @mbowzeylo,

    So that code in the updateShippingOptionCost() function relates to the code you set for each shipping rate in the addShippingCost() function - essentially you need to add a shipping rate (with a code/id) before you can update it based on that code/id.

    With your script above, you're actually not adding any shipping costs.

    Another thing to clarify as well - these shipping rates don't relate per category unless your javascript above accounts for categories in its calculations.

    Does that make sense? To get shipping rates displaying in your script you'll need to add them first. Essentially you could do it like this:
    function customShippingLogic() 
    {
    	/* BEGIN CUSTOM SHIPPING LOGIC */
     
    	if (typeof(country_code) === "undefined") 
    	{
    		country_code = "";
    	}
    	new_country_code = (jQuery("#use_different_addresses").is(":checked") ? $("#shipping_country").val() : jQuery("#customer_country").val());
    	if (country_code != new_country_code) // The shipping country has changed!
    	{ 
    		country_code = new_country_code;
    		removeCustomShippingContainer();  // This call will make sure that when it updates, it starts fresh.
     
    		if (country_code == "CA") 
    		{
    			addShippingOption(1, 10.00, 'Shipping Rate 1');
    			addShippingOption(2, 15.00, 'Shipping Rate 2');
    			addShippingOption(3, 20.00, 'Shipping Rate 3');
    			alert('Ship to Canada');
    		} 
    		if (country_code == "US")
    		{
    			addShippingOption(1, 20.00, 'Shipping Rate 1');
    			addShippingOption(2, 25.00, 'Shipping Rate 2');
    			addShippingOption(3, 30.00, 'Shipping Rate 3');
    			alert('Ship to USA');
    		}
    	} 
    	/* END CUSTOM SHIPPING LOGIC */
    }
    
  • Thanks Adam,

    That makes sense. I'll experiment with that this week. Thanks for your reply.
  • mbowzeylombowzeylo Member
    edited March 2012
    Hey Adam,

    I finally got back to this job. I was able to get this working quickly now that I understand how your functions operate...I just have one small problem. Shipping cost gets calculated and updated on the checkout page, but the taxes do not update.

    I am aware that the taxes do get correctly recalculated once the City and Province are entered...but is there a way to get them to update when my customShippingLogic is finished running?


    Here is my custom logic for Canada...
    		if (country_code == "CA") 
    		{
    			// Find out how many items from each category are in the shopping cart
    			var quantity10x10 = 0;
    			var quantity14x14 = 0;
    			var quantity22x22 = 0;
    			
    			for (p in fc_json.products) 
    			{
    				if (fc_json.products[p].category == "10x10")
    				{
    					quantity10x10 += fc_json.products[p].quantity;
    				} 
    				else if (fc_json.products[p].category == "14x14")
    				{
    					quantity14x14 += fc_json.products[p].quantity;
    				}
    				else if (fc_json.products[p].category == "22x22")
    				{
    					quantity22x22 += fc_json.products[p].quantity;
    				}
    			}
    			
    			// Multiply the number of items in each category by the shipping price for the category
    			var shippingCost10x10 = quantity10x10 * 16.85;
    			var shippingCost14x14 = quantity14x14 * 17.65;
    			var shippingCost22x22 = quantity22x22 * 18.85;
    			
    			// Add up the total shipping costs for all products
    			var totalShippingCost = shippingCost10x10 + shippingCost14x14 + shippingCost22x22;
    			
    			// Create a new shipping option and select the radio button by default…finally update the shipping cost and disable the radio
    			addShippingOption(1, 0, 'Canada Post Shipping &amp; Handling');
    			jQuery('#shipping_service_1').attr('checked', true);
    			updateShippingOptionCost(1, totalShippingCost);
    			jQuery('#shipping_service_1').attr('disabled', 'disabled');
    		} 
    
    

    Any ideas? Appreciate your help!

    Mike
  • fc_adamfc_adam FoxyCart Team
    @mbowzeylo,

    The default functionality of the javascript function that updates taxes relies on the country, state and city fields are completed - both to cut down on duplicate requests and to load global, state and local taxes at the same time.

    The issue you're hitting actually looks to be a bug - in that the handling fee you've set in the admin isn't being applied on page load in the tax calculations - but it is in the javascript function. We've ticketed this and will look into it as soon as possible.

    In the interim, there are two temporary fixes you could implement:

    1) Set the two tax line items in the cart listing to be "TBD", which will be replaced by the correct tax calculations when the custom has filled in those three fields and taxes are updated.

    2) Create a duplicate function of the updateTaxes function from the checkout.js, remove the checks on those fields and have that run on page load after the multipleFlatRates.

    As your taxes don't rely on anything, but are global taxes, the second option might be the best bet. If you paste the following right before the closing </head> tag of your checkout template you should be good to go: http://pastie.org/private/qfvjwh7l6cp4e4o9xypl4g

Sign In or Register to comment.