ajout helper nom champs tva

This commit is contained in:
Jean-Philippe Staelen 2026-01-14 13:40:01 +01:00
parent 3f9a113663
commit 2f01441922
2 changed files with 68 additions and 1 deletions

View File

@ -59,7 +59,8 @@ class PEPPOL_Woocommerce_controller {
// N'envoyer vers Peppol que les commandes avec un numéro de TVA saisi ET valide.
// Le plugin "WooCommerce EU VAT Number" stocke le numéro validé dans la meta "_billing_vat_number"
// et le statut de validation dans "_vat_number_is_valid" (valeur 'true' ou 'false').
$billing_vat_number = (string) $order->get_meta('_billing_vat_number');
// Utilisation de la fonction helper pour gérer différentes variantes de clés (billing_vat_number, billing_tva, etc.)
$billing_vat_number = \ESI_PEPPOL\helpers\PEPPOL_Woo_Helper::esi_get_order_vat_number($order);
$vat_is_valid = (string) $order->get_meta('_vat_number_is_valid');
if ($billing_vat_number === '' || $vat_is_valid !== 'true') {

View File

@ -28,4 +28,70 @@ class PEPPOL_Woo_Helper {
return $out;
}
public static function esi_get_order_vat_number( $order ) {
if ( is_numeric( $order ) ) {
$order = wc_get_order( $order );
}
if ( ! $order instanceof \WC_Order ) {
return '';
}
// 1) Clés les plus courantes (ajuste/complète si besoin)
$known_keys = [
'_billing_vat_number',
'billing_vat_number',
'_vat_number',
'vat_number',
'_billing_vat',
'billing_vat',
'_billing_tva',
'billing_tva',
'_tva',
'tva',
'eu_vat_number',
'_eu_vat_number',
'vat_id',
'_vat_id',
];
foreach ( $known_keys as $key ) {
$val = $order->get_meta( $key, true );
if ( ! empty( $val ) ) {
return is_string($val) ? trim($val) : $val;
}
}
// 2) Fallback: scan des meta keys contenant vat/tva
$vat_candidates = [];
foreach ( $order->get_meta_data() as $meta ) {
$k = (string) $meta->key;
if ( preg_match('/\b(vat|tva)\b/i', $k) || preg_match('/(vat|tva)/i', $k) ) {
$v = $meta->value;
if ( is_string($v) ) {
$v = trim($v);
}
if ( ! empty($v) ) {
$vat_candidates[$k] = $v;
}
}
}
// Si plusieurs candidats, tu peux choisir une règle
// Ici: si un seul => return, sinon priorise ceux qui contiennent "billing"
if ( count($vat_candidates) === 1 ) {
return reset($vat_candidates);
}
foreach ( $vat_candidates as $k => $v ) {
if ( stripos($k, 'billing') !== false ) {
return $v;
}
}
// sinon le 1er trouvé
return ! empty($vat_candidates) ? reset($vat_candidates) : '';
}
}