oMama Posted April 17, 2007 Share Posted April 17, 2007 Newbie to PHP, trying to hack an existing shopping cart which uses the following to compute the order total: "amount" => round( $db->f("order_subtotal")+$tax_total-$discount_total, 2), Since this is the only computation being used, I am assuming the "ROUND" function is causeing the computation to be rounded up. So, if an order total for example is $18.50, the user is sent over to paypal and the pre-filled paypal charge is turned into $19.00 I can't seem to find an alternative to "ROUND" I tried "ABS" but it just sent an empty total over to paypal. Any *fast* help wouldbe really appreciated! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/ Share on other sites More sharing options...
btherl Posted April 17, 2007 Share Posted April 17, 2007 A standard technique is $price = 18.50112; # Too many digits! $price = round($price * 100.0) / 100.0; The idea is 18.50112 * 100 = 1850.112 round(1850.112) = 1850 1850 / 100 = 18.50 Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/#findComment-230979 Share on other sites More sharing options...
oMama Posted April 17, 2007 Author Share Posted April 17, 2007 Thank you for the fast reply. I hope you can walk me through making the change? I am using an opensource cart, and am at a loss for how to solve this without causing errors elsewhere. Can you give me the exact changes you would make to fix this problem? I have included the full code here: <?php $url = "https://www.paypal.com/cgi-bin/webscr"; $tax_total = $db->f("order_tax") + $db->f("order_shipping_tax"); $discount_total = $db->f("coupon_discount") + $db->f("order_discount"); $post_variables = Array( "cmd" => "_xclick", "business" => PAYPAL_EMAIL, "receiver_email" => PAYPAL_EMAIL, "item_name" => $VM_LANG->_PHPSHOP_ORDER_PRINT_PO_NUMBER.": ". $db->f("order_id"), "order_id" => $db->f("order_id"), "invoice" => $db->f("order_number"), "amount" => round( $db->f("order_subtotal")+$tax_total-$discount_total, 2), "shipping" => sprintf("%.2f", $db->f("order_shipping")), "currency_code" => $_SESSION['vendor_currency'],"first_name" => $dbbt->f('first_name'), "last_name" => $dbbt->f('last_name'), "address_street" => $dbbt->f('address_1'), "address_zip" => $dbbt->f('zip'), "address_city" => $dbbt->f('city'), "address_state" => $dbbt->f('state'), "address_country" => $dbbt->f('country'), "image_url" => $vendor_image_url, "return" => SECUREURL ."index.php?option=com_virtuemart&page=checkout.result&order_id=".$db->f("order_id"), "notify_url" => SECUREURL ."administrator/components/com_virtuemart/notify.php", "cancel_return" => SECUREURL ."index.php", "undefined_quantity" => "0", "test_ipn" => PAYPAL_DEBUG, "pal" => "xxxxxxxx", "no_shipping" => "1", "no_note" => "1" ); if( $page == "checkout.thankyou" ) { $query_string = "?"; foreach( $post_variables as $name => $value ) { $query_string .= $name. "=" . urlencode($value) ."&"; } mosRedirect( $url . $query_string ); } else { echo '<form action="'.$url.'" method="post" target="_blank">'; echo '<input type="image" name="submit" src="http://images.paypal.com/images/x-click-but6.gif" border="0" alt="Make payments with PayPal, it is fast, free, and secure!" />'; foreach( $post_variables as $name => $value ) { echo '<input type="hidden" name="'.$name.'" value="'.$value.'" />'; } echo '</form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/#findComment-230987 Share on other sites More sharing options...
Psycho Posted April 17, 2007 Share Posted April 17, 2007 There may be some other issue, because the round() function in your current code is using the optional precision operator of 2 which would round the number to the nearest hundredths (i.e. 2 decimal places). Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/#findComment-231004 Share on other sites More sharing options...
oMama Posted April 17, 2007 Author Share Posted April 17, 2007 I am at a total loss. This is the entire piece of code that is used in this module. Is there nothing that can be done? I appreciate your time, greatly. Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/#findComment-231006 Share on other sites More sharing options...
oMama Posted April 17, 2007 Author Share Posted April 17, 2007 If there is a way to make this work, I am open to completely re-write this (with help from a guru;)) I am pretty much stuck at this point. Thanks for any assistance. Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/#findComment-231042 Share on other sites More sharing options...
oMama Posted April 17, 2007 Author Share Posted April 17, 2007 Still hoping for some help with this one. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/#findComment-231323 Share on other sites More sharing options...
boo_lolly Posted April 17, 2007 Share Posted April 17, 2007 look into number_format() Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/#findComment-231337 Share on other sites More sharing options...
oMama Posted April 17, 2007 Author Share Posted April 17, 2007 Thank you... your advice led me here. I wonder if this could be the culprit? I am new to PHP, sorry if this is basic. Please leet me know if this should be changed in some way to change the variable being used in the other page of code. Thank you so much!! /* Product PRICE */ $my_taxrate = $ps_product->get_product_taxrate($cart[$i]["product_id"], $weight_subtotal); $tax = $my_taxrate * 100; $price = $ps_product->get_adjusted_attribute_price($cart[$i]["product_id"], $cart[$i]["description"]); if( $auth["show_price_including_tax"] == 1 ) { $product_price = $price["product_price"] * ($my_taxrate+1); } else { $product_price = $price["product_price"]; } $product_price = round( $product_price, 2 ); $product_rows[$i]['product_price'] = $CURRENCY_DISPLAY->getFullValue($product_price); /* Quantity Box */ $product_rows[$i]['quantity_box'] = "<input type=\"text\" title=\"". $VM_LANG->_PHPSHOP_CART_UPDATE ."\" class=\"inputbox\" size=\"4\" maxlength=\"4\" name=\"quantity\" value=\"".$cart[$i]["quantity"]."\" />"; /* SUBTOTAL CALCULATION */ $subtotal = $product_price * $cart[$i]["quantity"]; $total += $subtotal; $product_rows[$i]['subtotal'] = $CURRENCY_DISPLAY->getFullValue($subtotal); $product_rows[$i]['subtotal_with_tax'] = $CURRENCY_DISPLAY->getFullValue($subtotal * ($my_taxrate+1)); if (!empty($my_taxrate) && MULTIPLE_TAXRATES_ENABLE=='1') { if( $auth["show_price_including_tax"] == 1 ) { eval( "\$message = \"".$VM_LANG->_PHPSHOP_INCLUDING_TAX."\";" ); $product_rows[$i]['subtotal'] .= " ".$message; } else { $product_rows[$i]['subtotal'] .= " (+ $tax% ".$VM_LANG->_PHPSHOP_CART_TAX.")"; } } Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/#findComment-231360 Share on other sites More sharing options...
boo_lolly Posted April 17, 2007 Share Posted April 17, 2007 it could be that, and every method that block of code calls for, as well. i think you're getting warmer. Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/#findComment-231371 Share on other sites More sharing options...
Psycho Posted April 17, 2007 Share Posted April 17, 2007 Well, the code you jsut posted also uses round with the precision of 2. I think you need to just do some old-fashioned debugging. Start with your fist script above. The amount is comprised of three separate values: order subtotal, tax total, and discount total. The last two are composed of two separate values each. So, I would start by printing all of the individual values to my page and then show the summed values that the code is generating. So, at the bottom of the code you first posted, comment out the mosRedirect line and add this code after the very last closing curly bracket: $discount_total = $db->f("coupon_discount") + $db->f("order_discount"); <?php echo 'Order subtotal: '.$db->f("order_subtotal").'<br><br>'; echo 'Order tax: '.$db->f("order_tax").'<br>'; echo 'Order shipping tax: '.$db->f("order_shipping_tax").'<br>'; echo 'Tax Total: '.$tax_total.'<br><br>'; echo 'Coupon Discount: '.$db->f("coupon_discount").'<br>'; echo 'Order Discount: '.$db->f("order_discount").'<br>'; echo 'Discount Total: '.$discount_total.'<br><br>'; echo 'Order Total: '.$post_variables["amount"].'<br>'; echo 'Shipping: '.$post_variables["shipping"].'<br>'; ?> Do the values you get make sense? Quote Link to comment https://forums.phpfreaks.com/topic/47347-how-to-stop-shopping-cart-from-rounding-order-total/#findComment-231405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.