1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Sitewards_B2BProfessional_Model_Observer |
5
|
|
|
* - Observer containing the following event methods |
6
|
|
|
* - catalog_product_load_after - displays the correct stock status text on all products, |
7
|
|
|
* - catalog_product_is_salable_after - remove add to cart buttons, |
8
|
|
|
* - catalog_product_type_prepare_full_options - stop product being added to cart via url, |
9
|
|
|
* - core_block_abstract_to_html_after - remove price from product pages, |
10
|
|
|
* |
11
|
|
|
* @category Sitewards |
12
|
|
|
* @package Sitewards_B2BProfessional |
13
|
|
|
* @copyright Copyright (c) 2014 Sitewards GmbH (http://www.sitewards.com/) |
14
|
|
|
*/ |
15
|
|
|
class Sitewards_B2BProfessional_Model_Observer |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* The last product Id |
19
|
|
|
* |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected static $iLastProductId = 0; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The b2b prof helper class |
26
|
|
|
* |
27
|
|
|
* @var Sitewards_B2BProfessional_Helper_Data |
28
|
|
|
*/ |
29
|
|
|
protected $oB2BHelper; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Init the helper object |
33
|
|
|
*/ |
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$this->oB2BHelper = Mage::helper('sitewards_b2bprofessional'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check if the extension is active |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
protected function isExtensionActive() |
45
|
|
|
{ |
46
|
|
|
return $this->oB2BHelper->isExtensionActive(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Check to see if the product being added to the cart can be bought |
51
|
|
|
* |
52
|
|
|
* @param Varien_Event_Observer $oObserver |
53
|
|
|
* @throws Mage_Catalog_Exception |
54
|
|
|
*/ |
55
|
|
|
public function catalogProductTypePrepareFullOptions(Varien_Event_Observer $oObserver) |
56
|
|
|
{ |
57
|
|
|
if ($this->isExtensionActive()) { |
58
|
|
|
$oProduct = $this->getEventsProduct($oObserver); |
59
|
|
|
|
60
|
|
|
if ($this->oB2BHelper->isProductActive($oProduct) === false) { |
61
|
|
|
throw new Mage_Catalog_Exception($this->oB2BHelper->__('Your account is not allowed to access this store.')); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Replace the price information with the desired message |
68
|
|
|
* |
69
|
|
|
* @param Varien_Event_Observer $oObserver |
70
|
|
|
*/ |
71
|
|
|
public function coreBlockAbstractToHtmlAfter(Varien_Event_Observer $oObserver) |
72
|
|
|
{ |
73
|
|
|
if ($this->isExtensionActive()) { |
74
|
|
|
$oBlock = $oObserver->getData('block'); |
75
|
|
|
$oTransport = $oObserver->getData('transport'); |
76
|
|
|
|
77
|
|
|
/* |
78
|
|
|
* Check to see if we should remove the product price |
79
|
|
|
*/ |
80
|
|
|
if ($this->isExactlyPriceBlock($oBlock)) { |
81
|
|
|
$this->transformPriceBlock($oBlock, $oTransport); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check to see if we should remove the add-to-cart button |
86
|
|
|
*/ |
87
|
|
|
if ($this->isExactlyAddToCartBlock($oBlock)) { |
88
|
|
|
$oTransport->setHtml(''); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check to see if the user will need to be redirected to the login page or another saved via the admin |
95
|
|
|
* |
96
|
|
|
* @param Varien_Event_Observer $oObserver |
97
|
|
|
*/ |
98
|
|
|
public function controllerActionPredispatch(Varien_Event_Observer $oObserver) |
99
|
|
|
{ |
100
|
|
|
if ($this->isExtensionActive()) { |
101
|
|
|
/* @var $oControllerAction Mage_Core_Controller_Front_Action */ |
102
|
|
|
$oControllerAction = $oObserver->getData('controller_action'); |
103
|
|
|
|
104
|
|
|
/* @var $oB2BCustomerHelper Sitewards_B2BProfessional_Helper_Customer */ |
105
|
|
|
$oB2BCustomerHelper = Mage::helper('sitewards_b2bprofessional/customer'); |
106
|
|
|
|
107
|
|
|
/* |
108
|
|
|
* Check to see if the system requires a login |
109
|
|
|
* And there is no logged in user |
110
|
|
|
*/ |
111
|
|
|
if ($oB2BCustomerHelper->isLoginRequired() == true && !$oB2BCustomerHelper->isCustomerLoggedIn()) { |
|
|
|
|
112
|
|
|
Mage::helper('sitewards_b2bprofessional/redirects')->performRedirect($oControllerAction); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Remove the price option from the order by options |
119
|
|
|
* |
120
|
|
|
* @param Varien_Event_Observer $oObserver |
121
|
|
|
*/ |
122
|
|
|
public function coreBlockAbstractToHtmlBefore(Varien_Event_Observer $oObserver) |
123
|
|
|
{ |
124
|
|
|
if ($this->isExtensionActive()) { |
125
|
|
|
$oBlock = $oObserver->getData('block'); |
126
|
|
|
|
127
|
|
|
if ($oBlock instanceof Mage_Catalog_Block_Product_List_Toolbar) { |
|
|
|
|
128
|
|
|
$oBlock->removeOrderFromAvailableOrders('price'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* If we have a Mage_Catalog_Block_Layer_View |
135
|
|
|
* - remove the price attribute |
136
|
|
|
* |
137
|
|
|
* @param Varien_Event_Observer $oObserver |
138
|
|
|
*/ |
139
|
|
|
public function coreLayoutBlockCreateAfter(Varien_Event_Observer $oObserver) |
140
|
|
|
{ |
141
|
|
|
if ($this->isExtensionActive()) { |
142
|
|
|
$oBlock = $oObserver->getData('block'); |
143
|
|
|
if ($oBlock instanceof Mage_Catalog_Block_Layer_View) { |
|
|
|
|
144
|
|
|
$aCategoryOptions = $this->getCategoryFilters($oBlock); |
145
|
|
|
|
146
|
|
|
if ($this->oB2BHelper->hasEnabledCategories($aCategoryOptions)) { |
147
|
|
|
$this->removePriceFilter($oBlock); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Checks if the product can be added to the cart and if not, adds a dummy required |
155
|
|
|
* option in order to replace the add-to-cart button's url with the view-details url |
156
|
|
|
* |
157
|
|
|
* @param Varien_Event_Observer $oObserver |
158
|
|
|
*/ |
159
|
|
|
public function catalogBlockProductListCollectionLoadAfter(Varien_Event_Observer $oObserver) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$oProductCollection = $oObserver->getCollection(); |
162
|
|
|
$oDummyOption = Mage::getModel('catalog/product_option'); |
163
|
|
|
foreach ($oProductCollection as $oProduct) { |
164
|
|
|
if ($this->oB2BHelper->isProductActive($oProduct) === false) { |
165
|
|
|
$oProduct->setRequiredOptions(array($oDummyOption)); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
return $oProductCollection; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* checks if the block represents a price block |
173
|
|
|
* |
174
|
|
|
* @param Mage_Core_Block_Abstract $oBlock |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
protected function isExactlyPriceBlock($oBlock) |
178
|
|
|
{ |
179
|
|
|
return $oBlock && $this->oB2BHelper->isBlockPriceBlock($oBlock); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* checks if the block represents an add-to-cart block |
184
|
|
|
* |
185
|
|
|
* @param Mage_Core_Block_Abstract $oBlock |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
protected function isExactlyAddToCartBlock($oBlock) |
189
|
|
|
{ |
190
|
|
|
return $this->oB2BHelper->isAddToCartBlockAndHidden($oBlock); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* From a block get all the category filters when set |
195
|
|
|
* |
196
|
|
|
* @param Mage_Catalog_Block_Layer_View $oBlock |
197
|
|
|
* @return int[] |
198
|
|
|
*/ |
199
|
|
|
protected function getCategoryFilters($oBlock) |
200
|
|
|
{ |
201
|
|
|
/* @var $oCategoryFilter Mage_Catalog_Block_Layer_Filter_Category */ |
202
|
|
|
$oCategoryFilter = $oBlock->getChild('category_filter'); |
203
|
|
|
$aCategoryOptions = array(); |
204
|
|
|
if ($oCategoryFilter instanceof Mage_Catalog_Block_Layer_Filter_Category) { |
|
|
|
|
205
|
|
|
$oCategories = $oCategoryFilter->getItems(); |
206
|
|
|
foreach ($oCategories as $oCategory) { |
207
|
|
|
/* @var $oCategory Mage_Catalog_Model_Layer_Filter_Item */ |
208
|
|
|
$iCategoryId = $oCategory->getValue(); |
209
|
|
|
$aCategoryOptions[] = $iCategoryId; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (empty($aCategoryOptions)) { |
213
|
|
|
return $this->getDefaultCategoryOptions(); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
return $aCategoryOptions; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Return the default category, |
221
|
|
|
* - either from the filter, |
222
|
|
|
* - the current category, |
223
|
|
|
* - or the root category |
224
|
|
|
* |
225
|
|
|
* @return int[] |
226
|
|
|
*/ |
227
|
|
|
protected function getDefaultCategoryOptions() |
228
|
|
|
{ |
229
|
|
|
$aCategoryOptions = array(); |
230
|
|
|
|
231
|
|
|
/* @var $oCategory Mage_Catalog_Model_Category */ |
232
|
|
|
$oCategory = Mage::registry('current_category_filter'); |
233
|
|
|
if ($oCategory === null) { |
234
|
|
|
$oCategory = Mage::registry('current_category'); |
235
|
|
|
if ($oCategory === null) { |
236
|
|
|
$oCategory = Mage::getModel('catalog/category')->load( |
237
|
|
|
Mage::app()->getStore()->getRootCategoryId() |
238
|
|
|
); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
$aCategoryOptions[] = $oCategory->getId(); |
242
|
|
|
return $aCategoryOptions; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Remove the price filter options from the filterable attributes |
247
|
|
|
* |
248
|
|
|
* @param Mage_Catalog_Block_Layer_View $oBlock |
249
|
|
|
*/ |
250
|
|
|
protected function removePriceFilter($oBlock) |
251
|
|
|
{ |
252
|
|
|
$aFilterableAttributes = $oBlock->getData('_filterable_attributes'); |
253
|
|
|
$aNewFilterableAttributes = array(); |
254
|
|
|
|
255
|
|
|
if (is_array($aFilterableAttributes) || is_object($aFilterableAttributes)) { |
256
|
|
|
foreach ($aFilterableAttributes as $oFilterableAttribute) { |
257
|
|
|
if ($oFilterableAttribute->getAttributeCode() != 'price') { |
258
|
|
|
$aNewFilterableAttributes[] = $oFilterableAttribute; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
$oBlock->setData('_filterable_attributes', $aNewFilterableAttributes); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Set type id to combined to stop tax being displayed via Symmetrics_TweaksGerman_Block_Tax |
267
|
|
|
* |
268
|
|
|
* @param Mage_Catalog_Model_Product $oProduct |
269
|
|
|
*/ |
270
|
|
|
protected function setSymmetricsProductType(Mage_Catalog_Model_Product $oProduct) |
271
|
|
|
{ |
272
|
|
|
if ( |
273
|
|
|
Mage::helper('core')->isModuleEnabled('Symmetrics_TweaksGerman') |
274
|
|
|
&& $oProduct->getTypeId() == 'bundle' |
275
|
|
|
) { |
276
|
|
|
$oProduct->setTypeId('combined'); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* For a given product price block check if the product is active |
282
|
|
|
* - if it is active then set the price html as the default message |
283
|
|
|
* |
284
|
|
|
* @param Mage_Catalog_Block_Product_Price $oBlock |
285
|
|
|
* @param Varien_Object $oTransport |
286
|
|
|
*/ |
287
|
|
|
protected function transformPriceBlock($oBlock, $oTransport) |
288
|
|
|
{ |
289
|
|
|
$oProduct = $oBlock->getProduct(); |
290
|
|
|
$iCurrentProductId = $oProduct->getId(); |
291
|
|
|
|
292
|
|
|
if ($this->oB2BHelper->isProductActive($oProduct) === false) { |
293
|
|
|
// To stop duplicate information being displayed validate that we only do this once per product |
294
|
|
|
if ($iCurrentProductId !== self::$iLastProductId) { |
295
|
|
|
self::$iLastProductId = $iCurrentProductId; |
296
|
|
|
$oTransport->setHtml($this->oB2BHelper->getLoginMessage()); |
297
|
|
|
} else { |
298
|
|
|
$oTransport->setHtml(''); |
299
|
|
|
} |
300
|
|
|
$this->setSymmetricsProductType($oProduct); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Get the product attached to an event |
306
|
|
|
* |
307
|
|
|
* @param Varien_Event_Observer $oObserver |
308
|
|
|
* @return Mage_Catalog_Model_Product |
309
|
|
|
*/ |
310
|
|
|
protected function getEventsProduct(Varien_Event_Observer $oObserver) |
311
|
|
|
{ |
312
|
|
|
return $oObserver->getProduct(); |
313
|
|
|
} |
314
|
|
|
} |
|
|
|
|
315
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.