Option.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Model;
  7. /**
  8. * Bundle Option Model
  9. *
  10. * @api
  11. * @method int getParentId()
  12. * @method null|\Magento\Catalog\Model\Product[] getSelections()
  13. * @method Option setParentId(int $value)
  14. * @since 100.0.2
  15. */
  16. class Option extends \Magento\Framework\Model\AbstractExtensibleModel implements
  17. \Magento\Bundle\Api\Data\OptionInterface
  18. {
  19. /**#@+
  20. * Constants
  21. */
  22. const KEY_OPTION_ID = 'option_id';
  23. const KEY_TITLE = 'title';
  24. const KEY_REQUIRED = 'required';
  25. const KEY_TYPE = 'type';
  26. const KEY_POSITION = 'position';
  27. const KEY_SKU = 'sku';
  28. const KEY_PRODUCT_LINKS = 'product_links';
  29. /**#@-*/
  30. /**#@-*/
  31. protected $defaultSelection = null;
  32. /**
  33. * Initialize resource model
  34. *
  35. * @return void
  36. */
  37. protected function _construct()
  38. {
  39. $this->_init(\Magento\Bundle\Model\ResourceModel\Option::class);
  40. parent::_construct();
  41. }
  42. /**
  43. * Add selection to option
  44. *
  45. * @param \Magento\Catalog\Model\Product $selection
  46. * @return void
  47. */
  48. public function addSelection(\Magento\Catalog\Model\Product $selection)
  49. {
  50. if (!$this->hasData('selections')) {
  51. $this->setData('selections', []);
  52. }
  53. $selections = $this->getData('selections');
  54. $selections[] = $selection;
  55. $this->setSelections($selections);
  56. }
  57. /**
  58. * Check Is Saleable Option
  59. *
  60. * @return bool
  61. */
  62. public function isSaleable()
  63. {
  64. $saleable = false;
  65. $selections = $this->getSelections();
  66. if ($selections) {
  67. foreach ($selections as $selection) {
  68. if ($selection->isSaleable()) {
  69. $saleable = true;
  70. break;
  71. }
  72. }
  73. }
  74. return $saleable;
  75. }
  76. /**
  77. * Retrieve default Selection object
  78. *
  79. * @return \Magento\Catalog\Model\Product|null
  80. */
  81. public function getDefaultSelection()
  82. {
  83. if (!$this->defaultSelection && $this->getSelections()) {
  84. foreach ($this->getSelections() as $selection) {
  85. if ($selection->getIsDefault()) {
  86. $this->defaultSelection = $selection;
  87. break;
  88. }
  89. }
  90. }
  91. return $this->defaultSelection;
  92. }
  93. /**
  94. * Check is multi Option selection
  95. *
  96. * @return bool
  97. */
  98. public function isMultiSelection()
  99. {
  100. return $this->getType() == 'checkbox' || $this->getType() == 'multi';
  101. }
  102. /**
  103. * Retrieve options searchable data
  104. *
  105. * @param int $productId
  106. * @param int $storeId
  107. * @return array
  108. */
  109. public function getSearchableData($productId, $storeId)
  110. {
  111. return $this->_getResource()->getSearchableData($productId, $storeId);
  112. }
  113. /**
  114. * Return selection by it's id
  115. *
  116. * @param int $selectionId
  117. * @return \Magento\Catalog\Model\Product|null
  118. */
  119. public function getSelectionById($selectionId)
  120. {
  121. $foundSelection = null;
  122. foreach ($this->getSelections() as $selection) {
  123. if ($selection->getSelectionId() == $selectionId) {
  124. $foundSelection = $selection;
  125. break;
  126. }
  127. }
  128. return $foundSelection;
  129. }
  130. //@codeCoverageIgnoreStart
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function getOptionId()
  135. {
  136. return $this->getData(self::KEY_OPTION_ID);
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function getTitle()
  142. {
  143. return $this->getData(self::KEY_TITLE);
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function getRequired()
  149. {
  150. return $this->getData(self::KEY_REQUIRED);
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getType()
  156. {
  157. return $this->getData(self::KEY_TYPE);
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function getPosition()
  163. {
  164. return $this->getData(self::KEY_POSITION);
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function getSku()
  170. {
  171. return $this->getData(self::KEY_SKU);
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function getProductLinks()
  177. {
  178. return $this->getData(self::KEY_PRODUCT_LINKS);
  179. }
  180. /**
  181. * Set option id
  182. *
  183. * @param int $optionId
  184. * @return $this
  185. */
  186. public function setOptionId($optionId)
  187. {
  188. return $this->setData(self::KEY_OPTION_ID, $optionId);
  189. }
  190. /**
  191. * Set option title
  192. *
  193. * @param string $title
  194. * @return $this
  195. */
  196. public function setTitle($title)
  197. {
  198. return $this->setData(self::KEY_TITLE, $title);
  199. }
  200. /**
  201. * Set whether option is required
  202. *
  203. * @param bool $required
  204. * @return $this
  205. */
  206. public function setRequired($required)
  207. {
  208. return $this->setData(self::KEY_REQUIRED, $required);
  209. }
  210. /**
  211. * Set input type
  212. *
  213. * @param string $type
  214. * @return $this
  215. */
  216. public function setType($type)
  217. {
  218. return $this->setData(self::KEY_TYPE, $type);
  219. }
  220. /**
  221. * Set option position
  222. *
  223. * @param int $position
  224. * @return $this
  225. */
  226. public function setPosition($position)
  227. {
  228. return $this->setData(self::KEY_POSITION, $position);
  229. }
  230. /**
  231. * Set product sku
  232. *
  233. * @param string $sku
  234. * @return $this
  235. */
  236. public function setSku($sku)
  237. {
  238. return $this->setData(self::KEY_SKU, $sku);
  239. }
  240. /**
  241. * Set product links
  242. *
  243. * @param \Magento\Bundle\Api\Data\LinkInterface[] $productLinks
  244. * @return $this
  245. */
  246. public function setProductLinks(array $productLinks = null)
  247. {
  248. return $this->setData(self::KEY_PRODUCT_LINKS, $productLinks);
  249. }
  250. /**
  251. * {@inheritdoc}
  252. *
  253. * @return \Magento\Bundle\Api\Data\OptionExtensionInterface|null
  254. */
  255. public function getExtensionAttributes()
  256. {
  257. return $this->_getExtensionAttributes();
  258. }
  259. /**
  260. * {@inheritdoc}
  261. *
  262. * @param \Magento\Bundle\Api\Data\OptionExtensionInterface $extensionAttributes
  263. * @return $this
  264. */
  265. public function setExtensionAttributes(\Magento\Bundle\Api\Data\OptionExtensionInterface $extensionAttributes)
  266. {
  267. return $this->_setExtensionAttributes($extensionAttributes);
  268. }
  269. //@codeCoverageIgnoreEnd
  270. }