Jump to content

Help pls !! customize shipping cost


desmond_ckl

Recommended Posts

Hi there, I would like to customize $shippingCost in my shopping cart. I follow a tutorial from:

http://www.phpwebcommerce.com/plaincart/index.php

 

This tutorial using flat shipping rate, how to customize shippingCost based on the product weight ? 

 

Example: shipping method will based on weight & place within Malaysia:

 

west malaysia, up to 1kg = rm9

east malaysia, up to 1kg = rm12

 

west malaysia, up to 2kg = rm12

east malaysia, up to 2kg = rm16

 

west malaysia, up to 5kg = rm18

east malaysia, up to 5kg = rm32

 

west malaysia, up to 10kg = rm24

east malaysia, up to 10kg = rm48

 

 

Do i need to create a new field inside my database for product weight ? is it $weight * $product_quantity ?  Does anyone know how to accomplish this customization? . Please help because I am very confuse. Thank you in advanced 

 

 

checkout-functions.php:

<?php
require_once 'config.php';

/*********************************************************
*                 CHECKOUT FUNCTIONS 
*********************************************************/
function saveOrder()
{
$orderId       = 0;
$shippingCost  = 5;
$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, 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_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(), NOW(), '$hidShippingFirstName', '$hidShippingLastName', '$hidShippingAddress1', 
			        '$hidShippingAddress2', '$hidShippingPhone', '$hidShippingState', '$hidShippingCity', '$hidShippingPostalCode', '$shippingCost',
					'$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 )
*/
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;
}	

return $orderAmount;	
}

?>

 

 

any help will be greatly appreciated  :D

Link to comment
Share on other sites

1)You will have to store the weight of each product.

2) than ideally (from my point of view) have a table with the various prices per weight range. (so you can easily  add and adjust stuff)

 

Than when someone orders, say 10 banana's and 12 oranges you just do:

(10 banana's * weight of a banana) + (12 oranges * weight of an orange) = total_weight ;

than use that 'total_weight' to look up in your weight table. and output the price.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.