PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Catalog/Model/Product/Option/Type/Default.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 400 lines | 165 code | 37 blank | 198 comment | 20 complexity | 36a093fe5b2aeb6a4c9992fbc70ca09c 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog product option default type
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Product_Option_Type_Default extends Varien_Object
  34. {
  35. /**
  36. * Option Instance
  37. *
  38. * @var Mage_Catalog_Model_Product_Option
  39. */
  40. protected $_option;
  41. /**
  42. * Product Instance
  43. *
  44. * @var Mage_Catalog_Model_Product
  45. */
  46. protected $_product;
  47. /**
  48. * description
  49. *
  50. * @var mixed
  51. */
  52. protected $_productOptions = array();
  53. /**
  54. * Option Instance setter
  55. *
  56. * @param Mage_Catalog_Model_Product_Option $option
  57. * @return Mage_Catalog_Model_Product_Option_Type_Default
  58. */
  59. public function setOption($option)
  60. {
  61. $this->_option = $option;
  62. return $this;
  63. }
  64. /**
  65. * Option Instance getter
  66. *
  67. * @throws Mage_Core_Exception
  68. * @return Mage_Catalog_Model_Product_Option
  69. */
  70. public function getOption()
  71. {
  72. if ($this->_option instanceof Mage_Catalog_Model_Product_Option) {
  73. return $this->_option;
  74. }
  75. Mage::throwException(Mage::helper('catalog')->__('Wrong option instance type in options group.'));
  76. }
  77. /**
  78. * Product Instance setter
  79. *
  80. * @param Mage_Catalog_Model_Product $product
  81. * @return Mage_Catalog_Model_Product_Option_Type_Default
  82. */
  83. public function setProduct($product)
  84. {
  85. $this->_product = $product;
  86. return $this;
  87. }
  88. /**
  89. * Product Instance getter
  90. *
  91. * @throws Mage_Core_Exception
  92. * @return Mage_Catalog_Model_Product
  93. */
  94. public function getProduct()
  95. {
  96. if ($this->_product instanceof Mage_Catalog_Model_Product) {
  97. return $this->_product;
  98. }
  99. Mage::throwException(Mage::helper('catalog')->__('Wrong product instance type in options group.'));
  100. }
  101. /**
  102. * Getter for Configuration Item Option
  103. *
  104. * @return Mage_Catalog_Model_Product_Configuration_Item_Option_Interface
  105. */
  106. public function getConfigurationItemOption()
  107. {
  108. if ($this->_getData('configuration_item_option') instanceof Mage_Catalog_Model_Product_Configuration_Item_Option_Interface) {
  109. return $this->_getData('configuration_item_option');
  110. }
  111. // Back compatibility with quote specific keys to set configuration item options
  112. if ($this->_getData('quote_item_option') instanceof Mage_Sales_Model_Quote_Item_Option) {
  113. return $this->_getData('quote_item_option');
  114. }
  115. Mage::throwException(Mage::helper('catalog')->__('Wrong configuration item option instance in options group.'));
  116. }
  117. /**
  118. * Getter for Quote Item Option
  119. * Deprecated in favor of getConfigurationItemOption()
  120. *
  121. * @deprecated after 1.4.2.0
  122. *
  123. * @return Mage_Sales_Model_Quote_Item_Option
  124. */
  125. public function getQuoteItemOption()
  126. {
  127. return $this->getConfigurationItemOption();
  128. }
  129. /**
  130. * Getter for Configuration Item
  131. *
  132. * @return Mage_Catalog_Model_Product_Configuration_Item_Interface
  133. */
  134. public function getConfigurationItem()
  135. {
  136. if ($this->_getData('configuration_item') instanceof Mage_Catalog_Model_Product_Configuration_Item_Interface) {
  137. return $this->_getData('configuration_item');
  138. }
  139. // Back compatibility with quote specific keys to set configuration item
  140. if ($this->_getData('quote_item') instanceof Mage_Sales_Model_Quote_Item) {
  141. return $this->_getData('quote_item');
  142. }
  143. Mage::throwException(Mage::helper('catalog')->__('Wrong configuration item instance in options group.'));
  144. }
  145. /**
  146. * Getter for Quote Item
  147. * Deprecated in favor of getConfigurationItem()
  148. *
  149. * @deprecated after 1.4.2.0
  150. * @return Mage_Sales_Model_Quote_Item
  151. */
  152. public function getQuoteItem()
  153. {
  154. return $this->getConfigurationItem();
  155. }
  156. /**
  157. * Getter for Buy Request
  158. *
  159. * @return Varien_Object
  160. */
  161. public function getRequest()
  162. {
  163. if ($this->_getData('request') instanceof Varien_Object) {
  164. return $this->_getData('request');
  165. }
  166. Mage::throwException(Mage::helper('catalog')->__('Wrong BuyRequest instance in options group.'));
  167. }
  168. /**
  169. * Store Config value
  170. *
  171. * @param string $key Config value key
  172. * @return string
  173. */
  174. public function getConfigData($key)
  175. {
  176. return Mage::getStoreConfig('catalog/custom_options/' . $key);
  177. }
  178. /**
  179. * Validate user input for option
  180. *
  181. * @throws Mage_Core_Exception
  182. * @param array $values All product option values, i.e. array (option_id => mixed, option_id => mixed...)
  183. * @return Mage_Catalog_Model_Product_Option_Type_Default
  184. */
  185. public function validateUserValue($values)
  186. {
  187. Mage::getSingleton('checkout/session')->setUseNotice(false);
  188. $this->setIsValid(false);
  189. $option = $this->getOption();
  190. if (!isset($values[$option->getId()]) && $option->getIsRequire() && !$this->getSkipCheckRequiredOption()) {
  191. Mage::throwException(Mage::helper('catalog')->__('Please specify the product required option(s).'));
  192. } elseif (isset($values[$option->getId()])) {
  193. $this->setUserValue($values[$option->getId()]);
  194. $this->setIsValid(true);
  195. }
  196. return $this;
  197. }
  198. /**
  199. * Check skip required option validation
  200. *
  201. * @return bool
  202. */
  203. public function getSkipCheckRequiredOption()
  204. {
  205. return $this->getProduct()->getSkipCheckRequiredOption() ||
  206. $this->getProcessMode() == Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_LITE;
  207. }
  208. /**
  209. * Prepare option value for cart
  210. *
  211. * @throws Mage_Core_Exception
  212. * @return mixed Prepared option value
  213. */
  214. public function prepareForCart()
  215. {
  216. if ($this->getIsValid()) {
  217. return $this->getUserValue();
  218. }
  219. Mage::throwException(Mage::helper('catalog')->__('Option validation failed to add product to cart.'));
  220. }
  221. /**
  222. * Flag to indicate that custom option has own customized output (blocks, native html etc.)
  223. *
  224. * @return boolean
  225. */
  226. public function isCustomizedView()
  227. {
  228. return false;
  229. }
  230. /**
  231. * Return formatted option value for quote option
  232. *
  233. * @param string $optionValue Prepared for cart option value
  234. * @return string
  235. */
  236. public function getFormattedOptionValue($optionValue)
  237. {
  238. return $optionValue;
  239. }
  240. /**
  241. * Return option html
  242. *
  243. * @param array $optionInfo
  244. * @return string
  245. */
  246. public function getCustomizedView($optionInfo)
  247. {
  248. return isset($optionInfo['value']) ? $optionInfo['value'] : $optionInfo;
  249. }
  250. /**
  251. * Return printable option value
  252. *
  253. * @param string $optionValue Prepared for cart option value
  254. * @return string
  255. */
  256. public function getPrintableOptionValue($optionValue)
  257. {
  258. return $optionValue;
  259. }
  260. /**
  261. * Return formatted option value ready to edit, ready to parse
  262. * (ex: Admin re-order, see Mage_Adminhtml_Model_Sales_Order_Create)
  263. *
  264. * @param string $optionValue Prepared for cart option value
  265. * @return string
  266. */
  267. public function getEditableOptionValue($optionValue)
  268. {
  269. return $optionValue;
  270. }
  271. /**
  272. * Parse user input value and return cart prepared value, i.e. "one, two" => "1,2"
  273. *
  274. * @param string $optionValue
  275. * @param array $productOptionValues Values for product option
  276. * @return string|null
  277. */
  278. public function parseOptionValue($optionValue, $productOptionValues)
  279. {
  280. return $optionValue;
  281. }
  282. /**
  283. * Prepare option value for info buy request
  284. *
  285. * @param string $optionValue
  286. * @return mixed
  287. */
  288. public function prepareOptionValueForRequest($optionValue)
  289. {
  290. return $optionValue;
  291. }
  292. /**
  293. * Return Price for selected option
  294. *
  295. * @param string $optionValue Prepared for cart option value
  296. * @param float $basePrice For percent price type
  297. * @return float
  298. */
  299. public function getOptionPrice($optionValue, $basePrice)
  300. {
  301. $option = $this->getOption();
  302. return $this->_getChargableOptionPrice(
  303. $option->getPrice(),
  304. $option->getPriceType() == 'percent',
  305. $basePrice
  306. );
  307. }
  308. /**
  309. * Return SKU for selected option
  310. *
  311. * @param string $optionValue Prepared for cart option value
  312. * @param string $skuDelimiter Delimiter for Sku parts
  313. * @return string
  314. */
  315. public function getOptionSku($optionValue, $skuDelimiter)
  316. {
  317. return $this->getOption()->getSku();
  318. }
  319. /**
  320. * Return value => key all product options (using for parsing)
  321. *
  322. * @return array Array of Product custom options, reversing option values and option ids
  323. */
  324. public function getProductOptions()
  325. {
  326. if (!isset($this->_productOptions[$this->getProduct()->getId()])) {
  327. foreach ($this->getProduct()->getOptions() as $_option) {
  328. /* @var $option Mage_Catalog_Model_Product_Option */
  329. $this->_productOptions[$this->getProduct()->getId()][$_option->getTitle()] = array('option_id' => $_option->getId());
  330. if ($_option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
  331. $optionValues = array();
  332. foreach ($_option->getValues() as $_value) {
  333. /* @var $value Mage_Catalog_Model_Product_Option_Value */
  334. $optionValues[$_value->getTitle()] = $_value->getId();
  335. }
  336. $this->_productOptions[$this->getProduct()->getId()][$_option->getTitle()]['values'] = $optionValues;
  337. } else {
  338. $this->_productOptions[$this->getProduct()->getId()][$_option->getTitle()]['values'] = array();
  339. }
  340. }
  341. }
  342. if (isset($this->_productOptions[$this->getProduct()->getId()])) {
  343. return $this->_productOptions[$this->getProduct()->getId()];
  344. }
  345. return array();
  346. }
  347. /**
  348. * Return final chargable price for option
  349. *
  350. * @param float $price Price of option
  351. * @param boolean $isPercent Price type - percent or fixed
  352. * @param float $basePrice For percent price type
  353. * @return float
  354. */
  355. protected function _getChargableOptionPrice($price, $isPercent, $basePrice)
  356. {
  357. if($isPercent) {
  358. return ($basePrice * $price / 100);
  359. } else {
  360. return $price;
  361. }
  362. }
  363. }