|
3 | 3 | class PriceWaiter_NYPWidget_Block_Widget extends Mage_Core_Block_Template
|
4 | 4 | {
|
5 | 5 | /**
|
6 |
| - * @return PriceWaiter_NYPWidget_Model_Embed |
| 6 | + * @return array Bits of information used to make up the cache key. |
7 | 7 | */
|
8 |
| - public function getEmbed() |
| 8 | + public function getCacheKeyInfo() |
| 9 | + { |
| 10 | + $items = parent::getCacheKeyInfo(); |
| 11 | + |
| 12 | + if (!is_array($items)) { |
| 13 | + $items = array(); |
| 14 | + } |
| 15 | + |
| 16 | + $items[] = 'PriceWaiter'; |
| 17 | + |
| 18 | + |
| 19 | + // 1. Cache per-customer |
| 20 | + $customer = $this->getCustomer(); |
| 21 | + $items[] = $customer ? $customer->getId() : 0; |
| 22 | + |
| 23 | + // 2. Cache per-customer group |
| 24 | + $items[] = $this->getCustomerGroupId(); |
| 25 | + |
| 26 | + // 3. Cache per-product |
| 27 | + $product = $this->getProduct(); |
| 28 | + $items[] = $product ? $product->getId() : 0; |
| 29 | + |
| 30 | + // 4. Cache per-category |
| 31 | + $category = $this->getCategory(); |
| 32 | + $items[] = $category ? $category->getId() : 0; |
| 33 | + |
| 34 | + return $items; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @return Integer This block's cache lifetime (in seconds). |
| 39 | + */ |
| 40 | + public function getCacheLifetime() |
| 41 | + { |
| 42 | + return 7200; |
| 43 | + } |
| 44 | + |
| 45 | + public function getCacheTags() |
| 46 | + { |
| 47 | + $tags = parent::getCacheTags(); |
| 48 | + if (!is_array($tags)) { |
| 49 | + $tags = array(); |
| 50 | + } |
| 51 | + |
| 52 | + $tags[] = Mage_Catalog_Model_Product::CACHE_TAG; |
| 53 | + $tags[] = Mage_Catalog_Model_Category::CACHE_TAG; |
| 54 | + $tags[] = PriceWaiter_NYPWidget_Helper_Data::CACHE_TAG; |
| 55 | + |
| 56 | + return $tags; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return Mage_Catalog_Model_Category |
| 61 | + */ |
| 62 | + public function getCategory() |
9 | 63 | {
|
10 |
| - // Figure out where + who we are... |
11 |
| - $product = Mage::registry('current_product'); |
12 |
| - $store = Mage::app()->getStore(); |
13 | 64 | $category = Mage::registry('current_category');
|
| 65 | + return $category && $category->getId() ? $category : false; |
| 66 | + } |
14 | 67 |
|
15 |
| - $session = Mage::getSingleton('customer/session'); |
| 68 | + /** |
| 69 | + * @return Mage_Customer_Model_Customer|false |
| 70 | + */ |
| 71 | + public function getCustomer() |
| 72 | + { |
| 73 | + $session = $this->getCustomerSession(); |
16 | 74 | $customer = $session->getCustomer();
|
17 |
| - $customerGroupId = $session->getCustomerGroupId(); |
18 | 75 |
|
19 |
| - // ..and wire up an appropriate embed. |
| 76 | + if ($customer && $customer->getId()) { |
| 77 | + return $customer; |
| 78 | + } |
| 79 | + |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return Number |
| 85 | + */ |
| 86 | + public function getCustomerGroupId() |
| 87 | + { |
| 88 | + $session = $this->getCustomerSession(); |
| 89 | + return $session->getCustomerGroupId(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @return Mage_Customer_Model_Session |
| 94 | + */ |
| 95 | + public function getCustomerSession() |
| 96 | + { |
| 97 | + return Mage::getSingleton('customer/session'); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @return Mage_Catalog_Model_Product|false |
| 102 | + */ |
| 103 | + public function getProduct() |
| 104 | + { |
| 105 | + $product = Mage::registry('current_product'); |
| 106 | + return $product && $product->getId() ? $product : false; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @return Mage_Core_Model_Store |
| 111 | + */ |
| 112 | + public function getStore() |
| 113 | + { |
| 114 | + return Mage::app()->getStore(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return PriceWaiter_NYPWidget_Model_Embed |
| 119 | + */ |
| 120 | + public function getEmbed() |
| 121 | + { |
20 | 122 | return Mage::getModel('nypwidget/embed')
|
21 |
| - ->setProduct($product) |
22 |
| - ->setStore($store) |
23 |
| - ->setCategory($category) |
24 |
| - ->setCustomer($customer->getId() ? $customer : false) |
25 |
| - ->setCustomerGroupId($customerGroupId); |
| 123 | + ->setProduct($this->getProduct()) |
| 124 | + ->setStore($this->getStore()) |
| 125 | + ->setCategory($this->getCategory()) |
| 126 | + ->setCustomer($this->getCustomer()) |
| 127 | + ->setCustomerGroupId($this->getCustomerGroupId()); |
26 | 128 | }
|
27 | 129 | }
|
0 commit comments