feat: customer lookup by NIF with default customer fallback
- Search BC customers by taxRegistrationNumber (NIF/CIF from _nif meta) - If NIF found in BC, use that customer; if not, create with NIF - If no NIF on order, use configurable default customer number - Add Default Customer Number setting in Order Settings tab Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -94,6 +94,9 @@ class WBC_Admin {
|
||||
register_setting( 'wbc_orders', 'wbc_shipping_item_number', array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
) );
|
||||
register_setting( 'wbc_orders', 'wbc_default_customer_number', array(
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -328,6 +328,19 @@ $tabs = array(
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">
|
||||
<label for="wbc_default_customer_number"><?php esc_html_e( 'Default Customer Number', 'woo-business-central' ); ?></label>
|
||||
</th>
|
||||
<td>
|
||||
<input type="text" id="wbc_default_customer_number" name="wbc_default_customer_number"
|
||||
value="<?php echo esc_attr( get_option( 'wbc_default_customer_number', '' ) ); ?>"
|
||||
class="regular-text" />
|
||||
<p class="description">
|
||||
<?php esc_html_e( 'BC customer number to use for orders without NIF/CIF. Orders with NIF will look up or create a specific customer.', 'woo-business-central' ); ?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p class="submit">
|
||||
|
||||
@@ -20,24 +20,41 @@ class WBC_Customer_Sync {
|
||||
/**
|
||||
* Get or create a customer in Business Central
|
||||
*
|
||||
* Lookup priority:
|
||||
* 1. If NIF provided → search BC by taxRegistrationNumber, create if not found
|
||||
* 2. If no NIF → use configured default customer number
|
||||
*
|
||||
* @param WC_Order $order WooCommerce order.
|
||||
* @return string|WP_Error BC customer number or error.
|
||||
*/
|
||||
public function get_or_create_customer( $order ) {
|
||||
$email = $order->get_billing_email();
|
||||
$nif = $order->get_meta( '_nif' );
|
||||
$user_id = $order->get_user_id();
|
||||
|
||||
if ( empty( $email ) ) {
|
||||
return new WP_Error( 'wbc_no_email', __( 'Order has no billing email.', 'woo-business-central' ) );
|
||||
// No NIF → use default customer number
|
||||
if ( empty( $nif ) ) {
|
||||
$default_customer = get_option( 'wbc_default_customer_number', '' );
|
||||
|
||||
if ( empty( $default_customer ) ) {
|
||||
return new WP_Error( 'wbc_no_nif_no_default', __( 'Order has no NIF and no default customer number is configured.', 'woo-business-central' ) );
|
||||
}
|
||||
|
||||
WBC_Logger::info( 'CustomerSync', 'No NIF provided, using default customer', array(
|
||||
'order_id' => $order->get_id(),
|
||||
'customer_number' => $default_customer,
|
||||
) );
|
||||
|
||||
return $default_customer;
|
||||
}
|
||||
|
||||
WBC_Logger::debug( 'CustomerSync', 'Getting or creating customer', array( 'email' => $email ) );
|
||||
WBC_Logger::debug( 'CustomerSync', 'Getting or creating customer by NIF', array(
|
||||
'nif' => $nif,
|
||||
) );
|
||||
|
||||
// Check if we have a cached BC customer ID for this user
|
||||
$user_id = $order->get_user_id();
|
||||
// Check cached BC customer number for this user
|
||||
if ( $user_id ) {
|
||||
$cached_customer_number = get_user_meta( $user_id, '_wbc_bc_customer_number', true );
|
||||
if ( ! empty( $cached_customer_number ) ) {
|
||||
// Verify the customer still exists in BC
|
||||
$exists = $this->verify_customer_exists( $cached_customer_number );
|
||||
if ( $exists ) {
|
||||
WBC_Logger::debug( 'CustomerSync', 'Using cached BC customer', array(
|
||||
@@ -48,8 +65,8 @@ class WBC_Customer_Sync {
|
||||
}
|
||||
}
|
||||
|
||||
// Try to find existing customer in BC by email
|
||||
$existing = $this->find_customer_by_email( $email );
|
||||
// Search BC for customer with this NIF
|
||||
$existing = $this->find_customer_by_nif( $nif );
|
||||
|
||||
if ( is_wp_error( $existing ) ) {
|
||||
return $existing;
|
||||
@@ -58,21 +75,20 @@ class WBC_Customer_Sync {
|
||||
if ( $existing ) {
|
||||
$customer_number = $existing['number'];
|
||||
|
||||
// Cache the customer number for this user
|
||||
if ( $user_id ) {
|
||||
update_user_meta( $user_id, '_wbc_bc_customer_id', $existing['id'] );
|
||||
update_user_meta( $user_id, '_wbc_bc_customer_number', $customer_number );
|
||||
}
|
||||
|
||||
WBC_Logger::info( 'CustomerSync', 'Found existing BC customer', array(
|
||||
'email' => $email,
|
||||
WBC_Logger::info( 'CustomerSync', 'Found existing BC customer by NIF', array(
|
||||
'nif' => $nif,
|
||||
'customer_number' => $customer_number,
|
||||
) );
|
||||
|
||||
return $customer_number;
|
||||
}
|
||||
|
||||
// Create new customer in BC
|
||||
// Create new customer in BC with NIF
|
||||
$new_customer = $this->create_customer( $order );
|
||||
|
||||
if ( is_wp_error( $new_customer ) ) {
|
||||
@@ -81,14 +97,13 @@ class WBC_Customer_Sync {
|
||||
|
||||
$customer_number = $new_customer['number'];
|
||||
|
||||
// Cache the customer number for this user
|
||||
if ( $user_id ) {
|
||||
update_user_meta( $user_id, '_wbc_bc_customer_id', $new_customer['id'] );
|
||||
update_user_meta( $user_id, '_wbc_bc_customer_number', $customer_number );
|
||||
}
|
||||
|
||||
WBC_Logger::info( 'CustomerSync', 'Created new BC customer', array(
|
||||
'email' => $email,
|
||||
WBC_Logger::info( 'CustomerSync', 'Created new BC customer with NIF', array(
|
||||
'nif' => $nif,
|
||||
'customer_number' => $customer_number,
|
||||
) );
|
||||
|
||||
@@ -96,13 +111,13 @@ class WBC_Customer_Sync {
|
||||
}
|
||||
|
||||
/**
|
||||
* Find customer in BC by email
|
||||
* Find customer in BC by NIF (taxRegistrationNumber)
|
||||
*
|
||||
* @param string $email Customer email.
|
||||
* @param string $nif Tax registration number (NIF/CIF/NIE).
|
||||
* @return array|false|WP_Error Customer data, false if not found, or error.
|
||||
*/
|
||||
private function find_customer_by_email( $email ) {
|
||||
$filter = "email eq '" . WBC_API_Client::escape_odata_string( $email ) . "'";
|
||||
private function find_customer_by_nif( $nif ) {
|
||||
$filter = "taxRegistrationNumber eq '" . WBC_API_Client::escape_odata_string( $nif ) . "'";
|
||||
$result = WBC_API_Client::get_customers( $filter );
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
@@ -203,6 +218,12 @@ class WBC_Customer_Sync {
|
||||
'email' => $order->get_billing_email(),
|
||||
);
|
||||
|
||||
// Add NIF as tax registration number
|
||||
$nif = $order->get_meta( '_nif' );
|
||||
if ( ! empty( $nif ) ) {
|
||||
$customer_data['taxRegistrationNumber'] = $nif;
|
||||
}
|
||||
|
||||
// Add optional fields if present
|
||||
$phone = $order->get_billing_phone();
|
||||
if ( ! empty( $phone ) ) {
|
||||
|
||||
Reference in New Issue
Block a user