programing

WooCommerce의 국가 코드에서 국가 이름을 가져오는 중

goodcopy 2023. 3. 29. 23:48
반응형

WooCommerce의 국가 코드에서 국가 이름을 가져오는 중

WooCommerce는 다음과 같이 국가를 정의합니다(간단하게 편집).

class WC_Countries {
    public $countries;

    public function __construct() {
        global $woocommerce, $states;

        $this->countries = apply_filters( 'woocommerce_countries', array(
            'AF' => __( 'Afghanistan', 'woocommerce' ),
            'AX' => __( 'Åland Islands', 'woocommerce' ),
            'AL' => __( 'Albania', 'woocommerce' ),
            'DZ' => __( 'Algeria', 'woocommerce' ),
            // […]
        ));
    }
}

주문이 이루어지면 국가 코드가 WordPress에 기록됩니다.wp_postmeta테이블을 사용하여 주문 ID에 액세스할 수 있는 모든 위치에서 추출할 수 있습니다.get_post_meta()기능:

get_post_meta( $order->id, '_shipping_country', true ),

데이터베이스로부터 2글자를 취득하고 있기 때문에, 배송 국가 코드를 어떻게 번역하는지가 문제입니다(예:AF)에서 지정한 국가명으로 이행합니다.WC_Countries수업?

에 액세스 할 수 있습니다.WC_Countries와 같은 반을 나누다.WC()->countries주문에서 국가 이름을 가져오려면 다음을 사용해야 합니다.

WC()->countries->countries[ $order->shipping_country ];

WooCommerce 3.0+에서는 다음을 사용해야 합니다.

WC()->countries->countries[ $order->get_shipping_country() ];

상태를 취득하고 싶은 경우는, WooCommerce에 모든 상태가 포함되어 있는 것은 아니기 때문에, 사전에 체크할 필요가 있습니다.따라서 필요한 것은 다음과 같습니다.

$states = WC()->countries->get_states( $order->get_shipping_country() );
$state  = ! empty( $states[ $order->get_shipping_state() ] ) ? $states[ $order->get_shipping_state() ] : '';

사용할 수 있는 상태 코드에서 상태 이름을 가져옵니다.

WC()->countries->states[$order->billing_country][$order->billing_state];

카트나 주문주소가 아닌 Woocommerce 의 기본주소를 어떻게 얻을 수 있는지 알아보고 있었는데, 이 스레드는 토픽과 관련된 구글 키워드가 많습니다.저는 다음과 같은 답변에 도달했습니다.이 답변은 조금 업데이트 되었습니다.https://wordpress.stackexchange.com/a/334608/177690

private static function afg_getBaseAddressData() {
    return array(
        'address' => WC()->countries->get_base_address(),
        'address-2' => WC()->countries->get_base_address_2(),
        'city' => WC()->countries->get_base_city(),
        'zip' => WC()->countries->get_base_postcode(),
        'state' => WC()->countries->get_base_state(),
        'country' => WC()->countries->countries[WC()->countries->get_base_country()],
        'mail' => self::afg_getPublicMail()
    );
}

private static function afg_getPublicMail() {
    //replace by the way you get an email address
    return filter_var(get_option('address-public-mail'), FILTER_VALIDATE_EMAIL);
}

public static function afg_getAddressTemplate() {
    $datas = apply_filters( 'afg_shop_base_address', self::afg_getBaseAddressData() );
    $html = '';

    $html .= '<address>'; 
    foreach ($datas as $key => $data) {
        if($data) {
            $html .= '<p class="address-line '.$key.'">'.$data.'</p>';
        }
    }
    $html .= '</address>';

    return $html;
}
global $woocommerce;
$shipping_country = WC()->customer->get_shipping_country();
function a000_remove_bundles_counting(){
//////////////////////////////
global $woocommerce_bundles;
remove_filter( 'woocommerce_cart_contents_count',
array( $woocommerce_bundles->display, 'woo_bundles_cart_contents_count' ) );
}
add_action( 'init', 'a000_remove_bundles_counting' );
///////////////////////////////////////////////////

//////////////////////////////////////////////////////

function d000_cart_contents_count( $count ) {
    global $woocommerce;
 $cat_check = false;
  foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) { //foreach

 $product = $cart_item['data'];

 if ( has_term( 'VIP', 'product_tag', $product->id ) ) {//search product_cat
$cat_check = true;
// break because we only need one "true" to matter here
if (!function_exists('woo_override_checkout_fields')) {
function woo_override_checkout_fields( $fields ) { // woo_override_checkout_fields Function


$fields['billing']['billing_country'] = array(
'type' => 'select',
'label' => __('Country', 'woocommerce'),
'options' => array('US' => 'United States(US)')
); 

$fields['billing']['billing_state'] = array(
'type' => 'select',
'label' => __('State', 'woocommerce'),
'options' => array('CA' => 'California(CA)')

);

return $fields; 
} //end woo_override_checkout_fields Function
}
add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields' );

} // end search product_cat 
  }// end foreach



return $count;
}
add_filter( 'woocommerce_cart_contents_count',
'd000_cart_contents_count' );

먼저 제품 태그 "VIP 등 원하는 것을 설정한 후 코드에 추가합니다.

if ( has_term( 'VIP', 'product_tag', $product->id ) ) {//search product_cat 

    $cat_check = true;
}

이 기능에서 "VIP" 제품 태그를 가진 제품이 있는지 확인합니다.$cat_check = true;

그리고 이 기능 안에서 우리는 기능을 추가한다.woo_override_checkout_fields( $fields )제한 국가를 배송 국가 필드로 설정하는 함수입니다.

안에서.woo_override_checkout_fields( $fields ) { }

표시할 국가 설정

$fields['billing']['billing_country'] = array(
'type' => 'select',
'label' => __('Country', 'woocommerce'),
'options' => array('US' => 'United States(US)')
);



$fields['billing']['billing_state'] = array(
'type' => 'select',
'label' => __('State', 'woocommerce'),
'options' => array('CA' => 'California(CA)')

);

언급URL : https://stackoverflow.com/questions/25528454/getting-country-name-from-country-code-in-woocommerce

반응형