self::BLOCK_SIRET, 'label' => __( 'SIRET (businesses only)', 'factella' ), 'optionalLabel' => __( 'SIRET (businesses only, optional)', 'factella' ), 'location' => 'order', 'type' => 'text', 'required' => false, 'sanitize_callback' => array( 'EFacture_FR_Identifiers', 'normalize' ), 'validate_callback' => static function ( $value ) { if ( '' !== $value && ! EFacture_FR_Identifiers::is_valid_siret( $value ) ) { return new WP_Error( 'efacture_fr_invalid_siret', __( 'The SIRET number is not valid. Please check the 14 digits.', 'factella' ) ); } return true; }, ) ); woocommerce_register_additional_checkout_field( array( 'id' => self::BLOCK_VAT, 'label' => __( 'VAT number (businesses only)', 'factella' ), 'optionalLabel' => __( 'VAT number (businesses only, optional)', 'factella' ), 'location' => 'order', 'type' => 'text', 'required' => false, 'sanitize_callback' => array( 'EFacture_FR_Identifiers', 'normalize' ), 'validate_callback' => static function ( $value ) { if ( '' !== $value && ! EFacture_FR_Identifiers::looks_like_eu_vat( $value ) ) { return new WP_Error( 'efacture_fr_invalid_vat', __( 'The VAT number is not valid.', 'factella' ) ); } return true; }, ) ); } /** * Add fields to the classic checkout. * * @param array $fields Checkout fields. * @return array */ public static function add_classic_fields( $fields ) { $fields['billing']['billing_efacture_siret'] = array( 'label' => __( 'SIRET (businesses only)', 'factella' ), 'required' => false, 'class' => array( 'form-row-first' ), 'priority' => 35, ); $fields['billing']['billing_efacture_vat'] = array( 'label' => __( 'VAT number (businesses only)', 'factella' ), 'required' => false, 'class' => array( 'form-row-last' ), 'priority' => 36, ); return $fields; } /** * Validate classic checkout values. * * @param array $data Posted data. * @param WP_Error $errors Errors. */ public static function validate_classic( $data, $errors ) { $siret = isset( $data['billing_efacture_siret'] ) ? EFacture_FR_Identifiers::normalize( $data['billing_efacture_siret'] ) : ''; $vat = isset( $data['billing_efacture_vat'] ) ? EFacture_FR_Identifiers::normalize( $data['billing_efacture_vat'] ) : ''; if ( '' !== $siret && ! EFacture_FR_Identifiers::is_valid_siret( $siret ) ) { $errors->add( 'efacture_fr_invalid_siret', __( 'The SIRET number is not valid. Please check the 14 digits.', 'factella' ) ); } if ( '' !== $vat && ! EFacture_FR_Identifiers::looks_like_eu_vat( $vat ) ) { $errors->add( 'efacture_fr_invalid_vat', __( 'The VAT number is not valid.', 'factella' ) ); } } /** * Store classic checkout values on the order. * * @param WC_Order $order Order being created. * @param array $data Posted data. */ public static function save_classic( $order, $data ) { if ( ! empty( $data['billing_efacture_siret'] ) ) { $order->update_meta_data( self::CLASSIC_META_SIRET, EFacture_FR_Identifiers::normalize( $data['billing_efacture_siret'] ) ); } if ( ! empty( $data['billing_efacture_vat'] ) ) { $order->update_meta_data( self::CLASSIC_META_VAT, EFacture_FR_Identifiers::normalize( $data['billing_efacture_vat'] ) ); } } /** * Read the buyer identifiers from an order, whichever checkout was used. * * @param WC_Order $order Order. * @return array{siret:string,siren:string,vat:string} */ public static function get_buyer_ids( $order ) { $siret = (string) $order->get_meta( self::BLOCK_META_SIRET ); if ( '' === $siret ) { $siret = (string) $order->get_meta( self::CLASSIC_META_SIRET ); } $vat = (string) $order->get_meta( self::BLOCK_META_VAT ); if ( '' === $vat ) { $vat = (string) $order->get_meta( self::CLASSIC_META_VAT ); } return array( 'siret' => $siret, 'siren' => '' !== $siret ? EFacture_FR_Identifiers::siren_from_siret( $siret ) : '', 'vat' => $vat, ); } }