在 WooCommerce 结账时将客户邮政编码添加到特定的运输方式

Add customer postcode to specific shipping method on WooCommerce checkout

我目前在 functions.php 文件中使用以下代码。

add_action( 'woocommerce_after_shipping_rate', 'action_after_shipping_rate', 20, 2 );
function action_after_shipping_rate ( $method, $index ) {
    // Targeting checkout page only:
    if( is_cart() ) return; // Exit on cart page

    if( 'flat_rate:3' ) {
        echo __("<p>Delivery will take place tomorrow</br> morning between 8-12</p>");
        
    }
    
}

现在我想获取客户邮政编码,然后将其添加到我已有的代码中。

谁能帮我解决这个问题?

您可以使用 WC()->customer->get_shipping_postcode() 添加邮政编码

所以你得到:

function action_woocommerce_after_shipping_rate( $method, $index ) {    
    // Targeting checkout page only
    if ( is_cart() ) return;

    // Get shipping postcode
    $shipping_postcode = WC()->customer->get_shipping_postcode();
    
    // Compare
    if ( $method->get_id() === 'flat_rate:3' ) {
        // Output
        echo '<p>' . __( 'My text + zipcode = ', 'woocommerce') . $shipping_postcode . '</p>';      
    }
}
add_action( 'woocommerce_after_shipping_rate', 'action_woocommerce_after_shipping_rate', 10, 2 );