Die Validierung von Postleitzahlen funktioniert nicht für Postleitzahlenbereiche
P粉449281068
P粉449281068 2023-09-08 09:16:07
0
1
522

Versuchen Sie, eine Postleitzahlenvalidierung in Woocommerce zu erstellen.

Ich kann meine Abfrage nicht dazu bringen, Bereichsintervalle wie 10.000-000...15.000-000 zu betrachten.

Meine Abfrage funktioniert für einfache Postleitzahlen (10000-000 oder 11001-001), aber nicht für Postleitzahlenbereiche.

global $wpdb;
    $tabela_cep = $wpdb->prefix . 'woocommerce_shipping_zone_locations';
    $query = "SELECT location_code FROM $tabela_cep WHERE location_code = %s LIMIT 1";
    $resultado = $wpdb->get_var($wpdb->prepare($query, $cep));

Ich weiß nicht, wie Woocommerce alle Sortimente für alle Versandgebiete in der Datenbank speichert.

Ich habe versucht, die Felder „Standortcode“ und „Standortcode2“ mit der Sintax „Zwischen“ zu filtern, bin mir aber nicht sicher, ob dies der richtige Feldname/Tabellenname ist oder ob es einen anderen Weg gibt, dies zu erreichen.

global $wpdb; 
$tabela_cep = $wpdb->prefix . 'woocommerce_shipping_zone_locations'; 
$query = "SELECT location_code FROM $tabela_cep WHERE %s BETWEEN CAST(location_code AS UNSIGNED) AND CAST(location_code2 AS UNSIGNED) LIMIT 1";
$resultado = $wpdb->get_var($wpdb->prepare($query, $cep));
Danke

P粉449281068
P粉449281068

Antworte allen(1)
P粉872182023

我不知道这是否有帮助,但已经有一个名为 wc_postcode_location_matcher() 的核心 WooCommerce 函数,可以检查给定的邮政编码是否在邮政编码范围内。

您首先需要计算 $postcode_locations:

global $wpdb;
$postcode_locations = $wpdb->get_results( "SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';" );

然后您需要 $country,因此如果您在结帐页面上:

$country = WC()->customer->get_billing_country();

...或者如果您正在验证订单:

$country = $order->get_billing_country();

那么你就可以开始了:

$matches = wc_postcode_location_matcher( $postcode, $postcode_locations, 'zone_id', 'location_code', $country );
return $matches; // if empty there is no match
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!