From 84d36f7c334d2a09b005d58773156c3cfd7b7b74 Mon Sep 17 00:00:00 2001
From: Dima Uryvskiy <dima-uryvskiyy@rambler.ru>
Date: Mon, 15 Mar 2021 18:18:55 +0300
Subject: [PATCH] Add display of the total number of product variables

---
 CHANGELOG.md                                   |  8 ++++++++
 VERSION                                        |  2 +-
 src/include/class-wc-retailcrm-inventories.php | 18 ++++++++++++++++--
 src/readme.txt                                 |  7 +++++++
 src/retailcrm.php                              |  2 +-
 src/uninstall.php                              |  2 +-
 6 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index dd56a4b..e0cdd44 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,11 @@
+## 2021-03-15 4.2.4
+* Добавили отображение общего количества вариативных товаров
+* Добавили валидацию на дату создания заказа
+* Добавили валидацию на заказы с auto-draft статусом
+* Обновили версии WP и WC в локальных тестах
+* Исправили баг в опции "Передача номера заказа"
+
+
 ## 2021-01-20 4.2.3
 * Обновление версии в маркетплейсе
 
diff --git a/VERSION b/VERSION
index f2c6cb6..cf78d5b 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-4.2.3
+4.2.4
diff --git a/src/include/class-wc-retailcrm-inventories.php b/src/include/class-wc-retailcrm-inventories.php
index 01846ba..894cfc4 100644
--- a/src/include/class-wc-retailcrm-inventories.php
+++ b/src/include/class-wc-retailcrm-inventories.php
@@ -53,6 +53,7 @@ if (!class_exists('WC_Retailcrm_Inventories')) :
             }
 
             $page = 1;
+            $variationProducts = array();
 
             do {
                 /** @var WC_Retailcrm_Response $result */
@@ -70,8 +71,14 @@ if (!class_exists('WC_Retailcrm_Inventories')) :
                         $product = retailcrm_get_wc_product($offer[$this->bind_field], $this->retailcrm_settings);
 
                         if ($product instanceof WC_Product) {
-                            if ($product->get_type() == 'variable') {
-                                continue;
+                            if ($product->get_type() == 'variation' || $product->get_type() == 'variable') {
+                                $parentId = $product->get_parent_id();
+
+                                if (isset($variationProducts[$parentId])) {
+                                    $variationProducts[$parentId] += $offer['quantity'];
+                                } else {
+                                    $variationProducts[$parentId] = $offer['quantity'];
+                                }
                             }
 
                             $product->set_manage_stock(true);
@@ -80,6 +87,13 @@ if (!class_exists('WC_Retailcrm_Inventories')) :
                         }
                     }
                 }
+
+                foreach ($variationProducts as $id => $quantity) {
+                    $variationProduct = wc_get_product($id);
+                    $variationProduct->set_manage_stock(true);
+                    $variationProduct->set_stock($quantity);
+                    $success[] = $variationProduct->save();
+                }
             } while ($page <= $totalPageCount);
 
             return $success;
diff --git a/src/readme.txt b/src/readme.txt
index bfb2bcb..9edbfcb 100644
--- a/src/readme.txt
+++ b/src/readme.txt
@@ -83,6 +83,13 @@ Asegúrate de tener una clave API específica para cada tienda. Las siguientes i
 
 == Changelog ==
 
+= 4.2.4 =
+* Добавили отображение общего количества вариативных товаров
+* Добавили валидацию на дату создания заказа
+* Добавили валидацию на заказы с auto-draft статусом
+* Обновили версии WP и WC в локальных тестах
+* Исправили баг в опции "Передача номера заказа"
+
 = 4.2.3=
 * Обновление версии в маркетплейсе
 
diff --git a/src/retailcrm.php b/src/retailcrm.php
index 2424522..00ef262 100644
--- a/src/retailcrm.php
+++ b/src/retailcrm.php
@@ -1,6 +1,6 @@
 <?php
 /**
- * Version: 4.2.3
+ * Version: 4.2.4
  * WC requires at least: 3.0
  * WC tested up to: 4.7.1
  * Plugin Name: WooCommerce RetailCRM
diff --git a/src/uninstall.php b/src/uninstall.php
index 3ef7dee..6f5c473 100644
--- a/src/uninstall.php
+++ b/src/uninstall.php
@@ -15,7 +15,7 @@
  *
  *
  * @link       https://wordpress.org/plugins/woo-retailcrm/
- * @version    4.2.3
+ * @version    4.2.4
  *
  * @package    RetailCRM
  */