1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Sku.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\v2\Sku;
4
5use Bitrix\Catalog\ProductTable;
6use Bitrix\Catalog\v2\Barcode\BarcodeRepositoryContract;
7use Bitrix\Catalog\v2\BaseCollection;
8use Bitrix\Catalog\v2\BaseEntity;
9use Bitrix\Catalog\v2\Iblock\IblockInfo;
10use Bitrix\Catalog\v2\Image\ImageRepositoryContract;
11use Bitrix\Catalog\v2\MeasureRatio\MeasureRatioRepositoryContract;
12use Bitrix\Catalog\v2\Price\PriceRepositoryContract;
13use Bitrix\Catalog\v2\Product\BaseProduct;
14use Bitrix\Catalog\v2\Property\Property;
15use Bitrix\Catalog\v2\Property\PropertyRepositoryContract;
16use Bitrix\Catalog\v2\StoreProduct\StoreProductRepositoryContract;
17use Bitrix\Main\Result;
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 {
129 return $this->getPropertyCollection()->findById($skuPropertyId);
130 }
131
132 return null;
133 }
134
135 protected function setProductLinkProperty(int $productId): self
136 {
137 $property = $this->getProductLinkProperty();
138
139 if ($property)
140 {
141 // ToDo do we need property values type casting?
142 $property->getPropertyValueCollection()->setValues((string)$productId);
143 }
144
145 return $this;
146 }
147
148 protected function unsetProductLinkProperty(): self
149 {
150 $property = $this->getProductLinkProperty();
151
152 if ($property)
153 {
154 $property->getPropertyValueCollection()->setValues(null);
155 }
156
157 return $this;
158 }
159
160 public function saveInternalEntity(): Result
161 {
162 $isNeedCheckProductLinkAfterSaving = $this->isNew();
163 $result = parent::saveInternalEntity();
164
165 if ($isNeedCheckProductLinkAfterSaving && $result->isSuccess())
166 {
167 $product = $this->getParent();
168 if ($product && $this->iblockInfo->getSkuPropertyId())
169 {
170 \CIBlockElement::SetPropertyValuesEx(
171 $this->getId(),
172 $this->iblockInfo->getSkuIblockId(),
173 [$this->iblockInfo->getSkuPropertyId() => $product->getId()]
174 );
175 }
176
177 $this->checkProductLink();
178 }
179
180 return $result;
181 }
182}
const TYPE_FREE_OFFER
Определения product.php:74
const TYPE_OFFER
Определения product.php:73
$storeProductRepository
Определения BaseSku.php:51
$measureRatioRepository
Определения BaseSku.php:41
unsetProductLinkProperty()
Определения Sku.php:148
saveInternalEntity()
Определения Sku.php:160
setProductLinkProperty(int $productId)
Определения Sku.php:135
unlinkProduct()
Определения Sku.php:114
__construct(IblockInfo $iblockInfo, SkuRepositoryContract $skuRepository, PropertyRepositoryContract $propertyRepository, ImageRepositoryContract $imageRepository, PriceRepositoryContract $priceRepository, MeasureRatioRepositoryContract $measureRatioRepository, BarcodeRepositoryContract $barcodeRepository, StoreProductRepositoryContract $storeProductRepository)
Определения Sku.php:29
linkProduct(BaseProduct $product)
Определения Sku.php:102
setParentCollection(?BaseCollection $collection)
Определения Sku.php:68
$result
Определения get_property_values.php:14