Revision e790c22c
Von Jan Büren vor fast 3 Jahren hinzugefügt
SL/ShopConnector/Shopware6.pm | ||
---|---|---|
113 | 113 |
my $part = SL::DB::Part->new(id => $shop_part->part_id)->load; |
114 | 114 |
die "Shop Part but no kivi Part?" unless ref $part eq 'SL::DB::Part'; |
115 | 115 |
|
116 |
my @cat = (); |
|
117 |
# if the part is connected to a category at all |
|
118 |
if ($shop_part->shop_category) { |
|
119 |
foreach my $row_cat ( @{ $shop_part->shop_category } ) { |
|
120 |
my $temp = { ( id => @{$row_cat}[0] ) }; |
|
121 |
push ( @cat, $temp ); |
|
122 |
} |
|
123 |
} |
|
124 |
|
|
125 | 116 |
my $tax_n_price = $shop_part->get_tax_and_price; |
126 | 117 |
my $price = $tax_n_price->{price}; |
127 | 118 |
my $taxrate = $tax_n_price->{tax}; |
... | ... | |
240 | 231 |
$self->sync_all_images(shop_part => $shop_part, set_cover => 1, delete_orphaned => 1); |
241 | 232 |
} catch { die "Could not sync images for Part " . $part->partnumber . " Reason: $_" }; |
242 | 233 |
|
234 |
# if there are categories try to sync this with the shop_part |
|
235 |
try { |
|
236 |
$self->sync_all_categories(shop_part => $shop_part); |
|
237 |
} catch { die "Could not sync Categories for Part " . $part->partnumber . " Reason: $_" }; |
|
238 |
|
|
243 | 239 |
return 1; # no invalid response code -> success |
244 | 240 |
} |
241 |
sub sync_all_categories { |
|
242 |
my ($self, %params) = @_; |
|
243 |
|
|
244 |
my $shop_part = delete $params{shop_part}; |
|
245 |
croak "Need a valid Shop Part for updating Images" unless ref($shop_part) eq 'SL::DB::ShopPart'; |
|
246 |
|
|
247 |
my $partnumber = $shop_part->part->partnumber; |
|
248 |
die "Shop Part but no kivi Partnumber" unless $partnumber; |
|
249 |
|
|
250 |
my ($ret, $response_code); |
|
251 |
# 1 get uuid for product |
|
252 |
my $product_filter = { |
|
253 |
'filter' => [ |
|
254 |
{ |
|
255 |
'value' => $partnumber, |
|
256 |
'type' => 'equals', |
|
257 |
'field' => 'productNumber' |
|
258 |
} |
|
259 |
] |
|
260 |
}; |
|
261 |
|
|
262 |
$ret = $self->connector->POST('api/search/product', to_json($product_filter)); |
|
263 |
$response_code = $ret->responseCode(); |
|
264 |
die "Request failed, response code was: $response_code\n" . $ret->responseContent() unless $response_code == 200; |
|
265 |
my ($product_id, $category_tree); |
|
266 |
try { |
|
267 |
$product_id = from_json($ret->responseContent())->{data}->[0]->{id}; |
|
268 |
$category_tree = from_json($ret->responseContent())->{data}->[0]->{categoryTree}; |
|
269 |
} catch { die "Malformed JSON Data: $_ " . $ret->responseContent(); }; |
|
270 |
my $cat; |
|
271 |
# if the part is connected to a category at all |
|
272 |
if ($shop_part->shop_category) { |
|
273 |
foreach my $row_cat (@{ $shop_part->shop_category }) { |
|
274 |
$cat->{@{ $row_cat }[0]} = @{ $row_cat }[1]; |
|
275 |
} |
|
276 |
} |
|
277 |
# delete |
|
278 |
foreach my $shopware_cat (@{ $category_tree }) { |
|
279 |
if ($cat->{$shopware_cat}) { |
|
280 |
# cat exists and no delete |
|
281 |
delete $cat->{$shopware_cat}; |
|
282 |
next; |
|
283 |
} |
|
284 |
# cat exists and delete |
|
285 |
$ret = $self->connector->DELETE("api/product/$product_id/categories/$shopware_cat"); |
|
286 |
$response_code = $ret->responseCode(); |
|
287 |
die "Request failed, response code was: $response_code\n" . $ret->responseContent() unless $response_code == 204; |
|
288 |
} |
|
289 |
# now add only new categories |
|
290 |
my $p; |
|
291 |
$p->{id} = $product_id; |
|
292 |
$p->{categories} = (); |
|
293 |
foreach my $new_cat (keys %{ $cat }) { |
|
294 |
push @{ $p->{categories} }, {id => $new_cat}; |
|
295 |
} |
|
296 |
$ret = $self->connector->PATCH("api/product/$product_id", to_json($p)); |
|
297 |
$response_code = $ret->responseCode(); |
|
298 |
die "Request failed, response code was: $response_code\n" . $ret->responseContent() unless $response_code == 204; |
|
299 |
} |
|
245 | 300 |
|
246 | 301 |
sub sync_all_images { |
247 | 302 |
my ($self, %params) = @_; |
Auch abrufbar als: Unified diff
Shopware6: SW-Kategorien mit kivi-Produkten synchronisieren (Löschen, Anlegen)