當購物車上的產品是虛擬的時,我需要停用插件生成的結帳日期選擇器。
這是他們為此提供的鉤子:
apply_filters('woocommerce_delivery_disabled_dates', $disableDates);
基于這些資訊,這是我的代碼嘗試:
add_filter( 'woocommerce_checkout_fields' , 'disable_dates' );
function disable_dates( $fields ) {
$only_virtual = true;
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if there are non-virtual products
if ( ! $cart_item['data']->is_virtual() ) $only_virtual = false;
}
if( $only_virtual ) {
apply_filters(‘woocommerce_delivery_disabled_dates’, $disableDates);
}
return $fields;
}
然而,這并沒有給出預期的結果,當購物車包含虛擬產品時,有什么建議如何隱藏結帳日期選擇器?
uj5u.com熱心網友回復:
主要問題是$disabledDates未定義 - 但是我會更改$fields為,$disableDates因為它更有意義。見下文:
apply_filters('woocommerce_delivery_disabled_dates', $disableDates);
add_filter( 'woocommerce_checkout_fields' , 'disable_dates' );
function disable_dates( $disableDates ) {
$only_virtual = true;
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Check if there are non-virtual products
if ( ! $cart_item['data']->is_virtual() ) $only_virtual = false;
}
if( $only_virtual ) {
apply_filters('woocommerce_delivery_disabled_dates', $disableDates);
}
return $disableDates;
}
該$disableDates變數是您命名的鉤子回呼的輸入引數$fields (我認為)
附言。這只是基于您發布的代碼的猜測。有很多我不清楚,但$disableDates在你的原始代碼中顯然應該有一個值。
uj5u.com熱心網友回復:
無需使用 woocommerce_checkout_fields鉤子,邏輯可以應用在合適的鉤子中。
所以你得到:
function filter_woocommerce_delivery_disabled_dates( $disableDates ) {
// WC Cart NOT null
if ( ! is_null( WC()->cart ) ) {
// Loop through cart items
foreach ( WC()->cart->get_cart_contents() as $cart_item_key => $cart_item ) {
// Check if there are virtual products
if ( $cart_item['data']->is_virtual() ) {
$disableDates = true;
break;
}
}
}
return $disableDates;
}
add_filter( 'woocommerce_delivery_disabled_dates', 'filter_woocommerce_delivery_disabled_dates', 10, 1 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/381607.html
標籤:php WordPress的 求购 产品 大车
