【问题标题】:Add customer postcode to specific shipping method on WooCommerce checkout在 WooCommerce 结帐时将客户邮政编码添加到特定的运输方式
【发布时间】:2021-03-19 19:00:09
【问题描述】:

我目前在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>");
        
    }
    
}

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

谁能帮我解决这个问题?

【问题讨论】:

    标签: php wordpress woocommerce checkout shipping-method


    【解决方案1】:

    您可以使用WC()-&gt;customer-&gt;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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多