In this post, we’ll add the ‘website’ attribute to checkout. Entities for ‘order’ exist outside the EAV model, so we’ll need to add a column for ‘website’ to the tables sales_flat_order_address and sales_flat_quote_address.
We’ll run an upgrade script to add the new column to these tables, upgrade-0.1.0-0.2.0.php. In the previous post, in the install script, we added the new attribute to the EAV model using ‘addAttribute’. In an earlier post we added the column ‘estdate’ to the sales_flat_order table using addAttribute(‘order’, ‘estdate’, array(‘type’ => ‘text’)). Here in our upgrade script, we’ll use ‘addColumn’ to add ‘website’ to the sales_flat_order_address and sales_flat_quote_address tables.
1
2$installer = $this;
3
4$installer->startSetup();
5
6$installer->getConnection()
7->addColumn($installer->getTable(‘sales/quote_address’),
8‘website’,
9array(
10‘type’ => Varien_Db_Ddl_Table::TYPE_TEXT,
11‘nullable’ => false,
12‘comment’ => ‘website’,
13)
14);
15
16$installer->getConnection()
17->addColumn($installer->getTable(‘sales/order_address’),
18‘website’,
19array(
20‘type’ => Varien_Db_Ddl_Table::TYPE_TEXT,
21‘nullable’ => false,
22‘comment’ => ‘website’,
23)
24);
25
26$installer->endSetup();
Before we attempt to run this script, we must remember to change the version number of config.xml from 0.1.0 to 0.2.0. Then refresh any page on the site to run the script. If successful, we can now store ‘website’ values for orders in the tables sales_flat_order_address and sales_flat_quote_address, as well as in the customer_entity_address_varchar table for the customer record as per the previous post.
Add the attribute to the checkout address templates
We need to amend the templates for persistent/checkout/onepage/billing.phtml and checkout/onepage/shipping.phtml. In both we’re replacing the ‘fax’ field which the client regards as redundant. New code shown in red. Here’s the excerpt for billing.phtml: –
123<li class=”fields”>
124<div class=”field”>
125<label for=”billing:telephone” class=”required”><em>*</em><?php echo $this->__(‘Telephone’) ?></label>
126<div class=”input-box”>
127<input type=”text” name=”billing[telephone]” value=”<?php echo $this->escapeHtml($this->getAddress()->getTelephone()) ?>” title=”<?php echo $this->__(‘Telephone’) ?>” class=”input-text <?php echo $this->helper(‘customer/address’)->getAttributeValidationClass(‘telephone’) ?>” id=”billing:telephone” />
128</div>
129</div>
130<div class=”field”>
131<label for=”website”><?php echo $this->__(‘Website’) ?></label>
132<div class=”input-box”>
133<input type=”text” name=”billing[website]” value=”<?php echo $this->escapeHtml($this->getAddress()->getWebsite()) ?>” title=”<?php echo $this->__(‘Website’) ?>” class=”input-text” id=”billing:website” />
134</div>
135</div>
136</li>
And here’s the excerpt for shipping.phtml: –
109<li class=”fields”>
110<div class=”field”>
111<label for=”shipping:telephone” class=”required”><em>*</em><?php echo $this->__(‘Telephone’) ?></label>
112<div class=”input-box”>
113<input type=”text” name=”shipping[telephone]” value=”<?php echo $this->escapeHtml($this->getAddress()->getTelephone()) ?>” title=”<?php echo $this->__(‘Telephone’) ?>” class=”input-text <?php echo $this->helper(‘customer/address’)->getAttributeValidationClass(‘telephone’) ?>” id=”shipping:telephone” onchange=”shipping.setSameAsBilling(false);” />
114</div>
115</div>
116<div class=”field”>
117<label for=”website”><?php echo $this->__(‘Website’) ?></label>
118<div class=”input-box”>
119<input type=”text” name=”shipping[website]” value=”<?php echo $this->escapeHtml($this->getAddress()->getWebsite()) ?>” title=”<?php echo $this->__(‘Website’) ?>” class=”input-text” id=”shipping:website” />
120</div>
121</div>
122</li>
We should now see the input field for ‘website’ in billing and shipping addresses in checkout.
Recall the issue we noted in the previous post. When we created a new order in the Admin-Sales-Orders-Create New Order page, the value stored in the ‘customer_entity_address_varchar’ table does not show as the initial value for either the billing or the shipping input fields. The same is now true when we checkout as a customer using our newly adapted templates. No initial value is shown for the billing or shipping input fields for ‘website’. We can remedy this. Add the following xml to the global tag in this module’s config.xml.
20<fieldsets>
21<customer_address>
22<website>
23<to_quote_address>*</to_quote_address>
24</website>
25</customer_address>
26</fieldsets>
Create a new order in Admin-Sales-Orders-Create New Order or in checkout. You will now see the initial value for ‘website’ displayed as stored in the customer_entity_address_varchar table.
We will soon notice another issue. If you create a new order in Admin-Sales-Orders-Create New Order or in checkout, you may be surprised to discover that the website value is stored in sales_flat_quote_address but not in sales_flat_order_address. In fact in both tables we have two new rows, one for billing address and one for shipping address, but both rows in the sales_flat_order_address table have nothing for website value. So don’t expect to see a value for website in billing address or in shipping address when you look at the order in Admin.
OK let’s ensure the value is transferred from the quote (what you selected in checkout) to the order (what you confirmed when you completed checkout).
Transfer from quote to order
Add the xml in red to config.xml.
20<fieldsets>
21<customer_address>
22<website>
23<to_quote_address>*</to_quote_address>
24</website>
25</customer_address>
26<sales_convert_quote_address>
27<website>
28<to_order_address>*</to_order_address>
29</website>
30</sales_convert_quote_address>
31</fieldsets>
Create a new order in Admin or checkout and the quote value for ‘website’ is transferred to the sales_flat_order_address table for billing and shipping addresses. Check the last order in Admin and you’ll see the new ‘website’ is there for billing and shipping address.
OK, the ‘website’ attribute now appears in all the places we want it too. Let’s in the next post take a look at the attribute for ‘Reason for approaching us’, which is a select field.