使用邮政编码在管理订单详细信息中显示自定义数据

时间:2020-05-14 作者:Charlie C

所以我只是想显示每个订单的邮政编码位于城市的哪个地区。

我成功地提取了所有正确的数据,并将其分类添加,直到我意识到以下代码只是根据我的帐户保存的邮政编码显示信息。

我需要显示的结果基于客户订单的发货邮政编码。

以下是我所拥有的:

// -------------- DISPLAY CUSTOMER\'S CITY LOCATION BASED ON POSTAL CODE ----------/

add_action( \'woocommerce_admin_order_data_after_shipping_address\', \'end_of_city\' );
        function end_of_city( $order ) { 
        GLOBAL $woocommerce; 
        $customer_postcode = get_user_meta( get_current_user_id(), \'shipping_postcode\', true );

        $northwest = array(\'N6G\',\'N6H\');
        $northcentral = \'N6A\';
        $northeast = array(\'N5Y\', \'N5X\');
        $south = \'N6E\';
        $southwest = array(\'N6L\', \'N6P\');
        $southeast = array(\'N5Z\', \'N6M\', \'N6N\');
        $west = array(\'N6J\',\'N6K\');
        $east = array(\'N5V\',\'N5W\');
        $central = array(\'N6B\',\'N6C\');


            if ( $customer_postcode !== false ) {
                foreach ($northwest as $token) {
                    if (stripos($customer_postcode, $token) !== false) {
                       echo \'<h3>Northwest London</h3>\';
                       return true;
                    } 
                }               
                if (stripos($customer_postcode, $northcentral) !== false) {
                   echo \'<h3>North Central</h3>\';
                   return true;
                }
                foreach ($northeast as $token) {
                    if (stripos($customer_postcode, $token) !== false) {
                       echo \'<h3>Northeast London</h3>\';
                       return true;
                    } 
                }
                if (stripos($customer_postcode, $south) !== false) {
                   echo \'<h3>South London</h3>\';
                   return true;
                }
                foreach ($southwest as $token) {
                    if (stripos($customer_postcode, $token) !== false) {
                       echo \'<h3>Southwest London</h3>\';
                       return true;
                    } 
                }
                foreach ($southeast as $token) {
                    if (stripos($customer_postcode, $token) !== false) {
                       echo \'<h3>Southeast London</h3>\';
                       return true;
                    } 
                }
                foreach ($west as $token) {
                    if (stripos($customer_postcode, $token) !== false) {
                       echo \'<h3>West London</h3>\';
                       return true;
                    } 
                }
                foreach ($east as $token) {
                    if (stripos($customer_postcode, $token) !== false) {
                       echo \'<h3>East London</h3>\';
                       return true;
                    } 
                }
                foreach ($central as $token) {
                    if (stripos($customer_postcode, $token) !== false) {
                       echo \'<h3>Central / Downtown</h3>\';
                       return true;
                    } 
                }
            }
        }
我也尝试过打电话并使用以下功能,但似乎不起作用:

$billing = get_post_meta( $order->id, \'billing_postcode\', true );
        $shipping = get_post_meta( $order->id, \'shipping_postcode\', true );

        if ( empty($shipping) ) {
            $customer_postcode = get_post_meta( $order->id, \'billing_postcode\', true );
        }else {
            $customer_postcode = get_post_meta( $order->id, \'shipping_postcode\', true );
        }

1 个回复
SO网友:Charlie C

通过以下更改/添加解决:

$shipping = $order->get_shipping_postcode();

        if ( empty($shipping) ) {
            $customer_postcode = $order->get_billing_postcode();
        }else {
            $customer_postcode = $order->get_shipping_postcode();
        }