【问题标题】:Vies VAT Validator checkout WooCommerce竞争增值税验证器结帐 WooCommerce
【发布时间】:2021-07-15 06:19:48
【问题描述】:

有人知道这个增值税验证插件吗? https://wordpress.org/plugins/vies-validator/

这里是使用的库的解释:https://github.com/pH-7/eu-vat-validator

它工作得很好,还因为在我的情况下,我已经将增值税号作为一个字段,因此,由于它允许您将 ID 分配给现有字段的面板,它会对其进行验证。

但是,我想从验证中排除意大利,在插件中我看到验证增值税号的 3 个功能,但我不明白如何排除我想要的国家/地区。

/**
 * Validate the VAT Number
 *
 * @since    1.0.0
 */
public function vies_validator_validate_vat() {
    $enable_vat = get_option($this->option_name . '_add_vat_field');
    if ($enable_vat && $enable_vat == 1) {
        $vat_id = 'vies_billing_vat';
    }
    else {
        $vat_id = get_option($this->option_name . '_vat_id');
    }

    if ($vat_id && !empty(trim($vat_id))) {
        $this->vies_validator_validate_vat_field($vat_id);
    }
}

/**
 * Check a VAT Number via API
 *
 * @since    1.0.0
 */
protected function vatCheck($vat_number) {
    $country = substr($vat_number, 0, 2);
    try {
        $oVatValidator = new Validator(new Europa, $vat_number, $country);
        return $oVatValidator->check();
        }
    catch (Exception $e) {
        return false;
    }
}

/**
 * Validates vat number
 *
 * @since    1.0.0
 */
protected function vies_validator_validate_vat_field($vat_id) {
        if(isset($_POST[$vat_id]) && !empty($_POST[$vat_id])) {
            $vat_number = $_POST[$vat_id];
            if (! $this->vatCheck($vat_number)) {
                wc_add_notice(__(get_option('vies_validator_message'), 'vies-validator'), 'error');
            }
        }
    }

}

只有函数是受保护的和公开的,我不明白如何调用钩子来使函数在 WordPress 函数中运行。

在这里,我看到了正在记录的钩子,并且有 WooCommerce 结帐钩子。

/**
 * Register all of the hooks related to the public-facing 
functionality
 * of the plugin.
 *
 * @since    1.0.0
 * @access   private
 */
private function define_public_hooks() {

    $plugin_public = new Vies_Validator_Public( $this->get_plugin_name(), $this->get_version() );

    $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
    $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );

    if (get_option('vies_validator_add_vat_field') == '1') {
        $this->loader->add_filter('woocommerce_billing_fields', $plugin_public, 'vies_validator_add_vat_field', 10, 1);

        $this->loader->add_filter( 'woocommerce_my_account_my_address_formatted_address', $plugin_public, 'vies_my_account_my_address_formatted_address', 10, 3 );

        $this->loader->add_filter( 'woocommerce_order_formatted_billing_address', $plugin_public, 'vies_add_vat_formatted_billing_address', 10, 2 );

        $this->loader->add_filter( 'woocommerce_formatted_address_replacements', $plugin_public, 'vies_formatted_address_replacements', 10, 2 );

        $this->loader->add_filter( 'woocommerce_customer_meta_fields', $plugin_public, 'vies_customer_meta_fields' );

        $this->loader->add_filter( 'woocommerce_admin_billing_fields', $plugin_public, 'vies_admin_billing_fields' );

        $this->loader->add_filter( 'woocommerce_found_customer_details', $plugin_public, 'vies_found_customer_details' );
    }

    $this->loader->add_action('woocommerce_checkout_process', $plugin_public, 'vies_validator_validate_vat');
}

谢谢

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    如果您只想让代码接受所有意大利增值税号码,那么您只需对增值税检查功能进行小修改:

    protected function vatCheck($vat_number) {
        $country = substr($vat_number, 0, 2);
        if ($country == 'IT' || $country == 'it') {
           return true;
        } else {
           try {
               $oVatValidator = new Validator(new Europa, $vat_number, $country);
               return $oVatValidator->check();
           }
           catch (Exception $e) {
               return false;
           }
        }
    }
    

    如果您希望代码拒绝所有意大利号码,请使用此版本:

    protected function vatCheck($vat_number) {
        $country = substr($vat_number, 0, 2);
        if ($country == 'IT' || $country == 'it') {
           return false;
        } else {
           try {
               $oVatValidator = new Validator(new Europa, $vat_number, $country);
               return $oVatValidator->check();
           }
           catch (Exception $e) {
               return false;
           }
        }
    }
    

    祝你好运。我希望它对你有用。如果没有,那么您可能会发现此增值税验证插件可直接在 Excel 中使用,并且可以进行批量增值税验证:https://www.sanocast.dk/vat-validation/ 太棒了! (我可能有点偏心——这是我自己的创作)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 2019-09-25
      • 1970-01-01
      相关资源
      最近更新 更多