주 코드에서 또는 사용자가 WooCommerce에 입력하는 경우 배송 상태의 이름을 가져옵니다.

Aug 19 2020

제 경우에는 주를 추가하지 않은 국가의 경우 주 이름을 보여주는 다음 코드가 있습니다 (사용자가 수동으로 작성한 경우에만 텍스트가 표시됨).

그러나 사용자가 주가 추가 된 국가를 선택한 경우 주 이름 대신 코드가 표시됩니다.`

<?php
    $custom_order_meta = get_post_meta($order->get_order_number(), '_shipping_state', true);

    if( ! empty($custom_order_meta) ) { ?> <p> <?php printf( '<b>Region / Province:</b> ' . esc_html( '%s', 'woocommerce' ), esc_html($custom_order_meta)  );?> 
</p> <?php 
    }
    ?>

고객 국가 주 이름을 표시하는 Woocommerce 응답 코드 의 코드 대신 주 이름 가져 오기에서 영감을 얻었 지만이 코드는 고객이 수동으로 입력 할 때 처리되지 않습니다.

사용자가 입력 할 때와 사용자가 선택할 때 두 경우 모두 올바르게 작동하도록하려면 어떻게해야합니까?

답변

1 LoicTheAztec Aug 19 2020 at 22:12

다음을 시도하십시오 ( $order변수가 현재 WC_Order개체 로 정의되어 있다고 가정 ) .

<?php
    $shipping_country = $order->get_shipping_country(); $shipping_state   = $order->get_shipping_state(); if( ! empty($shipping_state) ) {
        $country_states = WC()->countries->get_states( $shipping_country );

        $value = isset($country_states[$shipping_state]) ? $country_states[$shipping_state] : $shipping_state;
        
        if( ! empty($value) ) { echo '<p><strong>' . __("Region / Province", "woocommerce") . '</strong>: ' . esc_html($value) . '</p>';
        }
    }
?>

테스트 및 작동합니다.