Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shipping by Weight #72

Closed
mrtom100 opened this issue Jan 24, 2011 · 30 comments
Closed

Shipping by Weight #72

mrtom100 opened this issue Jan 24, 2011 · 30 comments
Labels

Comments

@mrtom100
Copy link

Hello
I have been trying to get the option of having a weight per product then making a price range for postage 0 to 100 = £3.90 101 to 200 = £5.90

The trouble is i can not get it to work i have little knowledge of javascript need help please

@Andre-Agi
Copy link

I'm also in need of this!

@Andre-Agi
Copy link

Anyone managed to sort this out?

@glenpike
Copy link

Here is a quick-dirty-hack I made to get shipping by weight working for SimpleCart

It replaces the existing shipping function with my own, that gets the total weight of all products by checking if it has a "weight" field and adding this to the total.

Then a function called "getDeliveryCost" is called - mine looks through an array of weights and costs and rounds the delivery cost up to the next weight limit.

Help yourselves.

var delivery_cols = ["Weight Limit", "Price"];
var delivery = [["10", "10"], ["20", "15"], ["40", "20"], ["60", "30"], ["70", "40"], ["100", "50"]];
var debug = false;

function getDeliveryCost(calculated_weight) {
    if(0.0 == calculated_weight) {
        return 0.0;
    }
    var i = 0;
    while(calculated_weight > parseFloat(delivery[i][0]) && i < delivery.length) {
        i++;
    }

    var calculated_delivery = parseFloat(delivery[i][1]);
    if(debug) {
        alert("Delivery cost " + calculated_delivery + " for weight " + calculated_weight + " limit is " + delivery[i][0] + " i is " + i);
    }
    return calculated_delivery;
}

simpleCart.shippingByWeight = true;
simpleCart.shipping_old=simpleCart.shipping;
simpleCart.shipping=function() {
        if(true != simpleCart.shippingByWeight) {
            return simpleCart.shipping_old();
        }
        if(debug) {
            alert("shipping!");
        }
        if( parseInt(simpleCart.quantity,10)===0 )
            return 0;
        var shipping =  parseFloat(simpleCart.shippingFlatRate) + 
                        parseFloat(simpleCart.shippingTotalRate)*parseFloat(simpleCart.total) +
                        parseFloat(simpleCart.shippingQuantityRate)*parseInt(simpleCart.quantity,10),
            nextItem,
            next;
        weight = 0.0;
        for(next in simpleCart.items){
            nextItem = simpleCart.items[next];
            if(nextItem.weight) {
                if(debug) {
                    alert("have weight " + nextItem.weight);
                }
                weight += parseFloat(nextItem.weight);
            } else if( nextItem.shipping ){
                if( typeof nextItem.shipping == 'function' ){
                    shipping += parseFloat(nextItem.shipping());
                } else {
                    shipping += parseFloat(nextItem.shipping);
                }
            }
        }
        if(0.0 != weight) {
            shipping = getDeliveryCost(weight);
        }

        return shipping;
}

@giandy13b
Copy link

glenpike your the man!!! Thanks alot this helps me out!!

@ememem
Copy link

ememem commented Sep 28, 2011

Hi,

Ok I’m a novice at js but slowly (very slowly) learning. I’m having trouble trying to figure out what I need to put in my html page ie

<div class="simpleCart_shelfItem">
<h2 class="item_name">Awesome T-Shirt</h2>
<span class="item_price">$15.00</span>
<input type="text" class="item_quantity" value="1" />
<!-- field for item shipping cost -->
<input type="hidden" class="item_shipping" weight="5" name="getDeliveryCost" />
<a href="javascript:;" class="item_add" >Add to Cart</a>
</div>

I know - I’m sure I’m way off but if you have a minute to help out it would be much appreciated.
Cheers
EM

@glenpike
Copy link

Hi,

If you use the script above then add in your input field like this.

<input type="hidden" class="item_weight" id="item_weight" name="item_weight" value="0"/>

The getDeliveryCost is a function I wrote to use some variables I created to return a price based on the weight. This is shown on the Cart itself using the "shipping cost" element:

<span class="simpleCart_shippingCost"></span>

The cart script calculates this and my JavaScript above merely overrides the SimpleCart calculation if you set the variable:

simpleCart.shippingByWeight = true;

in your initialisation of the cart at the bottom of your HTML page. You should not have to change any SimpleCart scripts, just add the JavaScript above after you load the cart script.

Have a look at:http://www.floating-shelves.com/beech-floating-shelves.html for an example - the "calculator" on each page fills in the cost and weight of the items, so when you add it to the cart, it refreshes the cart and calculates the shipping price.
All my functions that override the SimpleCart are in: http://www.floating-shelves.com/js/funcs.js
There are some other ones in there, but search for getShippingCost and you will see.

Hope this helps.

Glen

@ememem
Copy link

ememem commented Sep 28, 2011

That's awesome Glen! Thank you so much for gettting back to me so soon and helping out.
Muchly appreciated!
Cheers
EM

@giandy13b
Copy link

You know a problem I found with this code is when I add 2 different items of the same weight, it added a larger shipping amount as opposed to two of the same items...

Wouldn't it be easier to just have a dollar amount that multiplies by the weight?

@glenpike
Copy link

The company I wrote the system for had a tier based shipping arrangement, e.g. 0-10kg = £10, 10.1-20kg =£20, etc.
This system works fine for that, if you add 2 items the same weight, the whole order is heavier right?

It is also possible there is a bug in the code - I will check it later to see...

@ememem
Copy link

ememem commented Sep 30, 2011

I suppose it's going to start getting tricky if you want to add GST on the shipping as well?

@glenpike
Copy link

Not sure about GST - I am guessing that is some tax like British VAT? All the prices on the website I worked for include the VAT, so the figures for shipping included that - not sure if that helps you, I guess if you are selling to commercial customers, you have to quote the tax added?

Maybe just do a sum in the shopping cart that calculates the GST of the total?

@giandy13b
Copy link

Hey Glen, I got it working but for some reason on my page it's not calculating the total weight quantity for the same item.
Example: 3 of the same shirts in my cart is still giving me the total of just one..
But it's working for adding new items, but only for one of each.
Example is here on my page:
http://www.ugh-fu.com/salestest.html
http://www.ugh-fu.com/carttest.html

Could you help me?

Thanks,

@glenpike
Copy link

Looks like you are not multiplying the weight of each item by the quantity of items in the cart:

You need to replace:

weight += parseFloat(nextItem.weight);

with:

weight += parseFloat((nextItem.weight * nextItem.quantity));

e.g.

for(next in simpleCart.items){
nextItem = simpleCart.items[next];
if(nextItem.weight) {
if(debug) {
alert("have weight " + nextItem.weight);
}
weight += parseFloat((nextItem.weight * nextItem.quantity));
} else if( nextItem.shipping ){
if( typeof nextItem.shipping == 'function' ){
shipping += parseFloat(nextItem.shipping());
} else {
shipping += parseFloat(nextItem.shipping);
}
}
}

@toniremi
Copy link

Hi to everybody!

I am just a novice in simplecart and javascript , so please be patience :)

Im using the simple cart in a web that distributes belts and i am using the script that Glen post here. Here my doubt or problem.

When i show the cart without add any item the shipping and the total appears with the value 0 . How can i fix this?

Code:

me.updateTotals = function() {

    me.total = 0 ;
    me.quantity  = 0;
    me.each(function(item){ 

        if( item.quantity < 1 ){
            item.remove();
        } else if( item.quantity !== null && item.quantity !== "undefined" ){
            me.quantity = parseInt(me.quantity,10) + parseInt(item.quantity,10);
        }
        if( item.price ){
            me.total = parseFloat(me.total) + parseInt(item.quantity,10)*parseFloat(item.price);
        }

    });

    me.shippingCost = me.shipping();
    me.taxCost = parseFloat(me.total)*me.taxRate;
    me.finalTotal = me.shippingCost + me.taxCost + me.total;
};

me.shipping = function(){
    var delivery_cols = ["Weight Limit", "Price"];
    var delivery = [["10", "10"], ["20", "15"], ["40", "20"], ["60", "30"], ["70", "40"], ["100", "50"]];
    var debug = true;

    function getDeliveryCost(calculated_weight) {
        if(0.0 == calculated_weight) {
            return 0.0;
        }
        var i = 0;
        while(calculated_weight > parseFloat(delivery[i][0]) && i < delivery.length) {
            i++;
        }

        var calculated_delivery = parseFloat(delivery[i][1]);
        if(debug) {
            alert("Delivery cost " + calculated_delivery + " for weight " + calculated_weight + " limit is " + delivery[i][0] + " i is " + i);
        }
        return calculated_delivery;
    };

    simpleCart.shippingByWeight = true;
    simpleCart.shipping_old=simpleCart.shipping;
    simpleCart.shipping=function() {
            if(true != simpleCart.shippingByWeight) {
                return simpleCart.shipping_old();
            }
            if(debug) {
                alert("shipping!");
            }
            if( parseInt(simpleCart.quantity,10)===0 )
                return 0;
            var shipping =  parseFloat(simpleCart.shippingFlatRate) + 
                            parseFloat(simpleCart.shippingTotalRate)*parseFloat(simpleCart.total) +
                            parseFloat(simpleCart.shippingQuantityRate)*parseInt(simpleCart.quantity,10),
                nextItem,
                next;
            weight = 0.0;
            for(next in simpleCart.items){
                nextItem = simpleCart.items[next];
                if(nextItem.weight) {
                    if(debug) {
                        alert("have weight " + nextItem.weight);
                    }
                    weight += parseFloat((nextItem.weight * nextItem.quantity));
                } else if( nextItem.shipping ){
                    if( typeof nextItem.shipping == 'function' ){
                        shipping += parseFloat(nextItem.shipping());
                    } else {
                        shipping += parseFloat(nextItem.shipping);
                    }
                }
            }
            if(0.0 != weight) {
                shipping = getDeliveryCost(weight);
            }

            return shipping;
    };
}

When there's no wieght the me.shipping returns 0 and the me.update does that the me.finalcost and me.shippingCost appears with the 0 value or this is that i think :S

So anybody can help me with some idea?

Thanks you!

if you need it I can put the url of the web so you can see what I mean!

@glenpike
Copy link

unrealsense - is it a problem that your cart is showing 0 when there is nothing in it?

With http://www.floating-shelves.com/ if there is nothing in the cart, the totals and shipping show as 0 - for me this is correct.

If you want your cart to change what it displays when there is nothing in it, you will have to have some extra javascript to show / hide the totals.

The easiest way to do this would be in your update totals function add some script to set the visibility of the HTML for the total / shipping cost - it it == 0 hide the HTML, if not show it.

I may have misunderstood your question, so sorry if I have.

Glen

@toniremi
Copy link

Hi Glen ,

First at all thanks you for responding soo quickly :) . I think that you have misunderstood me , im sorry my english is not so good enough. I try to explain it again , this time i hope to do it better jeje

I have made with php a little if to show in the same php page the product information (price , description , name , product id) or the cart with the products that you have added.

the problem come when i want to check only the cart (without adding any product) when i do this the shipping and total appears with 0. If i go now to the form and i add the second product to the cart the shipping and total reappear with the correct value but already if i try to check only the cart without adding products shipping and total appears with the 0 value already.

I think that its a problem of the code that i posted of the update function of the simplecart.

I have fixed temporaly with a "dirty hack" when i load the page with the cart i just add a product with all values at 0 less the wight that have a value 0.01. With this i can check the cart separetly without adding any product.

the dirty hack : onload="simpleCart.add( 'name=cart loader' , 'price=00.0' , 'quantity=0' , 'wight=0.01' );"

To show separetly the product information with the cart and only the cart i use a if ( all the products came with a unique product id) if i have a product id i show the description , etc but if dont i show only the cart with the total and the number of items added.

In the script of the other post maybe i think that i have to check the

me.shippingCost = me.shipping(); 
me.taxCost = parseFloat(me.total)*me.taxRate;
me.finalTotal = me.shippingCost + me.taxCost + me.total;

but really i dont know how to solve it without the dirty hack :S

If you need to see the code of the page i can sent you the url of both .php :D

Thanks you, realy!

@Lilli73
Copy link

Lilli73 commented Jan 31, 2012

Hello, I'm Italian and Lillina. To expose my questions, I will use the Google translator. Sorry, then, for some wrong word.
I need a function to SIMPLECART to calculate shipping costs.
The ecommerce site sells bottles of various weights.
Shipping costs are based on the weight of the package.
These rates:
Up to 3 kg EURos 19
From 3,100 kg to 10 kg EURos 23
From 10,100 kg to 20 kg Euros 24
from 20,100 kg to 30 kg euros 26
from 30,100 kg to 50 kg euros 32
from 50,100 kg to 100 kg to € 39 euros
over 100 kg add 50 kg add 39+19 euros
over 500 kg add 100 kg add 39+29 euros

Can you help? I set this function?
Thank you Lady!

@glenpike
Copy link

glenpike commented Feb 7, 2012

Hi Lillina,

If you want to calculate shipping cost by weight, you can use the same code as I posted above on May 15, 2011. You need to change the "delivery" array variable to your weights and prices - I have filled in the first 3 values from your rates in the code below:

var delivery_cols = ["Weight Limit", "Price"];
var delivery = [["3", "19"], ["10", "23"], ["20", "24"]];

So the first variable in each delivery array is the maximum weight limit for the price which is the second variable. For your values over 100kg, you will need to add in each 50kg jump, e.g. 151kg = 39+19?, 201=39+19+19 and same for values over 500kg

Hopefully if you paste the code I created into your site, it will override the SimpleCartJS shipping calculation. In your website you will need to add a weight to each product using something like the following:

You also need to have set simpleCart.shippingByWeight to true - this is done in the script I posted.

Hope this helps.

Glen

@bluetidepro
Copy link

Does this work with the current version 3.0.5? I can't seem to get it to work...

@devasia2112
Copy link

Does not work in the last version of simplecartjs.

@leoghitolox
Copy link

hy glen,,, i need your help...
can you send me the script that is modified with weight...

thanks...

email: leoghitolox99@gmail.com

@glenpike
Copy link

glenpike commented Mar 1, 2017

Hi - I've not used SimpleCart for a long time, so I don't know if my hack works for the latest version. Sorry!

@leoghitolox
Copy link

leoghitolox commented Mar 1, 2017 via email

@leoghitolox
Copy link

leoghitolox commented Mar 1, 2017 via email

@glenpike
Copy link

glenpike commented Mar 2, 2017

@leoghitolox - see comment #72 (comment) above. You need to add a hidden field with the weight value. As I said, the script may not still work with the current version, I haven't tried it.

@leoghitolox
Copy link

leoghitolox commented Mar 2, 2017 via email

@ghousahmed
Copy link

Hello , I wish to create a parse cloud server function that estimate weight clothes from database type

You will add an object that Name ClotheType

ClotheType

string : type
int:basePrice

This function must naming : getClotheEstimation(type, weight) ==> price

This function must read the type into database and
return weight multiplcate by the type weight of clothes.

  • You must create a simple script that save ClotheType list object

Type | basePrice
------------ |
Pantalon 10
Veste 10
Costume 10
Pull 10
Gillet 10
Polo / T-Shirt Court 5
Polo / T-Shirt Long 5
Short 7
Jupe 10
Robe 10
Chemisier / Chemise 10
Accessoires 2
Cravate 2
Nœud Papillon 10
Foulard / Echarpe 10
Chaussure de ville 20
Basket 20
Sous-Corps 10
Lingerie 10

@ghousahmed
Copy link

help @glenpike

@glenpike
Copy link

glenpike commented Sep 7, 2017

@ghousshah something like this?

https://jsfiddle.net/glenpike/sfg6k9x1/2/

@ghousahmed
Copy link

its work for a parse server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests