1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5 e-commerce Development- P39 pot

5 43 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 280,54 KB

Nội dung

Chapter 6 [ 173 ] A new database table A table like the one we created in Chapter 4, Product Variations and User Uploads, to relate products to potential variations, is needed to relate chosen product variants with products in a customer's shopping basket. The exception is that we don't need to retain the order eld or the cost difference. Field Type Description Basket_id Integer A reference to the product in a customers basket Attribute_id Integer A reference to a particular attribute for that product, indicating how that instance of the product in the basket should be customized or which variant of the product it is. The following code represents the new table: CREATE TABLE IF NOT EXISTS `basket_attribute_value_association` ( `basket_id` int(11) NOT NULL, `attribute_id` int(11) NOT NULL, KEY `basket_id` (`basket_id`,`attribute_id`), KEY `attribute_id` (`attribute_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Association of basket contents and attribute values'; Model changes As far as our model is concerned, we need to: Store products that have variations, which have been added to the customer basket, with an array key prex of variation This allows the basket to process these products differently when displaying them in the basket, and also when it comes to the checkout process. In addition to the prex and the product ID, we also need to store a unique reference, to indicate that the product is customized by means of the variations selected. This means if a customer has purchased two variations of one product, they are listed as two products in their basket. Their reference numbers allow the customer to update quantities and remove them. If the reference was not unique, removing one product variant from the basket would remove the other, which wouldn't be what the customer wanted. Check if products in the basket have variations associated with them so that the details of the variant can be displayed. Store references to the variations within the array for basket contents. • • • • This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 The Shopping Basket [ 174 ] When adding a product, check relevant POST data for variations that are being selected. The variants of a product (sizes, colors, and so on) are all displayed in drop-down lists on the product view. When the customer adds one of these products to the basket, it is done by a form submit button, which passes the values of these drop-downs as POST data, so we need to check for this data, to determine which variations and attributes need to be stored. The controller In addition to the features discussed earlier, at this stage in the chapter, our controller needs to also display attributes from variations when viewing the basket, so the customer is aware of exactly which products (or which variations of the products) they are purchasing, and can change quantities of/remove them from their basket. Editing quantities Once a product has been added to the basket, we want to make it easy for the customer to change the quantity. The hope here is obviously that they will increase the quantities and purchase more, but equally, we need to allow them to reduce quantities or remove products, until they are happy with the contents of their basket and want to proceed. When viewing the basket, each entry in our basket contents array will be a new row in a table within the view, and within this row will be a eld allowing us to change the quantity. This eld should have a reference the same as the key it has in the basket contents array. This is to ensure that we update the correct customized product; after all, if we have two customized instances of a product, and we wish to purchase two copies of one of them, we only want to alter the quantity of that record in our model and our database. The following code in our controller updates the basket based on new quantities quantitiesquantities supplied by the customer: /** * Update the shopping basket */ private function updateBasket() { // First, this populates the basket model, unless this has already // been done. if( ! $this->basket->isChecked == true ) { $this->basket->checkBasket(); } • This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 6 [ 175 ] We then go through each entry in the basket table (that is, each unique product in the customer's basket) and where there is a quantity that the customer has submitted, this is updated; if the customer removes the quantity for a product, it is then removed from the basket. foreach( $this->basket->getContents() as $pid => $data ) { // get the product rows basket ID $bid = $data['basket']; if( intval( $_POST['qty_' . $bid ] ) == 0 ) { $this->basket->removeProduct( $bid ); } else { $this->basket->updateProductQuantity( $bid, intval( $_POST['qty_' . $bid] ) ); } } // save the extra processing by marking embedded as false $this->embedded = false; // We then redirect the user to the basket page, informing them // that their changes have been saved. $this->registry->redirectUser('basket', 'Basket updated', 'Your shopping basket has been updated', false ); } If the quantities are zero, or if the customer clicks on a remove product link, we need a removeProduct function to remove the product from the basket. public function removeProduct( $bid ) { $this->basket->removeProduct( $bid ); $this->registry->redirectUser('basket' , 'Product removed', 'The product has been removed from your basket', false ); } Finally, the constructor needs some additional cases in its switch statement to call the appropriate functions depending on the page the customer is visiting, be it an "update basket" page, or the "remove product from basket" page. switch( $urlBits[1] ) { case 'view': $this->viewBasket(); break; case 'add-product': echo $this->addProduct( $urlBits[2], 1); This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 The Shopping Basket [ 176 ] break; case 'update': $this->updateBasket(); break; case 'remove-product': $this->removeProduct( intval( $urlBits[2] ) ); break; default: $this->viewBasket(); break; } Both the update and the removeupdate and the remove and the removeremove product functions rely on methods in the basket functions rely on methods in the basket model; these methods are very simple. To change the quantity of a product, we simply update the basket table with the new quantity. public function updateProductQuantity( $basketItemId, $quantity ) { $s = session_id(); $u = ( $this->registry->getObject('authenticate')-> isLoggedIn() ) ? $this->registry-> getObject('authenticate')->getUserID() : 0; $ip = $_SERVER['REMOTE_ADDR']; $changes = array( 'quantity' => $quantity ); $this->registry->getObject('db')-> updateRecords( 'basket_contents', $changes, " session_id='{$s}' AND user_id={$u} AND ID={$basketItemId} AND ip_address='{$ip}' "); } To remove a product, we simply remove an entry from the basket table, which corresponds to that particular instance of a product in the customer's basket. public function removeProduct( $basketItemId ) { $s = session_id(); $u = ( $this->registry->getObject('authenticate')->isLoggedIn() ) ? $this->registry->getObject('authenticate')->getUserID() : 0; $ip = $_SERVER['REMOTE_ADDR']; $this->registry->getObject('db')-> deleteRecords( 'basket_contents', " session_id='{$s}' AND user_id={$u} AND ID={$basketItemId} AND ip_address='{$ip}' ", 1 ); } This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 6 [ 177 ] From visitor to a user When a visitor comes to our site, adds products to their basket, and then signs up, we need to transfer the basket to them, as a user. This would allow them to access this basket on other computers too. The transferToUser function A function within our basket model would make this easy; all it needs to do is nd any products in the basket table, which are associated with the user's session data and update these records to have the user's ID contained. /** * Transfer the basket to another user * @param int user id * @return bool */ public function transferToUser( $user ) { $changes = array( 'user_id' => $user ); $s = session_id(); $ip = $_SERVER ['REMOTE_ADDR']; $this->registry->getObject('db')-> updateRecords( 'basket_contents', $changes, " SESSION_ID='{$s}' AND ip='{$ip}' "); return true; } One thing you may notice is that we are not ltering this based on recent entries. The reason for that is, out-of-date entries in the basket table should be pruned automatically, which we will cover in the next section. Performing the transfer The transferToUser( $user ) method should be called when processing a user login. However, we would need to use the directCall parameter in the controller to ensure that the controller does not try to display a basket-related page when the customer is not trying to view such a page. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 . and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 6 [ 1 75 ] We then go through each entry in the basket table (that is, each unique. material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 The Shopping Basket [ 174 ] When adding a product, check relevant. like the one we created in Chapter 4, Product Variations and User Uploads, to relate products to potential variations, is needed to relate chosen product variants with products in a customer's

Ngày đăng: 07/07/2014, 10:20