PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/module-catalog/Model/Product/Option/Value.php

https://bitbucket.org/sergiu-tot-fb/vendors
PHP | 436 lines | 206 code | 44 blank | 186 comment | 10 complexity | db4361f2487c6e05c306053867b27971 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. // @codingStandardsIgnoreFile
  7. namespace Magento\Catalog\Model\Product\Option;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Catalog\Model\Product\Option;
  10. use Magento\Framework\Model\AbstractModel;
  11. /**
  12. * Catalog product option select type model
  13. *
  14. * @api
  15. * @method int getOptionId()
  16. * @method \Magento\Catalog\Model\Product\Option\Value setOptionId(int $value)
  17. *
  18. * @SuppressWarnings(PHPMD.LongVariable)
  19. * @since 100.0.2
  20. */
  21. class Value extends AbstractModel implements \Magento\Catalog\Api\Data\ProductCustomOptionValuesInterface
  22. {
  23. /**
  24. * Option type percent
  25. */
  26. const TYPE_PERCENT = 'percent';
  27. /**#@+
  28. * Constants
  29. */
  30. const KEY_TITLE = 'title';
  31. const KEY_SORT_ORDER = 'sort_order';
  32. const KEY_PRICE = 'price';
  33. const KEY_PRICE_TYPE = 'price_type';
  34. const KEY_SKU = 'sku';
  35. const KEY_OPTION_TYPE_ID = 'option_type_id';
  36. /**#@-*/
  37. /**#@-*/
  38. protected $_values = [];
  39. /**
  40. * @var Product
  41. */
  42. protected $_product;
  43. /**
  44. * @var Option
  45. */
  46. protected $_option;
  47. /**
  48. * Value collection factory
  49. *
  50. * @var \Magento\Catalog\Model\ResourceModel\Product\Option\Value\CollectionFactory
  51. */
  52. protected $_valueCollectionFactory;
  53. /**
  54. * @param \Magento\Framework\Model\Context $context
  55. * @param \Magento\Framework\Registry $registry
  56. * @param \Magento\Catalog\Model\ResourceModel\Product\Option\Value\CollectionFactory $valueCollectionFactory
  57. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  58. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  59. * @param array $data
  60. */
  61. public function __construct(
  62. \Magento\Framework\Model\Context $context,
  63. \Magento\Framework\Registry $registry,
  64. \Magento\Catalog\Model\ResourceModel\Product\Option\Value\CollectionFactory $valueCollectionFactory,
  65. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  66. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  67. array $data = []
  68. ) {
  69. $this->_valueCollectionFactory = $valueCollectionFactory;
  70. parent::__construct(
  71. $context,
  72. $registry,
  73. $resource,
  74. $resourceCollection,
  75. $data
  76. );
  77. }
  78. /**
  79. * @return void
  80. */
  81. protected function _construct()
  82. {
  83. $this->_init(\Magento\Catalog\Model\ResourceModel\Product\Option\Value::class);
  84. }
  85. /**
  86. * @codeCoverageIgnoreStart
  87. * @param mixed $value
  88. * @return $this
  89. */
  90. public function addValue($value)
  91. {
  92. $this->_values[] = $value;
  93. return $this;
  94. }
  95. /**
  96. * @return array
  97. */
  98. public function getValues()
  99. {
  100. return $this->_values;
  101. }
  102. /**
  103. * @param array $values
  104. * @return $this
  105. */
  106. public function setValues($values)
  107. {
  108. $this->_values = $values;
  109. return $this;
  110. }
  111. /**
  112. * @return $this
  113. */
  114. public function unsetValues()
  115. {
  116. $this->_values = [];
  117. return $this;
  118. }
  119. /**
  120. * @param Option $option
  121. * @return $this
  122. */
  123. public function setOption(Option $option)
  124. {
  125. $this->_option = $option;
  126. return $this;
  127. }
  128. /**
  129. * @return $this
  130. */
  131. public function unsetOption()
  132. {
  133. $this->_option = null;
  134. return $this;
  135. }
  136. /**
  137. * Enter description here...
  138. *
  139. * @return Option
  140. */
  141. public function getOption()
  142. {
  143. return $this->_option;
  144. }
  145. /**
  146. * @param Product $product
  147. * @return $this
  148. */
  149. public function setProduct($product)
  150. {
  151. $this->_product = $product;
  152. return $this;
  153. }
  154. //@codeCoverageIgnoreEnd
  155. /**
  156. * @return Product
  157. */
  158. public function getProduct()
  159. {
  160. if (is_null($this->_product)) {
  161. $this->_product = $this->getOption()->getProduct();
  162. }
  163. return $this->_product;
  164. }
  165. /**
  166. * @return $this
  167. */
  168. public function saveValues()
  169. {
  170. foreach ($this->getValues() as $value) {
  171. $this->setData(
  172. $value
  173. )->setData(
  174. 'option_id',
  175. $this->getOption()->getId()
  176. )->setData(
  177. 'store_id',
  178. $this->getOption()->getStoreId()
  179. );
  180. if ($this->getData('is_delete') == '1') {
  181. if ($this->getId()) {
  182. $this->deleteValues($this->getId());
  183. $this->delete();
  184. }
  185. } else {
  186. $this->save();
  187. }
  188. }
  189. //eof foreach()
  190. return $this;
  191. }
  192. /**
  193. * Return price. If $flag is true and price is percent
  194. * return converted percent to price
  195. *
  196. * @param bool $flag
  197. * @return float|int
  198. */
  199. public function getPrice($flag = false)
  200. {
  201. if ($flag && $this->getPriceType() == self::TYPE_PERCENT) {
  202. $basePrice = $this->getOption()->getProduct()->getFinalPrice();
  203. $price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
  204. return $price;
  205. }
  206. return $this->_getData(self::KEY_PRICE);
  207. }
  208. /**
  209. * Return regular price.
  210. *
  211. * @return float|int
  212. */
  213. public function getRegularPrice()
  214. {
  215. if ($this->getPriceType() == self::TYPE_PERCENT) {
  216. $basePrice = $this->getOption()->getProduct()->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue();
  217. $price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
  218. return $price;
  219. }
  220. return $this->_getData(self::KEY_PRICE);
  221. }
  222. /**
  223. * Enter description here...
  224. *
  225. * @param Option $option
  226. * @return \Magento\Catalog\Model\ResourceModel\Product\Option\Value\Collection
  227. */
  228. public function getValuesCollection(Option $option)
  229. {
  230. $collection = $this->_valueCollectionFactory->create()->addFieldToFilter(
  231. 'option_id',
  232. $option->getId()
  233. )->getValues(
  234. $option->getStoreId()
  235. );
  236. return $collection;
  237. }
  238. /**
  239. * @param array $optionIds
  240. * @param int $option_id
  241. * @param int $store_id
  242. * @return \Magento\Catalog\Model\ResourceModel\Product\Option\Value\Collection
  243. */
  244. public function getValuesByOption($optionIds, $option_id, $store_id)
  245. {
  246. $collection = $this->_valueCollectionFactory->create()->addFieldToFilter(
  247. 'option_id',
  248. $option_id
  249. )->getValuesByOption(
  250. $optionIds,
  251. $store_id
  252. );
  253. return $collection;
  254. }
  255. /**
  256. * @param int $option_id
  257. * @return $this
  258. */
  259. public function deleteValue($option_id)
  260. {
  261. $this->getResource()->deleteValue($option_id);
  262. return $this;
  263. }
  264. /**
  265. * @param int $option_type_id
  266. * @return $this
  267. */
  268. public function deleteValues($option_type_id)
  269. {
  270. $this->getResource()->deleteValues($option_type_id);
  271. return $this;
  272. }
  273. /**
  274. * Duplicate product options value
  275. *
  276. * @param int $oldOptionId
  277. * @param int $newOptionId
  278. * @return $this
  279. */
  280. public function duplicate($oldOptionId, $newOptionId)
  281. {
  282. $this->getResource()->duplicate($this, $oldOptionId, $newOptionId);
  283. return $this;
  284. }
  285. /**
  286. * Get option title
  287. *
  288. * @return string
  289. * @codeCoverageIgnoreStart
  290. */
  291. public function getTitle()
  292. {
  293. return $this->_getData(self::KEY_TITLE);
  294. }
  295. /**
  296. * Get sort order
  297. *
  298. * @return int
  299. */
  300. public function getSortOrder()
  301. {
  302. return $this->_getData(self::KEY_SORT_ORDER);
  303. }
  304. /**
  305. * Get price type
  306. *
  307. * @return string
  308. */
  309. public function getPriceType()
  310. {
  311. return $this->_getData(self::KEY_PRICE_TYPE);
  312. }
  313. /**
  314. * Get Sku
  315. *
  316. * @return string|null
  317. */
  318. public function getSku()
  319. {
  320. return $this->_getData(self::KEY_SKU);
  321. }
  322. /**
  323. * Get Sku
  324. *
  325. * @return string|null
  326. */
  327. public function getOptionTypeId()
  328. {
  329. return $this->_getData(self::KEY_OPTION_TYPE_ID);
  330. }
  331. /**
  332. * Set option title
  333. *
  334. * @param string $title
  335. * @return $this
  336. */
  337. public function setTitle($title)
  338. {
  339. return $this->setData(self::KEY_TITLE, $title);
  340. }
  341. /**
  342. * Set sort order
  343. *
  344. * @param int $sortOrder
  345. * @return $this
  346. */
  347. public function setSortOrder($sortOrder)
  348. {
  349. return $this->setData(self::KEY_SORT_ORDER, $sortOrder);
  350. }
  351. /**
  352. * Set price
  353. *
  354. * @param float $price
  355. * @return $this
  356. */
  357. public function setPrice($price)
  358. {
  359. return $this->setData(self::KEY_PRICE, $price);
  360. }
  361. /**
  362. * Set price type
  363. *
  364. * @param string $priceType
  365. * @return $this
  366. */
  367. public function setPriceType($priceType)
  368. {
  369. return $this->setData(self::KEY_PRICE_TYPE, $priceType);
  370. }
  371. /**
  372. * Set Sku
  373. *
  374. * @param string $sku
  375. * @return $this
  376. */
  377. public function setSku($sku)
  378. {
  379. return $this->setData(self::KEY_SKU, $sku);
  380. }
  381. /**
  382. * Set Option type id
  383. *
  384. * @param int $optionTypeId
  385. * @return int|null
  386. */
  387. public function setOptionTypeId($optionTypeId)
  388. {
  389. return $this->setData(self::KEY_OPTION_TYPE_ID, $optionTypeId);
  390. }
  391. //@codeCoverageIgnoreEnd
  392. }