Unable to get total amount after checkout. Why???????

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Unable to get total amount after checkout. Why???????

Post by ashebrian »

I'm trying to get he total cost and send it to the 3rd party website. What the code actually does is it first saves the order first then with the $orderAmount sent to the 3rd party it only calculates ($totalPurchase + $shippingCost * 100). What i want it to do is calculate (($totalPurchase + $shippingCost + (($vat / 100) * $totalPurchase)) * 100;) But it doesn't seem to work......................why???????

I'm also trying to say that if the shipping cost is less than 300 there is no cost and couldn't do it in this code.

I've done it this way in the cart and displaying the info afterwards and works fine.

Code: Select all

<?php if ($subTotal <= $shipTotalHigh){
                            echo displayAmount($subTotal + 0 + (($vat / 100) * $subTotal)); 
                            } else {
                            echo displayAmount($subTotal + $shopConfig['shippingCost'] + (($vat / 100) * $subTotal));
                            }
                            ?>
I also tried to implement that code into the one below but wouldn't work at all. Is there another way it can work??

Here's my checkout-function.php code:

Code: Select all

function saveOrder()
{
    $orderId       = 0;
    $shippingCost  = 10;
    $vat  = 21;
    $requiredField = array('hidShippingFirstName', 'hidShippingLastName', 'hidShippingAddress1', 'hidShippingCity', 'hidShippingPostalCode',
                           'hidPaymentFirstName', 'hidPaymentLastName', 'hidPaymentAddress1', 'hidPaymentCity', 'hidPaymentPostalCode');
                           
    if (checkRequiredPost($requiredField)) {
        extract($_POST);
        
        // make sure the first character in the 
        // customer and city name are properly upper cased
        $hidShippingFirstName = ucwords($hidShippingFirstName);
        $hidShippingLastName  = ucwords($hidShippingLastName);
        $hidPaymentFirstName  = ucwords($hidPaymentFirstName);
        $hidPaymentLastName   = ucwords($hidPaymentLastName);
        $hidShippingCity      = ucwords($hidShippingCity);
        $hidPaymentCity       = ucwords($hidPaymentCity);
                
        $cartContent = getCartContent();
        $numItem     = count($cartContent);
        
        // save order & get order id
        $sql = "INSERT INTO tbl_order(od_date, member_username, od_last_update, od_shipping_first_name, od_shipping_last_name, od_shipping_address1, 
                                      od_shipping_address2, od_shipping_phone, od_shipping_state, od_shipping_city, od_shipping_postal_code, od_shipping_cost, od_vat,
                                      od_payment_first_name, od_payment_last_name, od_payment_address1, od_payment_address2, 
                                      od_payment_phone, od_payment_state, od_payment_city, od_payment_postal_code)
                VALUES (NOW(), '$hidClient', NOW(), '$hidShippingFirstName', '$hidShippingLastName', '$hidShippingAddress1', 
                        '$hidShippingAddress2', '$hidShippingPhone', '$hidShippingState', '$hidShippingCity', '$hidShippingPostalCode', '$shippingCost', '$vat',
                        '$hidPaymentFirstName', '$hidPaymentLastName', '$hidPaymentAddress1', 
                        '$hidPaymentAddress2', '$hidPaymentPhone', '$hidPaymentState', '$hidPaymentCity', '$hidPaymentPostalCode')";
        $result = dbQuery($sql);
        
        // get the order id
        $orderId = dbInsertId();
        
        if ($orderId) {
            // save order items
            for ($i = 0; $i < $numItem; $i++) {
                $sql = "INSERT INTO tbl_order_item(od_id, pd_id, od_qty)
                        VALUES ($orderId, {$cartContent[$i]['pd_id']}, {$cartContent[$i]['ct_qty']})";
                $result = dbQuery($sql);                    
            }
        
            
            // update product stock
            for ($i = 0; $i < $numItem; $i++) {
                $sql = "UPDATE tbl_product 
                        SET pd_qty = pd_qty - {$cartContent[$i]['ct_qty']}
                        WHERE pd_id = {$cartContent[$i]['pd_id']}";
                $result = dbQuery($sql);                    
            }
            
            
            // then remove the ordered items from cart
            for ($i = 0; $i < $numItem; $i++) {
                $sql = "DELETE FROM tbl_cart
                        WHERE ct_id = {$cartContent[$i]['ct_id']}";
                $result = dbQuery($sql);                    
            }                           
        }                   
    }
    
    return $orderId;
}
 
/*
    Get order total amount ( total purchase + shipping cost + vat)
*/
function getOrderAmount($orderId)
{
    $orderAmount = 0;
    
    $sql = "SELECT SUM(pd_price * od_qty)
            FROM tbl_order_item oi, tbl_product p 
            WHERE oi.pd_id = p.pd_id and oi.od_id = $orderId
            
            UNION
            
            SELECT od_shipping_cost
            FROM tbl_order
            WHERE od_id = $orderId";
            
    $result = dbQuery($sql);
 
    if (dbNumRows($result) == 2) {
        $row = dbFetchRow($result);
        $totalPurchase = $row[0];
        
        $row = dbFetchRow($result);
        $shippingCost = $row[0];
        
        
        $orderAmount = ($totalPurchase + $shippingCost + (($vat / 100) * $totalPurchase)) * 100;
    }   
    
    return $orderAmount;    
}
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: Unable to get total amount after checkout. Why???????

Post by ashebrian »

I need this sorted by the end of this week. Can you please help?? I've gotten so far with my first php website and now i'm stuck at the very end while everything is displayed correctly. I can give you more of my codes if you need it............please help
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: Unable to get total amount after checkout. Why???????

Post by ashebrian »

I've tried this code:

Code: Select all

function getOrderAmount($orderId)
{
    if ($totalPurchase <= 300) {
    
    $orderAmount = 0;
    
    $sql = "SELECT SUM(pd_price * od_qty)
            FROM tbl_order_item oi, tbl_product p 
            WHERE oi.pd_id = p.pd_id and oi.od_id = $orderId
            
            UNION
            
            SELECT od_vat
            FROM tbl_order
            WHERE od_id = $orderId";
            
    $result = dbQuery($sql);
 
    if (dbNumRows($result) == 2) {
        $row = dbFetchRow($result);
        $totalPurchase = $row[0];
        
        $row = dbFetchRow($result);
        $vat = $row[0];
        
        
        $orderAmount = ($totalPurchase + (($vat / 100) * $totalPurchase)) * 100;
    
    }
    return $orderAmount;
    }
    
    else {
    
    $orderAmount = 0;
    
    $sql = "SELECT SUM(pd_price * od_qty)
            FROM tbl_order_item oi, tbl_product p 
            WHERE oi.pd_id = p.pd_id and oi.od_id = $orderId
            
            UNION
            
            SELECT od_shipping_cost
            FROM tbl_order
            WHERE od_id = $orderId
            
            UNION
            
            SELECT od_vat
            FROM tbl_order
            WHERE od_id = $orderId";
            
    $result = dbQuery($sql);
 
    if (dbNumRows($result) == 3) {
        $row = dbFetchRow($result);
        $totalPurchase = $row[0];
        
        $row = dbFetchRow($result);
        $shippingCost = $row[0];
        
        $row = dbFetchRow($result);
        $vat = $row[0];
        
        
        $orderAmount = ($totalPurchase + $shippingCost + (($vat / 100) * $totalPurchase)) * 100;
    }   
    
    return $orderAmount;
    }   
}
 
?>
But get an error from the 3rd party saying that there's Invalid characters in amount field. Can you help sort this?
Post Reply