Plugin Directory

Changeset 2978112


Ignore:
Timestamp:
10/12/2023 12:07:55 PM (2 years ago)
Author:
outshifter
Message:

update variables sync

Location:
outshifter-export/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • outshifter-export/trunk/README.txt

    r2974759 r2978112  
    44Requires at least: 5.0
    55Tested up to: 6.2
    6 Stable tag: 3.3
     6Stable tag: 3.4
    77Requires PHP: 7.0
    88License: GPLv3
  • outshifter-export/trunk/index.php

    r2974759 r2978112  
    88 * Author: Reachu
    99 * Author URI: https://reachu.io/
    10  * Version: 3.3
     10 * Version: 3.4
    1111 */
    1212
     
    2121
    2222//QA
    23 // define('OSEWCPHJC_API_URL', 'https://api-qa.reachu.io');
     23//define('OSEWCPHJC_API_URL', 'https://api-qa.reachu.io');
    2424
    2525//LOCAL
     
    179179        return str_replace('http://', 'https://', $imageUrl);
    180180      }
    181       return $imageUrl; 
     181      return $imageUrl;
    182182    }
    183183
     
    232232    }
    233233
    234     private static function deleteProductByPostId($postId, $userApiKey)
     234    private static function deleteProductByPostId($postId, $userApiKey, $deleteType = null)
    235235    {
    236236      self::log('[deleteProductByPostId] by postId ' . $postId);
     
    239239      if ($productId) {
    240240        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        }
    242244        update_post_meta($postId, OSEWCPHJC_FIELD_PRODUCT_ID, '');
    243245        update_post_meta($postId, OSEWCPHJC_FIELD_SQS_ID, '');
     
    270272      $currency = get_option('wc_reachu_currency');
    271273      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;
    279282      }
    280283
     
    340343            $stock = $stock + $stock_variation;
    341344            $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            }
    342365            $variants[] = array(
    343366              "sku" => $variation->get_sku(),
    344               "price" => $variation->get_regular_price(),
     367              "price" => $variation_price_to_use,
     368              "priceCompareAt" => $variation_compare_at,
    345369              "quantity" => $stock_variation,
    346370              "title" => $title,
    347               "barcode" => "",
    348371              "originId" => $variationId,
     372              "images" => $variation_image_array,
    349373            );
    350374          }
     
    354378        "title" => $product->get_title(),
    355379        "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,
    359384        ),
    360385        "origin" => 'WOOCOMMERCE',
     
    381406      $batchedProducts = [];
    382407      foreach($post_ids as $post_id) {
    383           $postDto = self::buildProductDto($post_id);
    384           $batchedProduct = [
    385             "product" => $postDto
    386           ];
    387           $batchedProducts[] = $batchedProduct;
     408        $postDto = self::buildProductDto($post_id);
     409        $batchedProduct = [
     410          "product" => $postDto
     411        ];
     412        $batchedProducts[] = $batchedProduct;
    388413      }
    389414      $payload = [
     
    415440        $postDto = self::buildProductDto($post_id);
    416441        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          }
    417459          self::log('[add_update_product] Updating product ' . $reachuProductId . ' from post ' . $post_id . '...');
    418460          $updated = self::call('/api/products/' . $reachuProductId, 'PUT', $postDto);
     
    463505    {
    464506      self::log('==> reachu_delete_prod called');
    465  
     507
    466508      if (isset($_POST['reachu_nonce']) && isset($_POST['id_posts'])) {
    467509        if (wp_verify_nonce($_POST['reachu_nonce'], 'reachu_sync')) {
     
    471513            $userApiKey = self::getUserApiKey();
    472514            if ($userApiKey !== '') {
     515              $productIds = [];
     516
    473517              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');
    475531              }
    476532            }
     
    484540        self::log('[reachu_delete_prod] Error: Nonce or post IDs not set');
    485541      }
    486  
     542
    487543      die();
    488544    }
     
    533589          }
    534590        }
    535         self::deleteProductByPostId($postId, $userApiKey);
     591        self::deleteProductByPostId($postId, $userApiKey, "trash");
    536592      }
    537593    }
Note: See TracChangeset for help on using the changeset viewer.