簡體   English   中英

WooCommerce 購物車和結帳中用於運輸方式的額外 HTML

[英]Extra HTML for shipping methods in WooCommerce cart and checkout

我正在嘗試將預計到達時間添加到我的運輸方式標簽上。 但是,我希望到達時間在標簽中包含一些額外的 HTML。 (使它們更小和不同的字體粗細)。

這是一個簡單的例子:

function add_estimated_arrival_times($rates, $package){
    
    $groundStuff = $timeReturnedFromAPI; //collect a bunch of cart information, post to the UPS API, return estimated arrival time to $timeReturnedFromAPI
    $ground_rate_id = 'ups:6:09'; //UPS ground rate ID
    
    // Loop through available shipping rates, add arrival time if rate is UPS ground
    foreach ( $rates as $rate_key => $rate ) {
        if( $ground_rate_id === $rate_key ) {
            $rates[$rate_key]->label .= ' <span style="font-size: 12px; font-weight: normal;">(' . $groundStuff . ')</span>'; //<---- problem: HTML is not added
            break;
        }
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates','add_estimated_arrival_times', 50, 2 );

上面的代碼成功地將純文本添加到運輸方式標簽,但 HTML 消失了。

對此有什么好的解決方法?

您使用的鈎子只允許您編輯文本:

function filter_woocommerce_package_rates( $rates, $package ){    
    // Loop through available shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        $rates[$rate_key]->label = __( 'My label', 'woocommerce' );
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates','filter_woocommerce_package_rates', 10, 2 );

所以如果你想在標簽后添加一些東西,你可以通過woocommerce_after_shipping_rate鈎子來做到這一點

function action_woocommerce_after_shipping_rate( $method, $index ) {
    // Target a specific method ID
    if ( $method->id == 'local_pickup:1' ) {
        echo '<p>test</p>';
    } else {
        $groundStuff = 'some value';
        
        echo '<span style="font-size: 12px; font-weight: normal;">(' . $groundStuff . ')</span>';
    }
}
add_action( 'woocommerce_after_shipping_rate', 'action_woocommerce_after_shipping_rate', 10, 2 );

或者

通過在第 40 行覆蓋/cart/cart-shipping.php @version 3.6.0 - 可以通過將其復制到yourtheme/woocommerce/cart/cart-shipping.php來覆蓋此模板

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM