تابع wc_postcode_location_matcher پیدا کردن محدوده موقعیت بر اساس کدپستی

تابع wc_postcode_location_matcher پیدا کردن محدوده موقعیت بر اساس کدپستی

تابع ووکامرسی wc_postcode_location_matcher – پیدا کردن محدوده موقعیت بر اساس کدپستی

Syntax – سینتکس

(array) wc_postcode_location_matcher( (string) $postcode, (array) $objects, (string) $object_id_key, (string) $object_compare_key, (string) $country = '' ); 

Parameters – پارامتر ها (5)

  • 1- $postcode (string)
  • 2- $objects (array)
  • 3- $object_id_key (string)
  • 4- $object_compare_key (string)
  • 5- $country (string)

Returns – مقادیر بازگشتی (array)

Array of matching object ID and matching values.

Usage – نحوه استفاده

if ( !function_exists( 'wc_postcode_location_matcher' ) ) { 
    require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-core-functions.php'; 
} 
  
// Postcode you want to match against stored postcodes 
$postcode = ''; 
  
// Array of postcode objects from Database 
$objects = array(); 
  
// DB column name for the ID. 
$object_id_key = ''; 
  
// DB column name for the value. 
$object_compare_key = ''; 
  
// Country from which this postcode belongs. Allows for formatting. 
$country = ''; 
  
// NOTICE! Understand what this does before running. 
$result = wc_postcode_location_matcher($postcode, $objects, $object_id_key, $object_compare_key, $country); 
    

Defined – محل تعریف

/includes/wc-core-functions.php

function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $object_compare_key, $country = '' ) { 
    $postcode = wc_normalize_postcode( $postcode ); 
    $wildcard_postcodes = array_map( 'wc_clean', wc_get_wildcard_postcodes( $postcode, $country ) ); 
    $matches = array(); 
 
    foreach ( $objects as $object ) { 
        $object_id = $object->$object_id_key; 
        $compare_against = $object->$object_compare_key; 
 
        // Handle postcodes containing ranges. 
        if ( strstr( $compare_against, '...' ) ) { 
            $range = array_map( 'trim', explode( '...', $compare_against ) ); 
 
            if ( 2 !== sizeof( $range ) ) { 
                continue; 
            } 
 
            list( $min, $max ) = $range; 
 
            // If the postcode is non-numeric, make it numeric. 
            if ( ! is_numeric( $min ) || ! is_numeric( $max ) ) { 
                $compare = wc_make_numeric_postcode( $postcode ); 
                $min = str_pad( wc_make_numeric_postcode( $min ), strlen( $compare ), '0' ); 
                $max = str_pad( wc_make_numeric_postcode( $max ), strlen( $compare ), '0' ); 
            } else { 
                $compare = $postcode; 
            } 
 
            if ( $compare >= $min && $compare <= $max ) { 
                $matches[ $object_id ]   = isset( $matches[ $object_id ] ) ? $matches[ $object_id ]: array(); 
                $matches[ $object_id ][] = $compare_against; 
            } 
 
        // Wildcard and standard comparison. 
        } elseif ( in_array( $compare_against, $wildcard_postcodes ) ) { 
            $matches[ $object_id ]   = isset( $matches[ $object_id ] ) ? $matches[ $object_id ]: array(); 
            $matches[ $object_id ][] = $compare_against; 
        } 
    } 
 
    return $matches; 
} 

versions – نسخه ها

از نسخه : 2.6.0

نسخه فعلی : 3.0.6

دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2

ارسال نظر

جهت استفاده از کد حتما از تگ pre استفاده نمایید .

contact us