From 44b96d33c7035d70c012b0683ecc230616487d1f Mon Sep 17 00:00:00 2001 From: Kamel Khelifa Date: Fri, 2 Feb 2024 16:54:12 +0100 Subject: [PATCH] =?UTF-8?q?-=20Correction=20de=20la=20recuperation=20du=20?= =?UTF-8?q?lib=C3=A9ll=C3=A9=20du=20produit=20('MISSING=20LABEL'=20lors=20?= =?UTF-8?q?de=20la=20synchro=20d'un=20produit=20du=20site=20vers=20dolibar?= =?UTF-8?q?r)=20et=20que=20l'on=20est=20en=20multi-langues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ChangeLog.md | 3 + VERSION | 2 +- ...eCommerceRemoteAccessWoocommerce.class.php | 91 +++++++++++++++---- 3 files changed, 76 insertions(+), 20 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 83fe585..2e975eb 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,8 @@ # ChangeLog +## 4.1.75 +- Correction de la recuperation du libéllé du produit ('MISSING LABEL' lors de la synchro d'un produit du site vers dolibarr) et que l'on est en multi-langues + ## 4.1.74 - Correction de l'enregistrement d'un champ complementaire d'un produit modifier (synchro Dolibarr vers Site) diff --git a/VERSION b/VERSION index c276152..bd407c7 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.1.74 \ No newline at end of file +4.1.75 \ No newline at end of file diff --git a/class/data/woocommerce/eCommerceRemoteAccessWoocommerce.class.php b/class/data/woocommerce/eCommerceRemoteAccessWoocommerce.class.php index ea20f68..89cddfe 100755 --- a/class/data/woocommerce/eCommerceRemoteAccessWoocommerce.class.php +++ b/class/data/woocommerce/eCommerceRemoteAccessWoocommerce.class.php @@ -959,6 +959,61 @@ public function checkRemoteProductExist($remote_id) return 1; } + /** + * Get name from remote product data + * + * @param array $remote_data Remote data + * @param array $parent_remote_data Parent remote data if the product is a variation + * @return string|bool FALSE if KO otherwise product name. + */ + public function getNameFromRemoteProductData($remote_data, $parent_remote_data) + { + dol_syslog(__METHOD__ . " remote_data=" . json_encode($remote_data), LOG_DEBUG); + global $conf, $langs; + + $this->errors = array(); + $isVariation = isset($parent_remote_data) || $remote_data['parent_id'] > 0; + $parent_id = isset($parent_remote_data) ? $parent_remote_data['id'] : ($remote_data['parent_id'] > 0 ? $remote_data['parent_id'] : 0); + if ($isVariation && empty($parent_remote_data)) { + $this->errors[] = $langs->trans('ECommerceWoocommerceErrorMissingParentRemoteDate'); + dol_syslog(__METHOD__ . ': Error:' . $this->errorsToString(), LOG_ERR); + return false; + } + + // if the parent has no variations (ex: webhook of a variation transformed in a simple product before the webhook is precessed) + if ($isVariation && empty($parent_remote_data['variations'])) { + $isVariation = false; + $parent_id = 0; + $remote_data = $parent_remote_data; + $parent_remote_data = null; + } + + $product_variation_mode_all_to_one = !empty($this->site->parameters['product_variation_mode']) && $this->site->parameters['product_variation_mode'] == 'all_to_one'; + + // Label + if ($isVariation) { + $label = $parent_remote_data['name']; + if (!$product_variation_mode_all_to_one) { + $label .= ' - '; + // Attributes of the variation + if (!empty($remote_data['name'])) { + $label .= preg_replace('/^' . preg_quote($label) . '/', '', $remote_data['name']); + } elseif (is_array($remote_data['attributes'])) { + $to_print = []; + foreach ($remote_data['attributes'] as $attribute) { + $to_print[] = $attribute['option']; + } + $label .= implode(', ', $to_print); + } + } + } else { + $label = $remote_data['name']; + } +// $label = dol_trunc($label, 255); + + return $label; + } + /** * Call Woocommerce API to get product datas and put into dolibarr product class. * @@ -1016,23 +1071,9 @@ public function convertProductDataIntoProcessedData($remote_data, $parent_remote } // Label - if ($isVariation) { - $label = $parent_remote_data['name']; - if (!$product_variation_mode_all_to_one) { - $label .= ' - '; - // Attributes of the variation - if (!empty($remote_data['name'])) { - $label .= $remote_data['name']; - } elseif (is_array($remote_data['attributes'])) { - $to_print = []; - foreach ($remote_data['attributes'] as $attribute) { - $to_print[] = $attribute['option']; - } - $label .= implode(', ', $to_print); - } - } - } else { - $label = $remote_data['name']; + $label = $this->getNameFromRemoteProductData($remote_data, $parent_remote_data); + if ($label === false) { + return false; } $last_update_product = $this->getDateTimeFromGMTDateTime(!empty($remote_data['date_modified_gmt']) ? $remote_data['date_modified_gmt'] : $remote_data['date_created_gmt']); @@ -1113,8 +1154,20 @@ public function convertProductDataIntoProcessedData($remote_data, $parent_remote $translated_product_data = $this->getProductLanguage($isVariation ? $remote_parent_id : $remote_data['id'], $isVariation ? $remote_data['id'] : 0, $remote_lang); if (!is_array($translated_product_data)) { return false; - } elseif (!empty($translated_product_data)) { - $translated_label = $translated_product_data['name']; + } + $translated_parent_product_data = null; + if ($isVariation) { + $translated_parent_product_data = $this->getProductLanguage($remote_parent_id, 0, $remote_lang); + if (!is_array($translated_product_data)) { + return false; + } + } + + if (!empty($translated_product_data)) { + $translated_label = $this->getNameFromRemoteProductData($translated_product_data, $translated_parent_product_data); + if ($translated_label === false) { + return false; + } $translated_description = $this->replace4byte($translated_product_data['description']); // short_description $found = true; }