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

PHP 5 e-commerce Development- P26 ppsx

5 110 0
Tài liệu đã được kiểm tra trùng lặp

Đ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 277,68 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Template switchingAs 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.. This needs to be d

Trang 1

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 files

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 fields and upload form templates

The first 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.

Trang 2

//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 fields is simply a case of caching an array of data, listing the name

and key of the field—the key is used as the ID and name of the input fields, and the

name value is for the label of the field, 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');

}

}

Trang 3

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 field, 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

Trang 4

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 files, and also supply custom

text, we need to record the file(s) and the text And depending on the file 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 file, 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 files 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

Trang 5

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 fields 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.

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

w