Prev | Current Page 485 | Next

Emilian Balanescu and Cristian Darie

"Beginning PHP and MySQL E-Commerce: From Novice to Professional, Second Edition"

Create a new presentation object file named presentation/cart_details.php, and add the following
code to it:
// Class that deals with managing the shopping cart
class CartDetails
{
// Public variables available in smarty template
public $mCartProducts;
public $mSavedCartProducts;
public $mTotalAmount;
public $mIsCartNowEmpty = 0; // Is the shopping cart empty?
public $mIsCartLaterEmpty = 0; // Is the 'saved for later' list empty?
public $mLinkToContinueShopping;
public $mUpdateCartTarget;
// Private attributes
private $_mItemId;
private $_mCartAction;
// Class constructor
public function __construct()
{
if (isset ($_GET['CartAction']))
$this->_mCartAction = $_GET['CartAction'];
else
trigger_error('CartAction not set', E_USER_ERROR);
// These cart operations require a valid product id
if ($this->_mCartAction == ADD_PRODUCT ||
$this->_mCartAction == REMOVE_PRODUCT ||
$this->_mCartAction == SAVE_PRODUCT_FOR_LATER ||
$this->_mCartAction == MOVE_PRODUCT_TO_CART)
{
if (isset ($_GET['ItemId']))
$this->_mItemId = $_GET['ItemId'];
else
trigger_error('ItemId must be set for this type of request',
E_USER_ERROR);
}
$this->mUpdateCartTarget = Link::ToCart(UPDATE_PRODUCTS_QUANTITIES);
// Setting the "Continue shopping" link target
if (isset ($_SESSION['link_to_last_page_loaded']))
$this->mLinkToContinueShopping = $_SESSION['link_to_last_page_loaded'];
}
CHAPTER 12 ?–  CREATING YOUR OWN SHOPPING CART 386
public function init()
{
switch ($this->_mCartAction)
{
case ADD_PRODUCT:
$selected_attributes = array ();
$selected_attribute_values = array ();
// Get selected product attributes if any
foreach ($_POST as $key => $value)
{
// If there are fields starting with "attr_" in the POST array
if (substr($key, 0, 5) == 'attr_')
{
// Get the selected attribute name and value
$selected_attributes[] = substr($key, strlen('attr_'));
$selected_attribute_values[] = $_POST[$key];
}
}
$attributes = '';
if (count($selected_attributes) > 0)
$attributes = implode('/', $selected_attributes) .


Pages:
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
Łona Marc et Claude Anita Lipnicka M-People Michael Makai