Solved

Javascript for each certain amount, increase variable

Posted on 2013-06-17
7
225 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
  • 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 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 52

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
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…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

861 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