<?php
/* In the Shopping Cart Definition Page */
require_once('addModule.trait.php');
class shoppingCart{
use addModules;
private $products;
public function __construct(){
$this->defineModuleType('getProductFromDB', 'single');
$this->defineModuleType('checkVoucherCode', 'multiple');
//initialize shopping cart
}
public function addVoucherDiscountModule($module){
$this->addModule('checkVoucherCode', $module);
}
public function setProductDataModule($module){
$this->addModule('getProductFromDB', $module);
}
public function addItem($partNo){
if(!isset($this->products[$partNo])) {
$this->products[$partNo] = $this->runModules('getProductFromDB', $partNo)[0];
}
//run add item code
}
//...
public function checkVoucherCode($voucherCode){
foreach($this->yieldModuleResults($voucherCode) as $results){
if($results['validCode'] != true) continue;
else {
//calculate voucher discount
}
}
}
}
/* In the Shopping Cart Setup Page */
$cart = new shoppingCart();
$cart->setProductDataModule('getProductFromDatabase');
$cart->addVoucherDiscountModule(function($inputCode){
$voucherCodes = getVoucherCodesFromDB();
foreach($voucherCodes as $code){
if($code == strtolower($inputCode)){
return array(
'validCode' => true,
'discount' => getVoucherCodeDiscount($code),
);
}
}
return array('validCode' => false);
});
|