...

View Full Version : adding Tax to the Total in cart



nrkar
08-12-2012, 06:26 AM
Hi, can somebody help me to add Tax @7.75% to the subtotal in my PHP Cart? And tax + subtotal = Grand Total

Here is the code;


// work out if we can start a session
$simpleshop_res = headers_sent($simpleshop_file,$simpleshop_line);
if($simpleshop_res){
// headers have been sent, display a message prompting the user to add session_start();
echo 'Please add <?php session_start(); ?> before line "'.$simpleshop_line.'" in file "'.$simpleshop_file.'" to use the shop correctly.';
exit;
}else{
session_start();
}
}

$cart_products = isset($_SESSION['_simpleshop_cart_products']) ? $_SESSION['_simpleshop_cart_products'] : array();
if(!is_array($cart_products)){
$cart_products=array();
}

if(isset($_REQUEST['simpleshop_action']) && $_REQUEST['simpleshop_action']){
$refresh = false;
// what are we doing?
switch($_REQUEST['simpleshop_action']){
case "add":
if(isset($_REQUEST['simpleshop_product_settings'])){
$product_settings = explode("|",$_REQUEST['simpleshop_product_settings']);
// hash to see if it's true. stops people modifying prices.
if($product_settings[1] == md5('my not so s3cret code heh'.$product_settings[0])){
$product_settings = unserialize(base64_decode($product_settings[0]));
}else{
$product_settings = false;
}
if($product_settings){
// which option have they selected? or use the default option 1 if none are selected
$product_option = (isset($_REQUEST['product_option'])) ? (int)$_REQUEST['product_option'] : 0;
if(!isset($product_settings['attr'])){
$product_settings['attr'] = array();
}
if(!isset($product_settings['attr'][$product_option]) && isset($product_settings['attr'][0])){
$product_option = 0;
}
$option_settings = $product_settings['attr'][$product_option];
if($option_settings && is_array($option_settings)){
// now we work out what values to add to the shopping cart session array.
// we want the name of the product
$product_name = $product_settings['name'];
// we want it's price, the price of the option that the user selected:
$price = $option_settings['price'];
// we see if there is a name of the option that the user selected.
if(isset($option_settings['name']) && $option_settings['name'] && $option_settings['name'] != $product_name){
$product_name .= ' - ' . $option_settings['name'];
}
// and work out the quantity ordered, with a defualt of 1.
$quantity = max(1, ((int)$_REQUEST['quantity']) ? (int)$_REQUEST['quantity'] : 1 );
$product_uri = (isset($_REQUEST['url']))?$_REQUEST['url']:'';
$new_product = array(
// values that are showed to the user in checkout:
"name"=>$product_name,
"price"=>$price,
"quantity"=>$quantity,
"product_uri"=>$product_uri,
"product_option"=>$product_option,
"product_settings"=>$product_settings,
);
// for now we just overwrite this product with a new one.
$cart_products [md5(serialize(array($product_option,$product_settings)))] = $new_product;
// refresh the page so it doesn't add the product twice on refresh.
$refresh = true;
}
}
}
break;
case "modify_quantities":
if(isset($_REQUEST['product_quantities'])){
$modify_product_id = $_REQUEST['product_quantities'];
if(is_array($modify_product_id)){
foreach($modify_product_id as $key => $val){
// key is the product id, val is the new quantity
if(!isset($cart_products[$key]))continue;
// quantities can only be integers:
$val = (int)$val;
// find the product we wish to modify
$modify_product = $cart_products[$key];
// check if it exists:
if(is_array($modify_product)){
// we are updating the quantities
if($val == 0){
// uset set the quantity to 0, remove from cart
unset($cart_products[$key]);
}else{
// update the quantity with the new value:
$modify_product['quantity'] = $val;
// load it back into the products array:
$cart_products[$key] = $modify_product;
}
}
}
}
$refresh=true;
}
break;
}
// the above actions may change the state of the shopping cart, so we load it back into the session for next time:
$_SESSION['_simpleshop_cart_products'] = $cart_products;
// reload the page with a flag to stop any possible redirect loops.
if($refresh && !isset($_REQUEST['simpleshop_redir'])){
if(isset($_REQUEST['simpleshop_checkout_go'])){
header("Location: "._SIMPLESHOP_BASE._SIMPLESHOP_URL . ( (strpos(_SIMPLESHOP_URL,'?')===false) ? '?' : '&' ) . "simpleshop_checkout");
}else{
header("Location: "._SIMPLESHOP_BASE._SIMPLESHOP_URL . ( (strpos(_SIMPLESHOP_URL,'?')===false) ? '?' : '&' ) . "simpleshop_redir=true");
}
exit;
}
}
$cart_sub_total = 0;
$cart_total = 0;
$cart_shipping = 0;
foreach($cart_products as $product_id => $product){
$quantity = max(1,(int)$product['quantity']);
$product_total = $product['price'] * $quantity;
$cart_sub_total += $product_total;
if(isset($product['product_settings']['shipping']) && $product['product_settings']['shipping']>0){
$cart_shipping += $product['product_settings']['shipping'];
}
}
$cart_total = $cart_sub_total + $cart_shipping;
$member = isset($_SESSION['_cart_member_data']) ? $_SESSION['_cart_member_data'] : array();
// quick header / footer hack:
function _simpleshop_header($admin=false){
if($admin){
?>

Len Whistler
08-12-2012, 07:16 AM
You should look into the math capabilities of MySQL instead of getting PHP to do the math. I believe it's faster to get MySQL to do all the work it can do then pass on the results to PHP.

You could then repost to the MySQL forum.


----

nrkar
08-12-2012, 11:28 AM
You should look into the math capabilities of MySQL instead of getting PHP to do the math. I believe it's faster to get MySQL to do all the work it can do then pass on the results to PHP.

You could then repost to the MySQL forum.


----

Thank you Len....But I didn't use MySql database server for my shopping cart.
I need a php simple calculation code so that I can add Tax @7.75% to the subtotal to get Grand Total = tax + subtotal..

Len Whistler
08-12-2012, 06:25 PM
The math for percentage is:


Tax = Subtotal * Tax Percentage / 100
Grand Total = Subtotal + Tax


----

nrkar
08-12-2012, 09:01 PM
The math for percentage is:


Tax = Subtotal * Tax Percentage / 100
Grand Total = Subtotal + Tax


----

Thank you Len..Does the above code work if I integrate into the existing cart code that I submitted? Or can you edit the my code and submit? Very thankful to you...

Len Whistler
08-12-2012, 09:44 PM
Below is a quick example of PHP math. It might need some work but it should give you an idea were to start.



<?php

$cart_sub_total=67.99;
$tax_percentage=7.5;

$tax_due=$cart_sub_total*$tax_percentage/100;
$tax_due = sprintf("%01.2f", $tax_due); // format results with trailing 0's.

$total=$cart_sub_total+$tax_due;

echo "Sub Total: \$$cart_sub_total<br>";
echo "Tax due: \$$tax_due<br>";
echo "Total: \$$total<br>";

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum