Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
- Correction de la recuperation du libéllé du produit ('MISSING LABEL…
Browse files Browse the repository at this point in the history
…' lors de la synchro d'un produit du site vers dolibarr) et que l'on est en multi-langues
  • Loading branch information
kkhelifa-opendsi committed Feb 2, 2024
1 parent 1829f35 commit 44b96d3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 20 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.74
4.1.75
91 changes: 72 additions & 19 deletions class/data/woocommerce/eCommerceRemoteAccessWoocommerce.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 44b96d3

Please sign in to comment.