Shopping cart
How it works
To implement virtual shopping cart function we use sessions.
For each visitor new session is initiated. We can use a simple array to store product, which customer has added to his shopping cart.
If visitor clicks "Add to cart" button, following PHP code is executed (includes/shopping_cart.php):
|
Here we use two array to store products in the shopping cart:
$_SESSION["gids"] contains productID values of the products which are currently in the customer's shopping cart.
$_SESSION["counts"] contains item quantities information - how many items of a certain product are there in customer's cart.
Such method allows easily add products to cart and remove them. Here is how you can remove a product from cart:
|
And if you would like to clear cart content (remove all products from cart), simple execute this code:
|
It's that easy.
Displaying shopping cart content
To display shopping cart content to customer, we only load cart content from session to Smarty variable, and then it can be presented to customer using this Smarty template (shopping_cart.tpl.html):

{section loop=$cart_content name=i}
<tr>
<td>{if $cart_content[i].product_code ne ""}[{$cart_content[i].product_code}] {/if}{$cart_content[i].name}</td>
<td><input type="text" name="count_{$cart_content[i].id}" size="5" value="{$cart_content[i].quantity}"></td>
<td>{$cart_content[i].cost}</td>
<td>
<a href="index.php?shopping_cart=yes&remove={$cart_content[i].id}"><img src="images/remove.jpg" border="0" alt="{$smarty.const.DELETE_BUTTON}"></a>
</td>
</tr>
{/section}
Checkout
Shopping cart content is stored in session till the moment visitor closes browser window (in this case session is terminated) or places an order (we clear shopping cart content stored in session). Learn more about accepting orders from customers and saving them to the database.



0 comments:
Post a Comment