Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ProductConverter.php
1<?php
2
4
12
22{
23 public const SIMPLE_PRODUCT = 'SIMPLE_PRODUCT';
24 public const SKU_PRODUCT = 'SKU_PRODUCT';
25
26 private $container;
27
28 public function __construct(ContainerContract $container)
29 {
30 $this->container = $container;
31 }
32
33 public function convert(BaseProduct $product, string $destinationType): Result
34 {
35 if ($destinationType === self::SKU_PRODUCT)
36 {
37 return $this->convertSimpleProductToSkuProduct($product);
38 }
39
40 // if ($destinationType === self::SIMPLE_PRODUCT)
41 // {
42 // return $this->convertSkuToSimpleProduct($product);
43 // }
44
45 return (new Result())->addError(new Error(sprintf(
46 'Could not convert product {%s} to type {%s}.',
47 get_class($product),
48 $destinationType
49 )));
50 }
51
52 protected function convertSimpleProductToSkuProduct(BaseProduct $product): Result
53 {
54 $result = new Result();
55
56 if ($product->isSimple() && $product->getIblockInfo()->canHaveSku())
57 {
59 foreach ($product->getSkuCollection() as $item)
60 {
61 if (!$item->isSimple())
62 {
63 continue;
64 }
65
66 $skuItem = $this->convertSimpleSkuToSku($item);
67 if ($skuItem)
68 {
69 $result->setData([
70 'CONVERTED_SKU' => $skuItem,
71 ]);
72 }
73 }
74
76 }
77
78 return $result;
79 }
80
81 protected function convertSimpleSkuToSku(BaseSku $simpleItem): BaseSku
82 {
84 $skuFactory = $this->container->get(Dependency::SKU_FACTORY, [
85 Dependency::IBLOCK_INFO => $simpleItem->getIblockInfo(),
86 ]);
87
88 if (!$skuFactory)
89 {
90 return $simpleItem;
91 }
92
93 $skuItem = $skuFactory->createEntity($skuFactory::SKU);
94 if ($skuItem->isSimple())
95 {
96 return $simpleItem;
97 }
98
100 $skuCollection = $simpleItem->getParentCollection();
101 $skuCollection
102 ->remove($simpleItem)
103 ->clearRemoved($simpleItem)
104 ->add($skuItem)
105 ;
106
107 $fields = array_diff_key($simpleItem->getFields(), [
108 'ID' => true,
109 'IBLOCK_ID' => true,
110 'TYPE' => true,
111 'DETAIL_TEXT' => true,
112 'DETAIL_TEXT_TYPE' => true,
113 'PREVIEW_TEXT' => true,
114 'PREVIEW_TEXT_TYPE' => true,
115 ]);
116 $skuItem->setFields($fields);
117
118 $skuItem->getPriceCollection()->setValues($simpleItem->getPriceCollection()->getValues());
119
120 $defaultRatio = $simpleItem->getMeasureRatioCollection()->findDefault();
121 if ($defaultRatio)
122 {
123 $skuItem->getMeasureRatioCollection()->setDefault($defaultRatio->getRatio());
124 }
125
126 return $skuItem;
127 }
128
129 // ToDo currently doesn't work properly because of different calculation handlers of old api
130 protected function convertSkuToSimpleProduct(BaseProduct $product): Result
131 {
132 $result = new Result();
133
134 if (!$product->isSimple() && $product->getSkuCollection()->count() <= 1)
135 {
137 foreach ($product->getSkuCollection() as $item)
138 {
139 if ($item->isSimple())
140 {
141 continue;
142 }
143
144 // ToDo another converter entity for Sku?
145 $skuItem = $this->convertSkuToSimpleSku($item);
146 if ($skuItem)
147 {
148 $result->setData([
149 'CONVERTED_SKU' => $skuItem,
150 ]);
151 }
152 }
153
154 $product->setType(ProductTable::TYPE_PRODUCT);
155 }
156
157 return $result;
158 }
159
160 protected function convertSkuToSimpleSku(BaseSku $skuItem): BaseSku
161 {
163 $skuFactory = $this->container->get(Dependency::SKU_FACTORY, [
164 Dependency::IBLOCK_INFO => $skuItem->getIblockInfo(),
165 ]);
166
167 if (!$skuFactory)
168 {
169 return $skuItem;
170 }
171
172 $simpleItem = $skuFactory->createEntity($skuFactory::SIMPLE_SKU);
173
175 $skuCollection = $skuItem->getParentCollection();
176 $skuCollection->remove($skuItem)
177 ->add($simpleItem)
178 ;
179
180 $fields = array_diff_key($skuItem->getFields(), [
181 'ID' => true,
182 'IBLOCK_ID' => true,
183 'TYPE' => true,
184 ]);
185 $simpleItem->setFields($fields);
186
187 $simpleItem->getPriceCollection()->setValues($skuItem->getPriceCollection()->getValues());
188
189 $defaultRatio = $skuItem->getMeasureRatioCollection()->findDefault();
190 if ($defaultRatio)
191 {
192 $simpleItem->getMeasureRatioCollection()->setDefault($defaultRatio->getRatio());
193 }
194
195 return $simpleItem;
196 }
197}
convert(BaseProduct $product, string $destinationType)