Skip to content

Commit

Permalink
name key -> unique key for promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Kerin committed Oct 14, 2013
1 parent 0f8c76c commit b6ac5ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Model_Purchase extends Kohana_Model_Purchase {
));
}
}
```php
```

And you'll need to add actual promotions to the database. The promtion modle uses single table inheritence to have a different class for each promotion. Each of these has to define "applies_to" and "price_for_purchase_item" which your promotions will have to implement. There is also the ``Model_Promotion_Promocode`` class which gives the promotion the ability to use promo codes which exhaust themselves when are used.

Expand Down
12 changes: 11 additions & 1 deletion classes/Kohana/Model/Promotion.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class Kohana_Model_Promotion extends Jam_Model implements Sellable {
public static function initialize(Jam_Meta $meta)
{
$meta
->name_key('identifier')
->unique_key(function($key){
return is_numeric($key) ? 'id' : 'identifier';
})
->table('promotions')
->associations(array(
'purchase_items' => Jam::association('hasmany', array(
Expand Down Expand Up @@ -59,6 +61,10 @@ public function applies_to(Model_Store_Purchase $purchase)
throw new Kohana_Exception('Not a valid promotion');
}

/**
* Build a purchase item for this promotion (inverseof "reference")
* @return Model_Purchase_Item
*/
public function build_purchase_item()
{
return $this->purchase_items->build(array(
Expand All @@ -73,6 +79,10 @@ public function currency()
return $this->currency;
}

/**
* If the promotion applies to the store_purchase - add a purchase_item for this promotion, otherwise remove the associated purchase item
* @param Model_Store_Purchase $store_purchase
*/
public function update_store_purchase(Model_Store_Purchase $store_purchase)
{
$promo_item = $this->build_purchase_item();
Expand Down

0 comments on commit b6ac5ec

Please sign in to comment.