PageRenderTime 20ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Checkout/Block/Cart/Item/Renderer.php

https://github.com/gryzz/crystal_magento
PHP | 353 lines | 192 code | 32 blank | 129 comment | 26 complexity | 4de07dd34d2160cb85e8a42c4c66f455 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Checkout
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Shopping cart item render block
  28. *
  29. * @category Mage
  30. * @package Mage_Checkout
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Checkout_Block_Cart_Item_Renderer extends Mage_Core_Block_Template
  34. {
  35. protected $_item;
  36. protected $_productUrl = null;
  37. protected $_productThumbnail = null;
  38. /**
  39. * Set item for render
  40. *
  41. * @param Mage_Sales_Model_Quote_Item $item
  42. * @return Mage_Checkout_Block_Cart_Item_Renderer
  43. */
  44. public function setItem(Mage_Sales_Model_Quote_Item_Abstract $item)
  45. {
  46. $this->_item = $item;
  47. return $this;
  48. }
  49. /**
  50. * Get quote item
  51. *
  52. * @return Mage_Sales_Model_Quote_Item
  53. */
  54. public function getItem()
  55. {
  56. return $this->_item;
  57. }
  58. /**
  59. * Get item product
  60. *
  61. * @return Mage_Catalog_Model_Product
  62. */
  63. public function getProduct()
  64. {
  65. return $this->getItem()->getProduct();
  66. }
  67. public function overrideProductThumbnail($productThumbnail)
  68. {
  69. $this->_productThumbnail = $productThumbnail;
  70. return $this;
  71. }
  72. /**
  73. * Get product thumbnail image
  74. *
  75. * @return Mage_Catalog_Model_Product_Image
  76. */
  77. public function getProductThumbnail()
  78. {
  79. if (!is_null($this->_productThumbnail)) {
  80. return $this->_productThumbnail;
  81. }
  82. return $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail');
  83. }
  84. public function overrideProductUrl($productUrl)
  85. {
  86. $this->_productUrl = $productUrl;
  87. return $this;
  88. }
  89. /**
  90. * Check Product has URL
  91. *
  92. * @return this
  93. */
  94. public function hasProductUrl()
  95. {
  96. if ($this->_productUrl || $this->getItem()->getRedirectUrl()) {
  97. return true;
  98. }
  99. $product = $this->getProduct();
  100. $option = $this->getItem()->getOptionByCode('product_type');
  101. if ($option) {
  102. $product = $option->getProduct();
  103. }
  104. if ($product->isVisibleInSiteVisibility()) {
  105. return true;
  106. }
  107. else {
  108. if ($product->hasUrlDataObject()) {
  109. $data = $product->getUrlDataObject();
  110. if (in_array($data->getVisibility(), $product->getVisibleInSiteVisibilities())) {
  111. return true;
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. /**
  118. * Retrieve URL to item Product
  119. *
  120. * @return string
  121. */
  122. public function getProductUrl()
  123. {
  124. if (!is_null($this->_productUrl)) {
  125. return $this->_productUrl;
  126. }
  127. if ($this->getItem()->getRedirectUrl()) {
  128. return $this->getItem()->getRedirectUrl();
  129. }
  130. $product = $this->getProduct();
  131. $option = $this->getItem()->getOptionByCode('product_type');
  132. if ($option) {
  133. $product = $option->getProduct();
  134. }
  135. return $product->getUrlModel()->getUrl($product);
  136. }
  137. /**
  138. * Get item product name
  139. *
  140. * @return string
  141. */
  142. public function getProductName()
  143. {
  144. return $this->getProduct()->getName();
  145. }
  146. /**
  147. * Get product customize options
  148. *
  149. * @return array || false
  150. */
  151. public function getProductOptions()
  152. {
  153. $options = array();
  154. if ($optionIds = $this->getItem()->getOptionByCode('option_ids')) {
  155. $options = array();
  156. foreach (explode(',', $optionIds->getValue()) as $optionId) {
  157. if ($option = $this->getProduct()->getOptionById($optionId)) {
  158. $quoteItemOption = $this->getItem()->getOptionByCode('option_' . $option->getId());
  159. $group = $option->groupFactory($option->getType())
  160. ->setOption($option)
  161. ->setQuoteItemOption($quoteItemOption);
  162. $options[] = array(
  163. 'label' => $option->getTitle(),
  164. 'value' => $group->getFormattedOptionValue($quoteItemOption->getValue()),
  165. 'print_value' => $group->getPrintableOptionValue($quoteItemOption->getValue()),
  166. 'option_id' => $option->getId(),
  167. 'option_type' => $option->getType(),
  168. 'custom_view' => $group->isCustomizedView()
  169. );
  170. }
  171. }
  172. }
  173. if ($addOptions = $this->getItem()->getOptionByCode('additional_options')) {
  174. $options = array_merge($options, unserialize($addOptions->getValue()));
  175. }
  176. return $options;
  177. }
  178. /**
  179. * Get list of all otions for product
  180. *
  181. * @return array
  182. */
  183. public function getOptionList()
  184. {
  185. return $this->getProductOptions();
  186. }
  187. /**
  188. * Get item delete url
  189. *
  190. * @return string
  191. */
  192. public function getDeleteUrl()
  193. {
  194. return $this->getUrl(
  195. 'checkout/cart/delete',
  196. array(
  197. 'id'=>$this->getItem()->getId(),
  198. Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->helper('core/url')->getEncodedUrl()
  199. )
  200. );
  201. }
  202. /**
  203. * Get quote item qty
  204. *
  205. * @return mixed
  206. */
  207. public function getQty()
  208. {
  209. return $this->getItem()->getQty()*1;
  210. }
  211. /**
  212. * Check item is in stock
  213. *
  214. * @return bool
  215. */
  216. public function getIsInStock()
  217. {
  218. if ($this->getItem()->getProduct()->isSaleable()) {
  219. if ($this->getItem()->getProduct()->getQty()>=$this->getItem()->getQty()) {
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. /**
  226. * Retrieve item messages
  227. * Return array with keys
  228. *
  229. * type => type of a message
  230. * text => the message text
  231. *
  232. * @return array
  233. */
  234. public function getMessages()
  235. {
  236. $messages = array();
  237. if ($this->getItem()->getMessage(false)) {
  238. foreach ($this->getItem()->getMessage(false) as $message) {
  239. $messages[] = array(
  240. 'text' => $message,
  241. 'type' => $this->getItem()->getHasError() ? 'error' : 'notice'
  242. );
  243. }
  244. }
  245. return array_unique($messages);
  246. }
  247. /**
  248. * Accept option value and return its formatted view
  249. *
  250. * @param mixed $optionValue
  251. * Method works well with these $optionValue format:
  252. * 1. String
  253. * 2. Indexed array e.g. array(val1, val2, ...)
  254. * 3. Associative array, containing additional option info, including option value, e.g.
  255. * array
  256. * (
  257. * [label] => ...,
  258. * [value] => ...,
  259. * [print_value] => ...,
  260. * [option_id] => ...,
  261. * [option_type] => ...,
  262. * [custom_view] =>...,
  263. * )
  264. *
  265. * @return array
  266. */
  267. public function getFormatedOptionValue($optionValue)
  268. {
  269. $optionInfo = array();
  270. // define input data format
  271. if (is_array($optionValue)) {
  272. if (isset($optionValue['option_id'])) {
  273. $optionInfo = $optionValue;
  274. if (isset($optionInfo['value'])) {
  275. $optionValue = $optionInfo['value'];
  276. }
  277. } elseif (isset($optionValue['value'])) {
  278. $optionValue = $optionValue['value'];
  279. }
  280. }
  281. // render customized option view
  282. if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
  283. $_default = array('value' => $optionValue);
  284. if (isset($optionInfo['option_type'])) {
  285. try {
  286. $group = Mage::getModel('catalog/product_option')->groupFactory($optionInfo['option_type']);
  287. return array('value' => $group->getCustomizedView($optionInfo));
  288. } catch (Exception $e) {
  289. return $_default;
  290. }
  291. }
  292. return $_default;
  293. }
  294. // truncate standard view
  295. $result = array();
  296. if (is_array($optionValue)) {
  297. $_truncatedValue = implode("\n", $optionValue);
  298. $_truncatedValue = nl2br($_truncatedValue);
  299. return array('value' => $_truncatedValue);
  300. } else {
  301. $_truncatedValue = Mage::helper('core/string')->truncate($optionValue, 55, '');
  302. $_truncatedValue = nl2br($_truncatedValue);
  303. }
  304. $result = array('value' => $_truncatedValue);
  305. if (Mage::helper('core/string')->strlen($optionValue) > 55) {
  306. $result['value'] = $result['value'] . ' <a href="#" class="dots" onclick="return false">...</a>';
  307. $optionValue = nl2br($optionValue);
  308. $result = array_merge($result, array('full_view' => $optionValue));
  309. }
  310. return $result;
  311. }
  312. /**
  313. * Check whether Product is visible in site
  314. *
  315. * @return bool
  316. */
  317. public function isProductVisible()
  318. {
  319. return $this->getProduct()->isVisibleInSiteVisibility();
  320. }
  321. }