Product Variations and User Uploads [ 108 ] Template switching As with product variations, the templates can be used to generate the view to show the customer depending on if and how a product can be customized. If a product has no customization options, then we would just show them the standard template; otherwise, we will show them a template, which also supports the uploading of les and or supplying some text with the product. This needs to be done in conjunction with the template switching for the product variations, so let's edit the code we discussed earlier to account for additional text elds and upload form templates. The rst section of the code is the same as when we previously looked into switching templates. if( $this->model->hasAttributes() ) { $attrdata = $this->model->getAttributes(); $attrs = array_keys( $attrdata ); $temp = array(); $aftertags = array(); foreach( $attrs as $attribute ) { $temp[] = array( 'attribute_name' => $attribute ); $vtemp = array(); foreach( $attrdata[ $attribute ] as $key => $value ) { $vtemp[] = array('value_id'=> $value['attrid'], 'value_name'=>$value['attrvalue']); } $cache = $this->registry->getObject('db')->cacheData( $vtemp ); $aftertags[] = array( 'cache'=>$cache, 'tag' => 'values_' . $attribute); } $cache = $this->registry->getObject('db')->cacheData( $temp ); $this->registry->getObject('template')->getPage()-> addTag( 'attributes', array('DATA', $cache ) ); foreach( $aftertags as $key => $data ) { $this->registry->getObject('template')->getPage()-> addTag( $data['tag'], array('DATA', $data['cache'] ) ); } // The change to the code appears here, we detect if the product // has custom text inputs. if( $this->model->hasCustomTextInputs() ) { This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 4 [ 109 ] //If it does, we also check to see if the product allows uploads // to be submitted by the customer. if( $this->model->allowUploads() ) { // Assuming this is the case, we then build the list of fields // and use a template, which accommodates both custom text // inputs and file uploads. $fieldsdata = $this->model->getCustomTextInputs(); $tags = array(); foreach( $fieldsdata as $fieldkey => $name ) { $tags[] = array('fieldkey' => $fieldkey, 'fieldname' => $name ); } $cache = $this->registry->getObject('db')->cacheData( $tags ); $this->registry->getObject('template')->getPage()-> addTag('fields', array( 'DATA', $cache ) ); $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'product-attributes-custom-upload.tpl.php', 'footer.tpl.php'); } Building the list of elds is simply a case of caching an array of data, listing the name and key of the eld—the key is used as the ID and name of the input elds, and the name value is for the label of the eld, informing the customer of what information they need to supply. else { $fieldsdata = $this->model->getCustomTextInputs(); $tags = array(); foreach( $fieldsdata as $fieldkey => $name ) { $tags[] = array('fieldkey' => $fieldkey, 'fieldname' => $name ); } $cache = $this->registry->getObject('db')->cacheData( $tags ); $this->registry->getObject('template')->getPage()-> addTag('fields', array( 'DATA', $cache ) ); $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'product-attributes-custom.tpl.php', 'footer.tpl.php'); } } This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Product Variations and User Uploads [ 110 ] If the product doesn't have custom text inputs, we still check to see if uploads are permitted; if they are, we use a template which displays the upload eld, but not the custom text inputs. elseif( $this->model->allowUploads() ) { $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'product-attributes-uploads.tpl.php', 'footer.tpl.php'); } else { $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'product-attributes.tpl.php', 'footer.tpl.php'); } } else { $this->registry->getObject('template')-> buildFromTemplates('header.tpl.php', 'product.tpl.php', 'footer.tpl.php'); } Shopping basket preparation Although we won't be developing our shopping basket until Chapter 6, let us have a brief think about the consequences of customizable products. Stock control With product variations, stock control becomes an interesting issue. If we only had a single set of variations for each products (as we discussed under the Simple variants section, earlier in the chapter), we could simply disable an option if it was out of stock. However, with multiple variations we may have small blue t-shirts, but not large blue t-shirts. The logic for detecting if this is in stock obviously lies with the shopping basket itself: when we click on Add to basket, it will need to detect to see if there are any in stock. This obviously isn't an ideal situation; however, an alternative would be to utilize AJAX to enable the view to alert the customer that a particular combination is out of stock, by performing a lookup as and when the customer selects their variation of the product. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 4 [ 111 ] Product variations The shopping basket needs to detect which variations have been selected, and store a record of this as the customer's intended purchase. Because each product order could be a product with more than one variation, for instance both size and color, this would need to be maintained in a database table of its own. Product customizations For products where the customer can upload images or les, and also supply custom text, we need to record the le(s) and the text. And depending on the le type we may wish to do something with that; for instance, if it was an image we may wish for that to be displayed in the basket as the product's image. Both this and the product variations have another interesting implication when it comes to the shopping basket. If we were to add a product to our shopping basket on an e-commerce store, and then go back to the products page and add it again, we would expect to see the product listed once, with a quantity of two. However, if we add one product, with a variation or uploaded le, and then add the product again, we would need for these to display as two separate listings in the shopping basket. Basket templates Because of the different templates we have created to represent the product view, there is a minor implication for our shopping basket. A standard product would just require a customer to click on a link to add a product to their basket. With the customizable products, the user would need to click on a form submit button to pass the uploaded les and custom text, so our basket will need to add products based on both methods. Product subtotals With the costs or cost differences associated with various product variations, we will need to take this into consideration when we have the basket calculate totals and subtotals of orders. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Product Variations and User Uploads [ 112 ] Summary In this chapter, we have taken our e-commerce framework and extended the products' provisions to allow for variations of products and products to be customized by the customer using upload and free-text elds. We have also discussed the implications of these features upon the shopping basket, something which we will come to again later in Chapter 6, The Shopping Basket. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 . $this->registry->getObject('template')-> buildFromTemplates('header.tpl .php& apos;, 'product-attributes-custom-upload.tpl .php& apos;, 'footer.tpl .php& apos;); } Building the list of elds is simply a. $this->registry->getObject('template')-> buildFromTemplates('header.tpl .php& apos;, 'product-attributes-custom.tpl .php& apos;, 'footer.tpl .php& apos;); } } This material is copyright and is licensed. $this->registry->getObject('template')-> buildFromTemplates('header.tpl .php& apos;, 'product-attributes-uploads.tpl .php& apos;, 'footer.tpl .php& apos;); } else { $this->registry->getObject('template')->