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: 33: 34:
35: abstract class Mage_Catalog_Block_Product_Abstract extends Mage_Core_Block_Template
36: {
37: protected $_priceBlock = array();
38:
39: 40: 41: 42: 43:
44: protected $_block = 'catalog/product_price';
45:
46: protected $_priceBlockDefaultTemplate = 'catalog/product/price.phtml';
47:
48: protected $_tierPriceDefaultTemplate = 'catalog/product/view/tierprices.phtml';
49:
50: protected $_priceBlockTypes = array();
51:
52: 53: 54: 55: 56:
57: protected $_useLinkForAsLowAs = true;
58:
59: protected $_reviewsHelperBlock;
60:
61: 62: 63: 64: 65:
66: protected $_defaultColumnCount = 3;
67:
68: 69: 70: 71: 72:
73: protected $_columnCountLayoutDepend = array();
74:
75: 76: 77: 78: 79:
80: protected $_mapRenderer = 'msrp';
81:
82: 83: 84: 85: 86: 87: 88: 89:
90: public function getAddToCartUrl($product, $additional = array())
91: {
92: if ($product->getTypeInstance(true)->hasRequiredOptions($product)) {
93: if (!isset($additional['_escape'])) {
94: $additional['_escape'] = true;
95: }
96: if (!isset($additional['_query'])) {
97: $additional['_query'] = array();
98: }
99: $additional['_query']['options'] = 'cart';
100:
101: return $this->getProductUrl($product, $additional);
102: }
103: return $this->helper('checkout/cart')->getAddUrl($product, $additional);
104: }
105:
106: 107: 108: 109: 110: 111: 112: 113: 114:
115: public function getSubmitUrl($product, $additional = array())
116: {
117: $submitRouteData = $this->getData('submit_route_data');
118: if ($submitRouteData) {
119: $route = $submitRouteData['route'];
120: $params = isset($submitRouteData['params']) ? $submitRouteData['params'] : array();
121: $submitUrl = $this->getUrl($route, array_merge($params, $additional));
122: } else {
123: $submitUrl = $this->getAddToCartUrl($product, $additional);
124: }
125: return $submitUrl;
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function getAddToWishlistUrl($product)
135: {
136: return $this->helper('wishlist')->getAddUrl($product);
137: }
138:
139: 140: 141: 142: 143: 144:
145: public function getAddToCompareUrl($product)
146: {
147: return $this->helper('catalog/product_compare')->getAddUrl($product);
148: }
149:
150: public function getMinimalQty($product)
151: {
152: if ($stockItem = $product->getStockItem()) {
153: return ($stockItem->getMinSaleQty() && $stockItem->getMinSaleQty() > 0 ? $stockItem->getMinSaleQty() * 1 : null);
154: }
155: return null;
156: }
157:
158: protected function _getPriceBlock($productTypeId)
159: {
160: if (!isset($this->_priceBlock[$productTypeId])) {
161: $block = $this->_block;
162: if (isset($this->_priceBlockTypes[$productTypeId])) {
163: if ($this->_priceBlockTypes[$productTypeId]['block'] != '') {
164: $block = $this->_priceBlockTypes[$productTypeId]['block'];
165: }
166: }
167: $this->_priceBlock[$productTypeId] = $this->getLayout()->createBlock($block);
168: }
169: return $this->_priceBlock[$productTypeId];
170: }
171:
172: protected function _getPriceBlockTemplate($productTypeId)
173: {
174: if (isset($this->_priceBlockTypes[$productTypeId])) {
175: if ($this->_priceBlockTypes[$productTypeId]['template'] != '') {
176: return $this->_priceBlockTypes[$productTypeId]['template'];
177: }
178: }
179: return $this->_priceBlockDefaultTemplate;
180: }
181:
182:
183: 184: 185: 186: 187: 188:
189: public function _preparePriceRenderer($productType)
190: {
191: return $this->_getPriceBlock($productType)
192: ->setTemplate($this->_getPriceBlockTemplate($productType))
193: ->setUseLinkForAsLowAs($this->_useLinkForAsLowAs);
194: }
195:
196: 197: 198: 199: 200: 201: 202: 203:
204: public function getPriceHtml($product, $displayMinimalPrice = false, $idSuffix = '')
205: {
206: $type_id = $product->getTypeId();
207: if (Mage::helper('catalog')->canApplyMsrp($product)) {
208: $realPriceHtml = $this->_preparePriceRenderer($type_id)
209: ->setProduct($product)
210: ->setDisplayMinimalPrice($displayMinimalPrice)
211: ->setIdSuffix($idSuffix)
212: ->toHtml();
213: $product->setAddToCartUrl($this->getAddToCartUrl($product));
214: $product->setRealPriceHtml($realPriceHtml);
215: $type_id = $this->_mapRenderer;
216: }
217:
218: return $this->_preparePriceRenderer($type_id)
219: ->setProduct($product)
220: ->setDisplayMinimalPrice($displayMinimalPrice)
221: ->setIdSuffix($idSuffix)
222: ->toHtml();
223: }
224:
225: 226: 227: 228: 229: 230: 231:
232: public function addPriceBlockType($type, $block = '', $template = '')
233: {
234: if ($type) {
235: $this->_priceBlockTypes[$type] = array(
236: 'block' => $block,
237: 'template' => $template
238: );
239: }
240: }
241:
242: 243: 244: 245: 246: 247: 248: 249:
250: public function getReviewsSummaryHtml(Mage_Catalog_Model_Product $product, $templateType = false,
251: $displayIfNoReviews = false)
252: {
253: if ($this->_initReviewsHelperBlock()) {
254: return $this->_reviewsHelperBlock->getSummaryHtml($product, $templateType, $displayIfNoReviews);
255: }
256:
257: return '';
258: }
259:
260: 261: 262: 263: 264: 265: 266:
267: public function addReviewSummaryTemplate($type, $template)
268: {
269: if ($this->_initReviewsHelperBlock()) {
270: $this->_reviewsHelperBlock->addTemplate($type, $template);
271: }
272:
273: return '';
274: }
275:
276: 277: 278: 279: 280:
281: protected function _initReviewsHelperBlock()
282: {
283: if (!$this->_reviewsHelperBlock) {
284: if (!Mage::helper('catalog')->isModuleEnabled('Mage_Review')) {
285: return false;
286: } else {
287: $this->_reviewsHelperBlock = $this->getLayout()->createBlock('review/helper');
288: }
289: }
290:
291: return true;
292: }
293:
294: 295: 296: 297: 298:
299: public function getProduct()
300: {
301: if (!$this->hasData('product')) {
302: $this->setData('product', Mage::registry('product'));
303: }
304: return $this->getData('product');
305: }
306:
307: public function getTierPriceTemplate()
308: {
309: if (!$this->hasData('tier_price_template')) {
310: return $this->_tierPriceDefaultTemplate;
311: }
312:
313: return $this->getData('tier_price_template');
314: }
315: 316: 317: 318: 319: 320:
321: public function getTierPriceHtml($product = null)
322: {
323: if (is_null($product)) {
324: $product = $this->getProduct();
325: }
326: return $this->_getPriceBlock($product->getTypeId())
327: ->setTemplate($this->getTierPriceTemplate())
328: ->setProduct($product)
329: ->setInGrouped($this->getProduct()->isGrouped())
330: ->toHtml();
331: }
332:
333: 334: 335: 336: 337: 338:
339: public function getTierPrices($product = null)
340: {
341: if (is_null($product)) {
342: $product = $this->getProduct();
343: }
344: $prices = $product->getFormatedTierPrice();
345:
346: $res = array();
347: if (is_array($prices)) {
348: foreach ($prices as $price) {
349: $price['price_qty'] = $price['price_qty'] * 1;
350:
351: $_productPrice = $product->getPrice();
352: if ($_productPrice != $product->getFinalPrice()) {
353: $_productPrice = $product->getFinalPrice();
354: }
355:
356:
357: $groupPrice = $product->getGroupPrice();
358: if ($_productPrice > $groupPrice) {
359: $_productPrice = $groupPrice;
360: }
361:
362: if ($price['price'] < $_productPrice) {
363: $price['savePercent'] = ceil(100 - ((100 / $_productPrice) * $price['price']));
364:
365: $tierPrice = Mage::app()->getStore()->convertPrice(
366: Mage::helper('tax')->getPrice($product, $price['website_price'])
367: );
368: $price['formated_price'] = Mage::app()->getStore()->formatPrice($tierPrice);
369: $price['formated_price_incl_tax'] = Mage::app()->getStore()->formatPrice(
370: Mage::app()->getStore()->convertPrice(
371: Mage::helper('tax')->getPrice($product, $price['website_price'], true)
372: )
373: );
374:
375: if (Mage::helper('catalog')->canApplyMsrp($product)) {
376: $oldPrice = $product->getFinalPrice();
377: $product->setPriceCalculation(false);
378: $product->setPrice($tierPrice);
379: $product->setFinalPrice($tierPrice);
380:
381: $this->getPriceHtml($product);
382: $product->setPriceCalculation(true);
383:
384: $price['real_price_html'] = $product->getRealPriceHtml();
385: $product->setFinalPrice($oldPrice);
386: }
387:
388: $res[] = $price;
389: }
390: }
391: }
392:
393: return $res;
394: }
395:
396: 397: 398: 399: 400: 401: 402: 403:
404: protected function _addProductAttributesAndPrices(Mage_Catalog_Model_Resource_Product_Collection $collection)
405: {
406: return $collection
407: ->addMinimalPrice()
408: ->addFinalPrice()
409: ->addTaxPercents()
410: ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
411: ->addUrlRewrite();
412: }
413:
414: 415: 416: 417: 418: 419: 420: 421:
422: public function getImageLabel($product=null, $mediaAttributeCode='image')
423: {
424: if (is_null($product)) {
425: $product = $this->getProduct();
426: }
427:
428: $label = $product->getData($mediaAttributeCode.'_label');
429: if (empty($label)) {
430: $label = $product->getName();
431: }
432:
433: return $label;
434: }
435:
436: 437: 438: 439: 440: 441: 442:
443: public function getProductUrl($product, $additional = array())
444: {
445: if ($this->hasProductUrl($product)) {
446: if (!isset($additional['_escape'])) {
447: $additional['_escape'] = true;
448: }
449: return $product->getUrlModel()->getUrl($product, $additional);
450: }
451:
452: return '#';
453: }
454:
455: 456: 457: 458: 459: 460:
461: public function hasProductUrl($product)
462: {
463: if ($product->getVisibleInSiteVisibilities()) {
464: return true;
465: }
466: if ($product->hasUrlDataObject()) {
467: if (in_array($product->hasUrlDataObject()->getVisibility(), $product->getVisibleInSiteVisibilities())) {
468: return true;
469: }
470: }
471:
472: return false;
473: }
474:
475: 476: 477: 478: 479:
480: public function getColumnCount()
481: {
482: if (!$this->_getData('column_count')) {
483: $pageLayout = $this->getPageLayout();
484: if ($pageLayout && $this->getColumnCountLayoutDepend($pageLayout->getCode())) {
485: $this->setData(
486: 'column_count',
487: $this->getColumnCountLayoutDepend($pageLayout->getCode())
488: );
489: } else {
490: $this->setData('column_count', $this->_defaultColumnCount);
491: }
492: }
493:
494: return (int) $this->_getData('column_count');
495: }
496:
497: 498: 499: 500: 501: 502: 503:
504: public function addColumnCountLayoutDepend($pageLayout, $columnCount)
505: {
506: $this->_columnCountLayoutDepend[$pageLayout] = $columnCount;
507: return $this;
508: }
509:
510: 511: 512: 513: 514: 515:
516: public function removeColumnCountLayoutDepend($pageLayout)
517: {
518: if (isset($this->_columnCountLayoutDepend[$pageLayout])) {
519: unset($this->_columnCountLayoutDepend[$pageLayout]);
520: }
521:
522: return $this;
523: }
524:
525: 526: 527: 528: 529: 530:
531: public function getColumnCountLayoutDepend($pageLayout)
532: {
533: if (isset($this->_columnCountLayoutDepend[$pageLayout])) {
534: return $this->_columnCountLayoutDepend[$pageLayout];
535: }
536:
537: return false;
538: }
539:
540: 541: 542: 543: 544:
545: public function getPageLayout()
546: {
547: return $this->helper('page/layout')->getCurrentPageLayout();
548: }
549:
550: 551: 552: 553: 554: 555:
556: public function getCanShowProductPrice($product)
557: {
558: return $product->getCanShowPrice() !== false;
559: }
560:
561: 562: 563: 564: 565:
566: protected function _prepareLayout()
567: {
568: parent::_prepareLayout();
569:
570:
571: $block = $this->getLayout()->getBlock('catalog_product_price_template');
572: if ($block) {
573: foreach ($block->getPriceBlockTypes() as $type => $priceBlock) {
574: $this->addPriceBlockType($type, $priceBlock['block'], $priceBlock['template']);
575: }
576: }
577:
578: return $this;
579: }
580: }
581: