?
Solved

Javascript for each certain amount, increase variable

Posted on 2013-06-17
7
Medium Priority
?
228 Views
Last Modified: 2013-06-20
Hi there,

I am needing to calculate shipping in a website I am writing. Each item we sell has a maxshipping variable which shows how many of that item can be shipped before the items shipping cost is increased.

E.g.
An item has a shipping cost of $0.60 for up to 10 of the items... After that 10 I want to increase it by another $0.60... then so forth for each 10 items.

How do I go about doing this?

Thanks,
Jessee
0
Comment
Question by:Letsgetcoding
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Expert Comment

by:Insoftservice
ID: 39255376
so you mean to say for 10 items its $0.60
now if it have only one item it would be also $0.60 right ?
please confirm.

If yes please copy below code and save it as "test.html".

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title>love to marry </title>
  <meta name="Generator" content="Lorem ipsum dolor sit amet,">
  <meta name="Author" content=" consectetur.adipisicing,">
  <meta name="Keywords" content="sed do eiusmod tempor incididunt">
  <meta name="Description" content="ut labore et dolore magna aliqua.">
  <script type="text/javascript">

  function bridedress()
  {
      var item_no =25;
      var cost = 0.60;
      var quantity =10;
      var extra = 0;
      var item = 0;
      var totalprice =0 ;
      var extraitem =0 ;

      extraitem = (item_no%quantity);
      item = Math.floor(item_no/quantity);
      if(extraitem < quantity)
      {
         extra = 1;// to keep additional item less than 10 as 1 unit or quantity
      }
      totalprice = (item*cost)+(extra*cost);
      alert(totalprice)
      alert(item)
      alert(extra*cost)


  }

  </script>
 </head>

 <body>
  <a href="javascript:bridedress()">lovetomarry</a>
 </body>
</html>

(Edit: Possible advertising removed - Modulus_Twelve)
0
 

Author Comment

by:Letsgetcoding
ID: 39255432
Hi there,

Yes.
1 - 10 items = 0.60
11 - 20 items = 1.20 etc etc...

Will try this code and get back to you.

Thanks,
Jessee
0
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 total points
ID: 39255459
Here is a working sample I made up  using jquery http://jsbin.com/ogilub/1/edit

You can take this as a base.  In short, you get the qty and divide by 10 and round up using Math.Ceil.  This way a qty of 11  gets multiplied by 2 *.60
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil

HTML
<lable><input id="qty" value="0">Qty</lable>
  <lable><input id="shipping" value="0">Shipping</lable>

Open in new window

JS
var qty=0;
var ship_cost=0;

$('#qty').change(function(){
  qty=$('#qty').val();
  ship_cost= (Math.ceil((qty/10)))*0.6;
  //console.log(ship_cost);
  $('#shipping').val(ship_cost);
  
});

Open in new window

0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:Letsgetcoding
ID: 39258424
Hey there,

The maxshipping changes with each item. Sometimes only 2 items can be sent for 0.60... other times 10 items can be. How do I include that?

Thanks,
Jessee
0
 
LVL 53

Expert Comment

by:Scott Fell, EE MVE
ID: 39258451
Did you try it?  If you add 1 through 10 in the qty box, it will be 60 cents. If you use 11 through 20 it will be 1.20.
0
 

Author Closing Comment

by:Letsgetcoding
ID: 39258559
Works like a charm. Just needed to put the maxshipping in where the /10 was.

Thank you!
0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 39261697
Hi @Letsgetcoding
For 25 it has to 1.80 right?
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question