Discounts, Vouchers, and Referrals [ 218 ] . $voucher['discount_amount'] . ' has been applied to your order'; return true; // Finally, if the discount operation is set to s // then, we set the shipping cost to the discount // value. This could allow us to set free shipping, // or just reduce shipping costs. } elseif( $voucher['discount_operation'] == 's' ) { $this->shipping_cost = $voucher['discount_amount']; $this->voucher_notice = 'Your orders shipping cost has been reduced to £' . $voucher['discount_amount']; return true; } } else { $this->voucher_notice = 'Sorry, your order total is not enough for your order to qualify for this discount code'; return false; } } else { $this->voucher_notice = 'Sorry, this was a limited edition voucher code, there are no more instances of that code left'; return false; } } } else { $this->voucher_notice = 'Sorry, the vocuher code you entered is no longer active'; return false; } } } } Now, we have the basis for our discount code feature. We can issue discount codes as an incentive to get new customers, or to encourage existing customers to make more purchases. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 9 [ 219 ] Reducing the number of codes available One feature we added to the database structure for our discount codes was the ability to limit the number of times a voucher could be used, effectively allowing a code to be "issued" a set number of times. We need to take this into account, and reduce the number of vouchers in circulation, once one has been used. This won't be done at this stage, but will need to be implemented at the nal checkout stage when an order is updated to "paid", or the customer pays online. The following function will do this for us; it checks to see if a code is used, or if it's unlimited or has expired, and if it's not unlimited and hasn't already expired, it updates the code to decrement the number of uses remaining: private function adjustDiscountCodeQuantities( $codeId ) { $sql = "SELECT num_vouchers FROM discount_codes WHERE ID=" . $codeId; $this->registry->getObject('db')->executeQuery( $sql ); if( $this->registry->getObject('db')->numRows() > 0 ) { $codeData = $this->registry->getObject('db')->getRows(); if( $codeData['num_vouchers'] > 0 ) { $sql = "UPDATE discount_codes SET num_vouchers=num_vouchers-1 WHERE ID=" . $codeId; $this->registry->getObject('db')->executeQuery( $sql ); } } } Purchasable voucher codes Voucher codes work in the same way as discount codes, except that they are purchased for use by a customer, as opposed to given away for promotional reasons. Existing functionality The discount codes and product variation features already give us most of the functionality required for purchasable voucher codes. Discount codes As a voucher code has the same functionality as our discount codes, which we built earlier, we have a large portion of the functionality in place. When a voucher is purchased, a new record in the discount codes table will be made, with a num_vouchers value of 1. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Discounts, Vouchers, and Referrals [ 220 ] Product variations The product variations feature we built in Chapter 4, Product Variations and User Uploads, allows us to create a purchasable voucher code product, with variations that increase the price in increments, perhaps of $5. Required additional functionality We only need to add additional functionality to this if we wish to automate the process of generating a voucher code when a customer purchases a voucher code. Without additional functionality, we would simply notice a new order, manually create a new discount code, and then e-mail the customer with the code. However, automating this would save us quite a lot of time, so let's look at what would be involved in doing this: 1. First, we must wait until an order has its status updated to "paid". This would either be when a payment is made online by the customer, or when an ofine payment is received and we, as the administrator, mark the order as "paid". 2. The order contents must be searched for anything that is a purchasable voucher code. 3. For each of these vouchers purchased, a new record must be automatically inserted into the voucher_codes table with the following values: vouchercode: A randomly generated string active: 1 min_basket_cost: The amount of the voucher purchased discount_operation: (because we are subtracting an amount from the cost) discount_amount: The amount of the voucher purchased num_vouchers: 1 expiry: A year in the future would be a suitable validity period 4. These vouchers would then be sent through e-mail to the customer. Referrals To reward loyal customers, we may wish to offer referral discounts to them. This would work by encouraging our customers to introduce new customers, and giving them a credit based off a percentage of orders placed by customers they refer to our store. Customers referring other customers would be given a referral code, which could either be part of a link used to promote the site, or given to customers to enter somewhere on the order process. • • • • • • • This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 9 [ 221 ] The referral process should work like this: 1 Check for a referral code in the URL. 2. If a code is found in the URL, it should be stored in a cookie, unless a previous referral code is already being stored; in that case, the older code should be used. 3. At the checkout stage, this code should be looked up, to check if it is valid and to see the commission to be applied to the customer who passed the referral. The order number, commission value, and referring customer should be stored in a database table. 4. When the order is updated to "paid" or "paid online", the commission should be applied to the relevant customer's account. Database changes This requires a new database table, as well as some database alterations to work. New table: Referrers A referrers table is required to link customers to a unique referral code, and a commission percentage. It would require the following elds: Field Type Description Customer ID Integer A reference to the customer's ID number Referral Code Varchar (Unique) The customer's referral code, which he/she would give to friends Commission percentage Float The percentage of commission that would be credited back to the customer's account Active Boolean If the customer is a referrer, or not, as we may wish to disable a customer's referral code, if he/she is abusing it Changes Customers should have a credit eld, showing how much credit they have with the store based on referred customers. The orders table should also be altered to store a record of any referral code used. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Discounts, Vouchers, and Referrals [ 222 ] Functionality The workow of this feature would work like this: 1. An order is placed, and a referral code is associated with it. 2. The order is marked as "paid". 3. A lookup is performed on the referrers table to nd the customer and the percentage. 4. The commission value is calculated. 5. The referring customer's credit value is updated to include new commission earned. We could extend this to record a list of transactions to a customer's credit, such as dates and amounts their account was credited by, and allow reports to be generated and sent to them. However, that could occupy several chapters itself! Checkout process consideration This requires an important consideration when we get to the checkout stage: we must take into account any credit stored on a customer's account. If they have credit, it should be deducted from any of their own orders, to ensure they can spend the commission they earned. Summary We now have a number of useful features to encourage new orders and customers to our store, as well as encouraging existing customers to promote our store through afliate codes. This included creating a voucher code system which supported vouchers that deducted a percentage from the order total, vouchers that deducted a xed amount from the order total, and vouchers that altered the cost of shipping to the customer. These vouchers were able to take into account expiry dates, limited-use options, and the current cost of the customer's basket. Taking this forward, we looked at how to extend our framework to allow customers to purchase vouchers as gifts for other customers, as well as how to extend our store to support referral bonuses and incentives for our customers. Now we can move onto the most important stage of our store: payments. Without payments, our store would not actually trade. This material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 . material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Chapter 9 [ 219 ] Reducing the number of codes available One feature. material is copyright and is licensed for the sole use by jackie tracey on 23rd February 2010 953 Quincy Drive, , Brick, , 08724 Discounts, Vouchers, and Referrals [ 220 ] Product variations The. purchasable voucher code product, with variations that increase the price in increments, perhaps of $5. Required additional functionality We only need to add additional functionality to this if we