Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is essentially a one class functionality, really. You'd need a class interface that would look like this</p> <pre><code> class ProductDiscount { /** * Create a NEW discount code and return the instance of * * @param $code string the discount code * @param $discount float price adjustment in % (ex: * @param $options array (optional) an array of options : * 'expire' =&gt; timestamp (optional) * 'limited' =&gt; int (optional) * @return ProductDiscount */ static public function create($code, $discount, $options = NULL); /** * This essentially validate the code, and return the instance of the * discount if the code exists. The method returns null if the discount * is not valid for any reason. If an instance is returned, to apply * the discount, one should invoke the "consume()" method of the instance. * * @param $code string * * @return ProductDiscount|null */ static public function validate($code); private $_code; // string private $_discount; // float private $_expire; // unix timestamp (see time()) or null if unlimited private $_count; // int or null if unlimited private function __construct(); public function getCode(); // return string public function getDiscount(); // return float public function isLimited(); // return bool; true if $_count || $_expire is not null public function getCount(); // returns int; how many of this discount is left or null if unlimited public function getExpireDate();// returns int (unix timestamp) or null if unlimited public function consume(); // apply this discount now public function consumeAll(); // invalidate this discount for future use } </code></pre> <p>The DB table could look like this</p> <pre><code>id UNSIGNED INT(10) NOT NULL AUTOINCREMENT -- (Primary Key) code VARCHAR(12) NOT NULL -- (Indexed, unique) discount UNSIGNED INT(3) NOT NULL -- percent value 0..100 expire DATETIME DEFAULT NULL -- unlimited by default count INT(10) DEFAULT 1 -- can be NULL </code></pre> <hr> <p><strong>Note :</strong> The validation process could just be a simple <code>SELECT</code> statement :</p> <pre><code>SELECT * FROM tblproductdiscount WHERE code = {$code} -- $code = mysql_real_escape_string($code) AND (count IS NULL OR count &gt; 0) AND (expire IS NULL or expire &gt; NOW()) </code></pre> <hr> <p>Then just use this class when validating checkout form. For example, </p> <pre><code>if (!empty($discountCode)) { $discount = ProductDiscount::validate($discountCode); if ($discount !== null) { $price *= (1 - $discount-&gt;getDiscount()); $discount-&gt;consume(); } } </code></pre> <p>Well, that's somewhat how I would do it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload