3 Replies - 1695 Views - Last Post: 28 May 2014 - 07:56 AM Rate Topic: -----

#1 chaoslord  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 27-May 14

Find the Quantity, Unit Price and Total Cost Shopping Cart Help

Posted 27 May 2014 - 08:36 PM

Hello I am having some difficulty with my shopping cart script. So far I have it so you can update how many quantities of the item you want and it also updates the price as well. I want it so that once you click a button that says "Add To Cart" it sends you to a page that displays your order and tells you the Quantity, Unit Price and Total Cost of it.

Below is the code I have so far.

Javascript:

<script type="text/javascript">  
   
$(document).ready(function() {  
   
                   
   
$("#Quantity").keypress(function(event) {  
   
return /\d/.test(String.fromCharCode(event.keyCode));  
   
                                                  });  
   
                   
   
$("#Quantity").change(function(event) {  
   
$(this).val(Math.abs($(this).val()));  
   
                     
   
UpdatePrice();  
   
});  
   
                   
   
$("#CartForm").submit(function(event) {  
   
                       
   
if(!isNormalInteger($("#Quantity").val()))  
   
{  
   
event.preventDefault();  
   
                       
   
alert("Sorry, you must select at least one of these DVD's!");  
   
           
   
return false  
   
}  
   
});  
   
                   
   
UpdatePrice();  
   
});  
   
   
   
function UpdatePrice()  
   
{  
   
var AdjustedPrice = parseFloat($("#BaseUnitPrice").val())*parseInt($("#Quantity").val());  
   
                               
   
$("#AdjustedPrice").html(AdjustedPrice.toFixed(2) + $("#Currency").val());  
   
                          }  
   
   
   
function ChangeQuantity(Value)  
   
{  
   
var NewQuantity = parseInt($("#Quantity").val()) + parseInt(Value);  
   
   
   
if(NewQuantity < 1)  
   
NewQuantity = 1;  
   
   
   
$("#Quantity").val(NewQuantity);  
   
   
   
UpdatePrice();  
   
}  
   
   
   
function isNormalInteger(str) {  
   
return /^\+?(0|[1-9]\d*)$/.test(str);  
   
}  
   
</script>



PHP:

echo'<div class="right"><img alt="Picture" src="dvdcovers/wolverine.jpg" height="100" width="100" /></div>';  
   
echo'<font color="white">';  
   
echo'<br />';  
   
echo'<b>Title:</b> The Wolverine';  
   
echo'<br />';  
   
echo'<b>Quantity:</b> $Quantity;';  
   
echo'<br />';  
   
echo'<b>Unit Price:</b>';  
   
echo'<br />';  
   
echo'<b>Total Cost:</b>';  
   
echo'</font>';  
   
     
   
echo'<br /><br />';  
   
echo'<h3 class="widget-title">Purchasing And Payment</h3>';  
   
echo'<form action="cart.php" method="post" id="CartForm">';  
   
     
   
echo'<b>Unit Price: </b><div id="AdjustedPrice"></div>';  
   
echo'<br />';  
   
echo'<input type="hidden" id="Currency" value=" AUD" />';  
   
   
   
echo'<input type="hidden" id="BaseUnitPrice" name="UnitPrice" value="29.99" />';  
   
   
   
echo'<input type="hidden" name="ProductCode" value="000" />';  
   
echo'<br />';  
   
echo'<input type="text" id="Quantity" name="Quantity" value="1" />';  
   
echo'<input type="button" value="-" onclick="ChangeQuantity(-1);" />';  
   
echo'<input type="button" value="+" onclick="ChangeQuantity(1);" />';  
   
echo'<br />';  
   
echo'<input type="Submit" value="Buy" />';  
   
echo'</form>'; 



Any help would be much appreciated. Thankyou.

Is This A Good Question/Topic? 1
  • +

Replies To: Find the Quantity, Unit Price and Total Cost Shopping Cart Help

#2 AGENT_P6  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 40
  • Joined: 05-May 14

Re: Find the Quantity, Unit Price and Total Cost Shopping Cart Help

Posted 27 May 2014 - 11:04 PM

View Postchaoslord, on 27 May 2014 - 08:36 PM, said:

once you click a button that says "Add To Cart" it sends you to a page...


Eww, please don't! Imagine if you were at the grocery store and every time you put something in your shopping cart, the manager forced you out of your aisle and dragged you to the front of the store just to inform you about the cost of the product and how it will affect your running total.
Even doing this in a new window is a bad user experience. Instead, consider using some kind of popup/hidden div javascript function, or perhaps even a fixed position status bar at the bottom of the window which updates onclick() with "add to cart" button.

However if you want to stick with your original plan and actually go to a different page altogether, I think you would need to look into window.location.search.
Was This Post Helpful? 0
  • +
  • -

#3 chaoslord  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 27-May 14

Re: Find the Quantity, Unit Price and Total Cost Shopping Cart Help

Posted 27 May 2014 - 11:17 PM

Thanks for that. I've taken a look and i'm still really confused about it.

Are you able to show me a code example? Or how i would incorporate it into my code. I'm sorry but I don't know where to start.
Was This Post Helpful? 0
  • +
  • -

#4 vitaliyTerziev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 09-March 13

Re: Find the Quantity, Unit Price and Total Cost Shopping Cart Help

Posted 28 May 2014 - 07:56 AM

View Postchaoslord, on 28 May 2014 - 09:17 AM, said:

Thanks for that. I've taken a look and i'm still really confused about it.

Are you able to show me a code example? Or how i would incorporate it into my code. I'm sorry but I don't know where to start.


Hi chaoslord have you consider a slightely different approach, first you can replace your javascript file with one product.php class and one main.php file where you need to put one simple HTML form with POST method or GET its not important, in the same file you can call your product class and do how many products are selected and total price.

Hope those simple guidelines will help you.

This post has been edited by vitaliyTerziev: 28 May 2014 - 07:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1