Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Sku.php
1<?php
2
4
18
27class Sku extends BaseSku
28{
54
55 public function getDetailUrl(): string
56 {
57 $detailUrl = parent::getDetailUrl();
58
60 if (!$detailUrl && $product = $this->getParent())
61 {
62 $detailUrl = $product->getDetailUrl();
63 }
64
65 return $detailUrl;
66 }
67
68 public function setParentCollection(?BaseCollection $collection): BaseEntity
69 {
70 // ToDo check for correct parent iblock for iblockelemententities!
71 parent::setParentCollection($collection);
72
73 if ($this->isNew())
74 {
75 $this->checkProductLink();
76 }
77
78 return $this;
79 }
80
85 public function checkProductLink(): self
86 {
88 $product = $this->getParent();
89
90 if ($product)
91 {
92 $this->linkProduct($product);
93 }
94 else
95 {
96 $this->unlinkProduct();
97 }
98
99 return $this;
100 }
101
102 protected function linkProduct(BaseProduct $product): self
103 {
104 if (!$this->isNew())
105 {
106 $this->setProductLinkProperty($product->getId());
107 }
108
110
111 return $this;
112 }
113
114 protected function unlinkProduct(): self
115 {
118
119 return $this;
120 }
121
122 protected function getProductLinkProperty(): ?Property
123 {
124 $skuPropertyId = $this->iblockInfo->getSkuPropertyId();
125
126 if ($skuPropertyId)
127 {
128 return $this->getPropertyCollection()->findBySetting('ID', $skuPropertyId);
129 }
130
131 return null;
132 }
133
134 protected function setProductLinkProperty(int $productId): self
135 {
136 $property = $this->getProductLinkProperty();
137
138 if ($property)
139 {
140 // ToDo do we need property values type casting?
141 $property->getPropertyValueCollection()->setValues((string)$productId);
142 }
143
144 return $this;
145 }
146
147 protected function unsetProductLinkProperty(): self
148 {
149 $property = $this->getProductLinkProperty();
150
151 if ($property)
152 {
153 $property->getPropertyValueCollection()->setValues(null);
154 }
155
156 return $this;
157 }
158
159 public function saveInternalEntity(): Result
160 {
161 $isNeedCheckProductLinkAfterSaving = $this->isNew();
162 $result = parent::saveInternalEntity();
163
164 if ($isNeedCheckProductLinkAfterSaving && $result->isSuccess())
165 {
166 $product = $this->getParent();
167 if ($product && $this->iblockInfo->getSkuPropertyId())
168 {
169 \CIBlockElement::SetPropertyValuesEx(
170 $this->getId(),
171 $this->iblockInfo->getSkuIblockId(),
172 [$this->iblockInfo->getSkuPropertyId() => $product->getId()]
173 );
174 }
175
176 $this->checkProductLink();
177 }
178
179 return $result;
180 }
181}
setProductLinkProperty(int $productId)
Definition Sku.php:134
__construct(IblockInfo $iblockInfo, SkuRepositoryContract $skuRepository, PropertyRepositoryContract $propertyRepository, ImageRepositoryContract $imageRepository, PriceRepositoryContract $priceRepository, MeasureRatioRepositoryContract $measureRatioRepository, BarcodeRepositoryContract $barcodeRepository, StoreProductRepositoryContract $storeProductRepository)
Definition Sku.php:29
linkProduct(BaseProduct $product)
Definition Sku.php:102
setParentCollection(?BaseCollection $collection)
Definition Sku.php:68