[英]Woocommerce Change shipping method title on checkout based on shipping class selected
[英]Change WooCommerce shipping method full label based on product shipping class
我想在 WooCommerce 的購物車頁面上添加自定義消息。該消息應根據分配給產品的運費 class 在woocommerce_cart_shipping_method_full_label
掛鈎中顯示。
我已經有了執行此操作的代碼,但是當我將它分配給該掛鈎時它不起作用,只有當我將它分配給woocommerce_before_calculate_totals
掛鈎時它才起作用。
當我想將它添加到woocommerce_cart_shipping_method_full_label
掛鈎時,我收到消息:
致命錯誤:未捕獲錯誤:調用成員 function get_cart()
有人可以告訴我我做錯了什么嗎? 我正在使用帶有子主題的店面模板。
基於WooCommerce 答案代碼中特定運輸 Class 的購物車消息,這是我在函數中使用的代碼。php:
add_action( 'woocommerce_cart_shipping_method_full_label', 'cart_items_shipping_class_message', 20, 1 );
function cart_items_shipping_class_message( $cart ){
if ( ! is_cart() || ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
return;
if ( did_action( 'woocommerce_cart_shipping_method_full_label' ) >= 2 )
return;
$shipping_class_id = '28'; // Your shipping class Id
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
// Check cart items for specific shipping class, displaying a notice
if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ){
wc_clear_notices();
wc_add_notice( sprintf( __('My custom message.', 'woocommerce'), '' . __("Pallet Shipping", "woocommerce") . ''), 'notice' );
break;
}
}
}
將為一個鈎子編寫的代碼映射到另一個鈎子有時需要進行一些調整,具體取決於原始鈎子和新映射的鈎子:
$cart
未傳遞給回調 function,因此您收到錯誤消息wc_clear_notices()
和wc_add_notice()
不適用,因為您希望使用的掛鈎是更改$label
if ( did_action( 'woocommerce_cart_shipping_method_full_label' ) >= 2 )
不存在所以你得到:
function filter_woocommerce_cart_shipping_method_full_label( $label, $method ) {
// Your shipping class Id
$shipping_class_id = 28;
// WC Cart
if ( WC()->cart ) {
// Get cart
$cart = WC()->cart;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Check cart items for specific shipping class
if ( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) {
$label = __( 'My custom message', 'woocommerce' );
break;
}
}
}
return $label;
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'filter_woocommerce_cart_shipping_method_full_label', 10, 2 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.