Jump to content
Larry Ullman's Book Forums

How To Add Shipping Cost To View_Cart.Php (Chapter 19)

Recommended Posts

Hi All


How do I add shipping_costs to view_cart.php. It's getting more complicated the more I think about real life situations because shipping costs is determined by weight.


The column 'shipping_cost' already added to database, so do I plus (+) the shipping cost to quantity, or times (x) shipping cost by quantity? Can someone show me both ways as it probably depends on the situation in real life.


Here's the view_cart.php section that needs the extra code:

		// Calculate the total and sub-totals.
		$subtotal = $_SESSION['cart'][$row['print_id']]['quantity'] * $_SESSION['cart'][$row['print_id']]['price'];
		$total += $subtotal;
		// Print the row:
		echo "\t<tr>
		<td align=\"left\">{$row['artist']}</td>
		<td align=\"left\">{$row['print_name']}</td>
		<td align=\"right\">£{$_SESSION['cart'][$row['print_id']]['price']}</td>
		<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['print_id']}]\" value=\"{$_SESSION['cart'][$row['print_id']]['quantity']['shipping_cost']}\" /></td>
		<td align=\"right\">£" . number_format ($subtotal, 2) . "</td>
Link to comment
Share on other sites

There's no one right answer here. It really depends upon the products being shipped. The three mitigating factors are: size, weight, and time sensitivity (e.g., perishables). 


If you're just using weight, and size generally won't be a problem, I'd store the actual weights in the database, as part of the item's details, and then add up the weights in the cart. Then use a function that accepts a total weight and returns the shipping cost based upon whatever formula. 


If it's something like size, you could store a fixed extra cost associated with the item (as part of the item's details in the database). Or you could store a extra shipping expense multiplier. Like this things costs 25% more shipping. Again you'd use a PHP function that takes the total multiplier (of all the items in the cart) and return a shipping cost.


Again, the specifics really depends upon the items being sold and the business's policies. The main thing is to store as much useful information in the database as possible, but in an abstract way (I.e., don't store fixed shipping costs there). Then use a single function to calculate shipping. This way, when shipping rates increase in the future, only the one function needs to be updated.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...