Option.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Model\Item;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Wishlist\Model\Item;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. /**
  11. * Item option model
  12. * @method int getProductId()
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Option extends \Magento\Framework\Model\AbstractModel implements
  18. \Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface
  19. {
  20. /**
  21. * @var Item
  22. */
  23. protected $_item;
  24. /**
  25. * @var Product|null
  26. */
  27. protected $_product;
  28. /**
  29. * @var ProductRepositoryInterface
  30. */
  31. protected $productRepository;
  32. /**
  33. * @param \Magento\Framework\Model\Context $context
  34. * @param \Magento\Framework\Registry $registry
  35. * @param ProductRepositoryInterface $productRepository
  36. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  37. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  38. * @param array $data
  39. */
  40. public function __construct(
  41. \Magento\Framework\Model\Context $context,
  42. \Magento\Framework\Registry $registry,
  43. ProductRepositoryInterface $productRepository,
  44. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  45. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  46. array $data = []
  47. ) {
  48. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  49. $this->productRepository = $productRepository;
  50. }
  51. /**
  52. * Initialize resource model
  53. *
  54. * @return void
  55. */
  56. protected function _construct()
  57. {
  58. $this->_init(\Magento\Wishlist\Model\ResourceModel\Item\Option::class);
  59. }
  60. /**
  61. * Checks that item option model has data changes
  62. *
  63. * @return bool
  64. */
  65. protected function _hasModelChanged()
  66. {
  67. if (!$this->hasDataChanges()) {
  68. return false;
  69. }
  70. return $this->_getResource()->hasDataChanged($this);
  71. }
  72. /**
  73. * Set quote item
  74. *
  75. * @param Item $item
  76. * @return $this
  77. */
  78. public function setItem($item)
  79. {
  80. $this->setWishlistItemId($item->getId());
  81. $this->_item = $item;
  82. return $this;
  83. }
  84. /**
  85. * Get option item
  86. *
  87. * @return Item
  88. */
  89. public function getItem()
  90. {
  91. return $this->_item;
  92. }
  93. /**
  94. * Set option product
  95. *
  96. * @param Product $product
  97. * @return $this
  98. */
  99. public function setProduct($product)
  100. {
  101. $this->setProductId($product->getId());
  102. $this->_product = $product;
  103. return $this;
  104. }
  105. /**
  106. * Get option product
  107. *
  108. * @return Product
  109. */
  110. public function getProduct()
  111. {
  112. //In some cases product_id is present instead product instance
  113. if (null === $this->_product && $this->getProductId()) {
  114. $this->_product = $this->productRepository->getById($this->getProductId());
  115. }
  116. return $this->_product;
  117. }
  118. /**
  119. * Get option value
  120. *
  121. * @return mixed
  122. */
  123. public function getValue()
  124. {
  125. return $this->_getData('value');
  126. }
  127. /**
  128. * Initialize item identifier before save data
  129. *
  130. * @return $this
  131. */
  132. public function beforeSave()
  133. {
  134. if ($this->getItem()) {
  135. $this->setWishlistItemId($this->getItem()->getId());
  136. }
  137. return parent::beforeSave();
  138. }
  139. /**
  140. * Clone option object
  141. *
  142. * @return $this
  143. */
  144. public function __clone()
  145. {
  146. $this->setId(null);
  147. $this->_item = null;
  148. return $this;
  149. }
  150. }