PHP 5 e-commerce Development- P31 ppsx

5 104 0
PHP 5 e-commerce Development- P31 ppsx

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

Thông tin tài liệu

Chapter 5 [ 133 ] The controller also needs to detect if the product is valid; we can do this by linking it up with the products model, and if it isn't a valid product, the customer should be informed of this. Let's look through a potential addProduct() method for our wish-list controller. /** * Add a product to a user's wish list * @param String $productPath the product path * @return void */ We rst check if the product is valid, by creating a new product model object, which informs us if the product is valid. private function addProduct( $productPath ) { // check product path is a valid and active product $pathToRemove = 'wishlist/add/'; $productPath = str_replace( $pathToRemove, '', $this->registry->getURLPath() ); require_once( FRAMEWORK_PATH . 'models/products/model.php'); $this->product = new Product( $this->registry, $productPath ); if( $this->product->isValid() { // check if user is logged in or not if( $this->registry->getObject('authenticate')-> loggedIn() == true ) { //Assuming the user is logged in, we can also store their ID, // so the insert data is slightly different. Here we insert the // wish into the database. $wish = array(); $pdata = $this->product->getData(); $wish['product'] = $pdata['ID']; $wish['quantity'] = 1; $wish['user'] = $this->registry->getObject('authenticate')-> getUserID(); $this->registry->getObject('db')-> insertRecords('wish_list_products', $wish ); // inform the user $this->registry->getObject('template')->getPage()-> addTag('message_heading', 'Product added to your wish list'); $this->registry->getObject('template')->getPage()-> addTag('message_heading', 'A ' . $pdata['name'] This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Enhancing the User Experience [ 134 ] .' has been added to your wish list'); $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'message.tpl.php', 'footer.tpl.php'); } The customer isn't logged into the website, so we add the wish to the database, using session and IP address data to tie the wish to the customer. else { // insert the wish $wish = array(); $wish['sessionID'] = session_id(); $wish['user'] = 0; $wish['IPAddress'] = $_SERVER['REMOTE_ADDR']; $pdata = $this->product->getData(); $wish['product'] = $pdata['ID']; $wish['quantity'] = 1; $this->registry->getObject('db')-> insertRecords('wish_list_products', $wish ); // inform the user $this->registry->getObject('template')->getPage()-> addTag('message_heading', 'Product added to your wish list'); $this->registry->getObject('template')->getPage()-> addTag('message_heading', 'A ' . $pdata['name'] .' has been added to your wish list'); $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'message.tpl.php', 'footer.tpl.php'); } } The product wasn't valid, so we can't insert the wish, so we need to inform the customer of this. else { // we can't insert the wish, so inform the user $this->registry->getObject('template')->getPage()-> addTag('message_heading', 'Invalid product'); $this->registry->getObject('template')->getPage()-> addTag('message_heading', 'Unfortunately, the product you tried to add to your wish list was invalid, and was not added, please try again'); This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 5 [ 135 ] $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'message.tpl.php', 'footer.tpl.php'); } } Add to wish list To actually add a product to our wish list, we need a simple link within our products view. This should be /wishlist/add/product-path. <p> <a href="wishlist/add/{product_path}" title="Add {product_name} to your wishlist"> Add to wishlist. </a> </p> We could encase this link around a nice image if we wanted, making it more user friendly. When the user clicks on this link, the product will be added to their wish list and they will be informed of that. Viewing a wish list As our customers are now able to create their own wish lists, we need to allow them to view and manage their wish lists. Controller changes Our controller needs to be modied to list items in a user's wish list; this involves detecting if the user is logged in or not, as this will determine the query it must use to lookup products. In addition to a function to display the list to the customer, we need to detect if the customer is trying to add a product to the list, or if they are trying to view the list, though a switch statement in the constructor. private function viewList() { $s = session_id(); $ip = $_SERVER['REMOTE_ADDR']; $uid = $this->regisry->getObject('authenticate')->getUserID(); if( $this->registry->getObject('authenticate')->loggedIn() ) { $when = strtotime("-1 week"); $when = date("Y-m-d h:i:s", $when); This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Enhancing the User Experience [ 136 ] $sql = "SELECT p.price AS product_price, v.name AS product_name, c.path AS product_path FROM content c, content_versions v, content_types_products p, wish_list_items w WHERE c.ID=w.product AND p.content_version=v.ID AND v.ID=c.current_revision AND c.active=1 AND ( w.user='{$uid}' OR ( w.sessionID='{$s}' AND w.IPAddress='{$ip}' AND w.dateadded > '{$when}' ) )"; } else { $sql = "SELECT p.price AS product_price, v.name AS product_name, c.path AS product_path FROM content c, content_versions v, content_types_products p, wish_list_items w WHERE c.ID=w.product AND p.content_version=v.ID AND v.ID=c.current_revision AND c.active=1 AND w.user=0 AND ( w.sessionID='{$s}' AND w.IPAddress='{$ip}' AND w.dateadded > '{$when}' )"; } $cache = $this->registry->getObject('database')-> cacheQuery( $sql ); if( $this->registry->getObject('database')-> numRowsFromCache( $cache ) == 0 ) { $this->registry->getObject('template')->getPage()-> addTag('message_heading', 'No products'); $this->registry->getObject('template')->getPage()-> addTag('message_heading', 'Unfortunately, there are no products in your wish-list at this time.'); $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'message.tpl.php', 'footer.tpl.php'); } else This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 5 [ 137 ] { $this->registry->getObject('template')->getPage()-> addTag( 'wishes', array( 'SQL', $cache ) ); $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'wishlist.tpl.php', 'footer.tpl.php'); } } The highlighted section within that code segment illustrates the difference between a user being logged out or logged in. For a logged-in user, the wish list displays products both associated to their user account and also ones associated with their session and IP address details. For users who are logged out, it ensures the user ID is set to 0; otherwise, they may end up viewing other customers' wish-list products. It limits the timeframe for which session-based products are viewed. This is because session IDs and IP addresses are not a secure method to "authenticate" against. We should investigate some form of garbage collection to ensure out-of-date session- based wish-list products are removed, and also something to detect if the logged- in user has some wish-list products that are not associated with their user ID, and transfer them to their user account to prevent them from losing them. Wish-list view Although we would need to extend this later, to include a purchase button, a priority, and quantity, this code would sufce for a basic view to show our customers their desired products for the time being: <h2>Your wishlist</h2> <ul> <! START wishes > <li><a href="products/view/{product_path}">{product_name}</a></li> <! END wishes > </ul> Purchases The purchases aspect of this feature is the most complicated, as it needs to facilitate both customers making purchases for themselves, and also others making a gift purchase for someone. We can't actually implement this aspect yet, as we don't have a shopping basket or a complete order process. However, we can discuss what will be involved. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 . 2010 953 Quincy Drive, , Brick, , 08724 Chapter 5 [ 1 35 ] $this->registry->getObject('template')-> buildFromTemplates('header.tpl .php& apos;, 'message.tpl .php& apos;,. buildFromTemplates('header.tpl .php& apos;, 'message.tpl .php& apos;, 'footer.tpl .php& apos;); } else This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy. $this->registry->getObject('template')-> buildFromTemplates('header.tpl .php& apos;, 'message.tpl .php& apos;, 'footer.tpl .php& apos;); } The customer isn't logged into the website, so

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

Từ khóa liên quan

Mục lục

  • Preface

  • PHP e-commerce

    • e-commerce: who, what, where, why?

      • An overview of e-commerce

        • eBay

        • Amazon

        • Brick 'N Mortar stores

        • Service-based companies

        • Why use e-commerce?

        • Rolling out your own framework

          • Why PHP?

          • Why a framework?

          • When to use an existing package?

            • Existing products

            • A look at e-commerce sites

              • iStockphoto

              • WooThemes

              • eBay

              • Amazon

              • Play.com

              • e-commerce: what does it need to do/have?

                • Products

                • Checkout process

                • General

                • Our framework: what is it going to do?

                • Our framework: why is it going to do it?

                  • Juniper Theatricals

                  • Summary

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan