Changeset 2978112
- Timestamp:
- 10/12/2023 12:07:55 PM (2 years ago)
- Location:
- outshifter-export/trunk
- Files:
-
- 2 edited
-
README.txt (modified) (1 diff)
-
index.php (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
outshifter-export/trunk/README.txt
r2974759 r2978112 4 4 Requires at least: 5.0 5 5 Tested up to: 6.2 6 Stable tag: 3. 36 Stable tag: 3.4 7 7 Requires PHP: 7.0 8 8 License: GPLv3 -
outshifter-export/trunk/index.php
r2974759 r2978112 8 8 * Author: Reachu 9 9 * Author URI: https://reachu.io/ 10 * Version: 3. 310 * Version: 3.4 11 11 */ 12 12 … … 21 21 22 22 //QA 23 // define('OSEWCPHJC_API_URL', 'https://api-qa.reachu.io');23 //define('OSEWCPHJC_API_URL', 'https://api-qa.reachu.io'); 24 24 25 25 //LOCAL … … 179 179 return str_replace('http://', 'https://', $imageUrl); 180 180 } 181 return $imageUrl; 181 return $imageUrl; 182 182 } 183 183 … … 232 232 } 233 233 234 private static function deleteProductByPostId($postId, $userApiKey )234 private static function deleteProductByPostId($postId, $userApiKey, $deleteType = null) 235 235 { 236 236 self::log('[deleteProductByPostId] by postId ' . $postId); … … 239 239 if ($productId) { 240 240 self::log('[deleteProductByPostId] deleting productId ' . $productId . ' by postId ' . $postId . '...'); 241 self::call('/api/products/' . $productId, 'DELETE'); 241 if ($deleteType === 'trash') { 242 self::call('/api/products/' . $productId, 'DELETE'); 243 } 242 244 update_post_meta($postId, OSEWCPHJC_FIELD_PRODUCT_ID, ''); 243 245 update_post_meta($postId, OSEWCPHJC_FIELD_SQS_ID, ''); … … 270 272 $currency = get_option('wc_reachu_currency'); 271 273 self::log('[buildProductDto] Currency to use: ' . $currency); 272 $sale_price = ''; 273 if ($product->get_sale_price()) { 274 $sale_price = $product->get_sale_price(); 275 } else if ($product->get_regular_price()) { 276 $sale_price = $product->get_regular_price(); 277 } else { 278 $sale_price = ($product->get_price()); 274 275 $regular_price = $product->get_regular_price(); 276 $sale_price = $product->get_sale_price(); 277 $price_to_use = $regular_price; 278 $compare_at = ''; 279 if ($sale_price && $sale_price < $regular_price) { 280 $price_to_use = $sale_price; 281 $compare_at = $regular_price; 279 282 } 280 283 … … 340 343 $stock = $stock + $stock_variation; 341 344 $title = implode('-', $variation->get_attributes()); 345 $variation_image_array = []; 346 $variation_image_id = $variation->get_image_id(); 347 if ($variation_image_id) { 348 $variation_image_url = wp_get_attachment_image_url($variation_image_id, 'full'); 349 if ($variation_image_url) { 350 $variation_image_url_https = self::forceSecureImage($variation_image_url); 351 $variation_image_array[] = array( 352 "image" => $variation_image_url_https, 353 "order" => 0 354 ); 355 } 356 } 357 $variation_regular_price = $variation->get_regular_price(); 358 $variation_sale_price = $variation->get_sale_price(); 359 $variation_price_to_use = $variation_regular_price; 360 $variation_compare_at = ''; 361 if ($variation_sale_price && $variation_sale_price < $variation_regular_price) { 362 $variation_price_to_use = $variation_sale_price; 363 $variation_compare_at = $variation_regular_price; 364 } 342 365 $variants[] = array( 343 366 "sku" => $variation->get_sku(), 344 "price" => $variation->get_regular_price(), 367 "price" => $variation_price_to_use, 368 "priceCompareAt" => $variation_compare_at, 345 369 "quantity" => $stock_variation, 346 370 "title" => $title, 347 "barcode" => "",348 371 "originId" => $variationId, 372 "images" => $variation_image_array, 349 373 ); 350 374 } … … 354 378 "title" => $product->get_title(), 355 379 "description" => $product->get_description(), 356 "publicPrice" => array( 357 "amount" => $sale_price, 358 "currency" => $currency 380 "price" => array( 381 "amount" => $price_to_use, 382 "compareAt" => $compare_at, 383 "currencyCode" => $currency, 359 384 ), 360 385 "origin" => 'WOOCOMMERCE', … … 381 406 $batchedProducts = []; 382 407 foreach($post_ids as $post_id) { 383 $postDto = self::buildProductDto($post_id);384 $batchedProduct = [385 "product" => $postDto386 ];387 $batchedProducts[] = $batchedProduct;408 $postDto = self::buildProductDto($post_id); 409 $batchedProduct = [ 410 "product" => $postDto 411 ]; 412 $batchedProducts[] = $batchedProduct; 388 413 } 389 414 $payload = [ … … 415 440 $postDto = self::buildProductDto($post_id); 416 441 if ($reachuProductId) { 442 $product = wc_get_product($post_id); 443 if ($product->is_type('variable')) { 444 $externalProduct = self::call('/api/products/' . $reachuProductId); 445 if($externalProduct) { 446 $externalProduct = (array) $externalProduct; 447 $externalVariants = isset($externalProduct['variants']) ? (array) $externalProduct['variants'] : []; 448 foreach($postDto['variants'] as &$variant) { 449 foreach($externalVariants as $externalVariant) { 450 $externalVariant = (array) $externalVariant; 451 if($variant['originId'] == $externalVariant['originId']) { 452 $variant['id'] = $externalVariant['id']; 453 } 454 } 455 } 456 unset($variant); 457 } 458 } 417 459 self::log('[add_update_product] Updating product ' . $reachuProductId . ' from post ' . $post_id . '...'); 418 460 $updated = self::call('/api/products/' . $reachuProductId, 'PUT', $postDto); … … 463 505 { 464 506 self::log('==> reachu_delete_prod called'); 465 507 466 508 if (isset($_POST['reachu_nonce']) && isset($_POST['id_posts'])) { 467 509 if (wp_verify_nonce($_POST['reachu_nonce'], 'reachu_sync')) { … … 471 513 $userApiKey = self::getUserApiKey(); 472 514 if ($userApiKey !== '') { 515 $productIds = []; 516 473 517 foreach ($post_ids as $post_id) { 474 self::deleteProductByPostId($post_id, $userApiKey); 518 $productId = self::getProductId($userApiKey, $post_id); 519 if ($productId !== null) { 520 $productIds[] = $productId; 521 self::deleteProductByPostId($post_id, $userApiKey); 522 } 523 } 524 $idsString = implode(',', $productIds); 525 $apiUrl = '/api/products?ids=' . $idsString; 526 $apiResponse = self::call($apiUrl, 'DELETE'); 527 if ($apiResponse['status'] === 'success') { 528 self::log('[reachu_delete_prod] All products deleted'); 529 } else { 530 self::log('[reachu_delete_prod] Error deleting products'); 475 531 } 476 532 } … … 484 540 self::log('[reachu_delete_prod] Error: Nonce or post IDs not set'); 485 541 } 486 542 487 543 die(); 488 544 } … … 533 589 } 534 590 } 535 self::deleteProductByPostId($postId, $userApiKey );591 self::deleteProductByPostId($postId, $userApiKey, "trash"); 536 592 } 537 593 }
Note: See TracChangeset
for help on using the changeset viewer.