diff --git a/CHANGELOG.md b/CHANGELOG.md index cc7a51d..d46a54d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2022-05-26 4.6.3 +* Optimizing unloading of stock + ## 2022-05-29 4.6.3 * Types of deliveries and payments are displayed only for available stores diff --git a/src/include/class-wc-retailcrm-inventories.php b/src/include/class-wc-retailcrm-inventories.php index d526d96..200ddd9 100644 --- a/src/include/class-wc-retailcrm-inventories.php +++ b/src/include/class-wc-retailcrm-inventories.php @@ -42,7 +42,7 @@ if (!class_exists('WC_Retailcrm_Inventories')) : /** * Load stock from RetailCRM * - * @return mixed + * @return void */ protected function load_stocks() { @@ -70,7 +70,7 @@ if (!class_exists('WC_Retailcrm_Inventories')) : if (isset($offer[$this->bind_field])) { $product = retailcrm_get_wc_product($offer[$this->bind_field], $this->retailcrm_settings); - if ($product instanceof WC_Product) { + if ($product instanceof WC_Product && $product->get_type() != 'external') { if ($product->get_type() == 'variation' || $product->get_type() == 'variable') { $parentId = $product->get_parent_id(); @@ -85,39 +85,44 @@ if (!class_exists('WC_Retailcrm_Inventories')) : $product->set_manage_stock(true); $product->set_stock_quantity($offer['quantity']); - $success[] = $product->save(); + $product->save(); } } } - if (!empty($variationProducts)) { - foreach ($variationProducts as $id => $quantity) { + // Clearing the object cache after calling the function wc_get_products + wp_cache_flush(); + } while ($page <= $totalPageCount); + + if (!empty($variationProducts)) { + $chunks = array_chunk($variationProducts, 100, true); + + foreach ($chunks as $chunk) { + foreach ($chunk as $id => $quantity) { $variationProduct = wc_get_product($id); if (is_object($variationProduct)) { $variationProduct->set_manage_stock(true); $variationProduct->set_stock_quantity($quantity); - $success[] = $variationProduct->save(); + $variationProduct->save(); } } - } - } while ($page <= $totalPageCount); - return $success; + wp_cache_flush(); + } + } } /** * Update stock quantity in WooCommerce * - * @return mixed + * @return void */ public function updateQuantity() { if ($this->retailcrm_settings['sync'] == WC_Retailcrm_Base::YES) { - return $this->load_stocks(); + $this->load_stocks(); } - - return false; } } endif; diff --git a/src/readme.txt b/src/readme.txt index d4feff6..604d7d3 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -82,6 +82,9 @@ Asegúrate de tener una clave API específica para cada tienda. Las siguientes i == Changelog == += 4.6.3 = +* Optimizing unloading of stock + = 4.6.3 = * Types of deliveries and payments are displayed only for available stores diff --git a/tests/test-wc-retailcrm-inventories.php b/tests/test-wc-retailcrm-inventories.php index 79a5360..4cdda9f 100644 --- a/tests/test-wc-retailcrm-inventories.php +++ b/tests/test-wc-retailcrm-inventories.php @@ -57,9 +57,9 @@ class WC_Retailcrm_Inventories_Test extends WC_Retailcrm_Test_Case_Helper } $retailcrm_inventories = new WC_Retailcrm_Inventories($retailcrm); - $result = $retailcrm_inventories->updateQuantity(); + $retailcrm_inventories->updateQuantity(); - $this->checkProductData($retailcrm, $response, $result, 'simple'); + $this->checkProductData($retailcrm, $response, $offer->get_id(), 'simple'); } /** @@ -86,9 +86,9 @@ class WC_Retailcrm_Inventories_Test extends WC_Retailcrm_Test_Case_Helper } $retailcrm_inventories = new WC_Retailcrm_Inventories($retailcrm); - $result = $retailcrm_inventories->updateQuantity(); + $retailcrm_inventories->updateQuantity(); - $this->checkProductData($retailcrm, $response, $result, 'variation'); + $this->checkProductData($retailcrm, $response, $childrens[0], 'variation'); } public function test_sync_off() @@ -104,19 +104,16 @@ class WC_Retailcrm_Inventories_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals(false, $result); } - private function checkProductData($retailcrm, $response, $result, $entity) + private function checkProductData($retailcrm, $response, $offerId, $entity) { + $product = wc_get_product($offerId); + if ($retailcrm && null !== $response) { - $product = wc_get_product($result[0]); $this->assertInstanceOf('WC_Product', $product); $this->assertEquals($entity, $product->get_type()); $this->assertEquals(10, $product->get_stock_quantity()); - $this->assertEquals($result[0], $product->get_id()); - $this->assertInternalType('array', $result); - } elseif (null === $response) { - $this->assertEquals(false, $result); } else { - $this->assertEquals(null, $result); + $this->assertNotEquals(10, $product->get_stock_quantity()); } }