1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31:
32: class Mage_Catalog_Helper_Product extends Mage_Core_Helper_Url
33: {
34: const XML_PATH_PRODUCT_URL_SUFFIX = 'catalog/seo/product_url_suffix';
35: const XML_PATH_PRODUCT_URL_USE_CATEGORY = 'catalog/seo/product_use_categories';
36: const XML_PATH_USE_PRODUCT_CANONICAL_TAG = 'catalog/seo/product_canonical_tag';
37:
38: 39: 40: 41: 42:
43: protected $_skipSaleableCheck = false;
44:
45: 46: 47: 48: 49:
50: protected $_productUrlSuffix = array();
51:
52: protected $_statuses;
53:
54: protected $_priceBlock;
55:
56: 57: 58: 59: 60: 61:
62: public function getProductUrl($product)
63: {
64: if ($product instanceof Mage_Catalog_Model_Product) {
65: return $product->getProductUrl();
66: }
67: elseif (is_numeric($product)) {
68: return Mage::getModel('catalog/product')->load($product)->getProductUrl();
69: }
70: return false;
71: }
72:
73: 74: 75: 76: 77: 78:
79: public function getPrice($product)
80: {
81: return $product->getPrice();
82: }
83:
84: 85: 86: 87: 88: 89:
90: public function getFinalPrice($product)
91: {
92: return $product->getFinalPrice();
93: }
94:
95: 96: 97: 98: 99:
100: public function getImageUrl($product)
101: {
102: $url = false;
103: if (!$product->getImage()) {
104: $url = Mage::getDesign()->getSkinUrl('images/no_image.jpg');
105: }
106: elseif ($attribute = $product->getResource()->getAttribute('image')) {
107: $url = $attribute->getFrontend()->getUrl($product);
108: }
109: return $url;
110: }
111:
112: 113: 114: 115: 116:
117: public function getSmallImageUrl($product)
118: {
119: $url = false;
120: if (!$product->getSmallImage()) {
121: $url = Mage::getDesign()->getSkinUrl('images/no_image.jpg');
122: }
123: elseif ($attribute = $product->getResource()->getAttribute('small_image')) {
124: $url = $attribute->getFrontend()->getUrl($product);
125: }
126: return $url;
127: }
128:
129: 130: 131: 132: 133:
134: public function getThumbnailUrl($product)
135: {
136: return '';
137: }
138:
139: public function getEmailToFriendUrl($product)
140: {
141: $categoryId = null;
142: if ($category = Mage::registry('current_category')) {
143: $categoryId = $category->getId();
144: }
145: return $this->_getUrl('sendfriend/product/send', array(
146: 'id' => $product->getId(),
147: 'cat_id' => $categoryId
148: ));
149: }
150:
151: public function getStatuses()
152: {
153: if(is_null($this->_statuses)) {
154: $this->_statuses = array();
155: }
156:
157: return $this->_statuses;
158: }
159:
160: 161: 162: 163: 164: 165:
166: public function canShow($product, $where = 'catalog')
167: {
168: if (is_int($product)) {
169: $product = Mage::getModel('catalog/product')->load($product);
170: }
171:
172:
173:
174: if (!$product->getId()) {
175: return false;
176: }
177:
178: return $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility();
179: }
180:
181: 182: 183: 184: 185: 186:
187: public function getProductUrlSuffix($storeId = null)
188: {
189: if (is_null($storeId)) {
190: $storeId = Mage::app()->getStore()->getId();
191: }
192:
193: if (!isset($this->_productUrlSuffix[$storeId])) {
194: $this->_productUrlSuffix[$storeId] = Mage::getStoreConfig(self::XML_PATH_PRODUCT_URL_SUFFIX, $storeId);
195: }
196: return $this->_productUrlSuffix[$storeId];
197: }
198:
199: 200: 201: 202: 203: 204:
205: public function canUseCanonicalTag($store = null)
206: {
207: return Mage::getStoreConfig(self::XML_PATH_USE_PRODUCT_CANONICAL_TAG, $store);
208: }
209:
210: 211: 212: 213: 214: 215: 216: 217:
218: public function getAttributeInputTypes($inputType = null)
219: {
220: 221: 222:
223: $inputTypes = array(
224: 'multiselect' => array(
225: 'backend_model' => 'eav/entity_attribute_backend_array'
226: ),
227: 'boolean' => array(
228: 'source_model' => 'eav/entity_attribute_source_boolean'
229: )
230: );
231:
232: if (is_null($inputType)) {
233: return $inputTypes;
234: } else if (isset($inputTypes[$inputType])) {
235: return $inputTypes[$inputType];
236: }
237: return array();
238: }
239:
240: 241: 242: 243: 244: 245:
246: public function getAttributeBackendModelByInputType($inputType)
247: {
248: $inputTypes = $this->getAttributeInputTypes();
249: if (!empty($inputTypes[$inputType]['backend_model'])) {
250: return $inputTypes[$inputType]['backend_model'];
251: }
252: return null;
253: }
254:
255: 256: 257: 258: 259: 260:
261: public function getAttributeSourceModelByInputType($inputType)
262: {
263: $inputTypes = $this->getAttributeInputTypes();
264: if (!empty($inputTypes[$inputType]['source_model'])) {
265: return $inputTypes[$inputType]['source_model'];
266: }
267: return null;
268: }
269:
270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281:
282: public function initProduct($productId, $controller, $params = null)
283: {
284:
285: if (!$params) {
286: $params = new Varien_Object();
287: }
288:
289:
290: Mage::dispatchEvent('catalog_controller_product_init_before', array(
291: 'controller_action' => $controller,
292: 'params' => $params,
293: ));
294:
295: if (!$productId) {
296: return false;
297: }
298:
299: $product = Mage::getModel('catalog/product')
300: ->setStoreId(Mage::app()->getStore()->getId())
301: ->load($productId);
302:
303: if (!$this->canShow($product)) {
304: return false;
305: }
306: if (!in_array(Mage::app()->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
307: return false;
308: }
309:
310:
311: $categoryId = $params->getCategoryId();
312: if (!$categoryId && ($categoryId !== false)) {
313: $lastId = Mage::getSingleton('catalog/session')->getLastVisitedCategoryId();
314: if ($product->canBeShowInCategory($lastId)) {
315: $categoryId = $lastId;
316: }
317: } elseif (!$product->canBeShowInCategory($categoryId)) {
318: $categoryId = null;
319: }
320:
321: if ($categoryId) {
322: $category = Mage::getModel('catalog/category')->load($categoryId);
323: $product->setCategory($category);
324: Mage::register('current_category', $category);
325: }
326:
327:
328: Mage::register('current_product', $product);
329: Mage::register('product', $product);
330:
331: try {
332: Mage::dispatchEvent('catalog_controller_product_init', array('product' => $product));
333: Mage::dispatchEvent('catalog_controller_product_init_after',
334: array('product' => $product,
335: 'controller_action' => $controller
336: )
337: );
338: } catch (Mage_Core_Exception $e) {
339: Mage::logException($e);
340: return false;
341: }
342:
343: return $product;
344: }
345:
346: 347: 348: 349: 350: 351: 352: 353:
354: public function prepareProductOptions($product, $buyRequest)
355: {
356: $optionValues = $product->processBuyRequest($buyRequest);
357: $optionValues->setQty($buyRequest->getQty());
358: $product->setPreconfiguredValues($optionValues);
359:
360: return $this;
361: }
362:
363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376:
377: public function addParamsToBuyRequest($buyRequest, $params)
378: {
379: if (is_array($buyRequest)) {
380: $buyRequest = new Varien_Object($buyRequest);
381: }
382: if (is_array($params)) {
383: $params = new Varien_Object($params);
384: }
385:
386:
387:
388: $currentConfig = $params->getCurrentConfig();
389: if ($currentConfig) {
390: if (is_array($currentConfig)) {
391: $params->setCurrentConfig(new Varien_Object($currentConfig));
392: } else if (!($currentConfig instanceof Varien_Object)) {
393: $params->unsCurrentConfig();
394: }
395: }
396:
397: 398: 399: 400:
401: $processingParams = $buyRequest->getData('_processing_params');
402: if (!$processingParams || !($processingParams instanceof Varien_Object)) {
403: $processingParams = new Varien_Object();
404: $buyRequest->setData('_processing_params', $processingParams);
405: }
406: $processingParams->addData($params->getData());
407:
408: return $buyRequest;
409: }
410:
411: 412: 413: 414: 415: 416: 417: 418:
419: public function getProduct($productId, $store, $identifierType = null)
420: {
421:
422: $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore($store)->getId());
423:
424: $expectedIdType = false;
425: if ($identifierType === null) {
426: if (is_string($productId) && !preg_match("/^[+-]?[1-9][0-9]*$|^0$/", $productId)) {
427: $expectedIdType = 'sku';
428: }
429: }
430:
431: if ($identifierType == 'sku' || $expectedIdType == 'sku') {
432: $idBySku = $product->getIdBySku($productId);
433: if ($idBySku) {
434: $productId = $idBySku;
435: } else if ($identifierType == 'sku') {
436:
437: return $product;
438: }
439: }
440:
441: if ($productId && is_numeric($productId)) {
442: $product->load((int) $productId);
443: }
444:
445: return $product;
446: }
447:
448: 449: 450: 451: 452: 453: 454: 455:
456: public function setSkipSaleableCheck($skipSaleableCheck = false)
457: {
458: $this->_skipSaleableCheck = $skipSaleableCheck;
459: return $this;
460: }
461:
462: 463: 464: 465: 466:
467: public function getSkipSaleableCheck()
468: {
469: return $this->_skipSaleableCheck;
470: }
471: }
472: