1 <?php
2 3 4 5 6
7 class ViewedProductExtention extends Extension {
8
9 static $viewed_cookie_name = 'ViewedProducts';
10
11 private static $viewed_products_count = 20;
12
13 static function set_viewed_products_count($value) {
14 self::$viewed_products_count = $value;
15 }
16
17 18 19 20 21
22 static function is_product_alive($productID) {
23 return Dataobject::get_by_id('Product', (int)$productID);
24 }
25
26 27 28 29
30 static function save_product_cookie($productID) {
31 $products = CookieExtention::get_cookie_data(self::$viewed_cookie_name);
32 if ($products) {
33 $productsArray = explode(',', $products);
34
35
36 $tmp = array();
37 foreach($productsArray as $entry) {
38 if (self::is_product_alive($entry) && ($entry != $productID)) {
39 $tmp[] = $entry;
40 }
41 }
42 $productsArray = $tmp;
43 array_unshift($productsArray, $productID);
44
45
46 while (count($productsArray) > self::$viewed_products_count) {
47 array_pop($productsArray);
48 }
49 $products = implode(',', $productsArray);
50 } else {
51 $products = $productID;
52 }
53 CookieExtention::set_cookie_data(self::$viewed_cookie_name, $products);
54 }
55
56 function index() {
57 if ($this->owner->ClassName == 'Product') {
58 self::save_product_cookie($this->owner->ID);
59 }
60 return $this->owner->defaultAction('index');
61 }
62 }
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-