随着WooCommerce和WooCommerce预订插件的启用,我试图在产品时间选择器部分上方添加一行文本,以告知客户他们需要选择其中一个选项。
我在WooCommerce Bookings插件中找到了一个函数,该插件位于wc-bookings-functions.php
php文件,我已将其复制到主题的functions.php
文件,通过在函数名的开头加上前缀“de\\u”,对其进行重命名,然后添加add_filter
.
当我这样做时,函数(原始插件或我的插件)根本不起作用,要么显示原始选择器,要么显示我的版本,什么都不返回。
原始功能:
function wc_bookings_get_time_slots_html( $bookable_product, $blocks, $intervals = array(), $resource_id = 0, $from = 0, $to = 0 ) {
$available_blocks = wc_bookings_get_time_slots( $bookable_product, $blocks, $intervals, $resource_id, $from, $to );
$block_html = \'\';
// If customer defined, we show two dropdowns start/end time.
if ( \'customer\' === $bookable_product->get_duration_type() ) {
$block_html .= wc_bookings_get_start_time_html( $bookable_product, $blocks, $intervals, $resource_id, $from, $to );
$block_html .= wc_bookings_get_end_time_html( $bookable_product, $blocks, \'\', $intervals, $resource_id, $from, $to );
} else {
$block_html .= \'<div class="please-select">Hi please select your session</div>\';
foreach ( $available_blocks as $block => $quantity ) {
if ( $quantity[\'available\'] > 0 ) {
if ( $quantity[\'booked\'] ) {
/* translators: 1: quantity available */
$block_html .= \'<li class="block" data-block="\' . esc_attr( date( \'Hi\', $block ) ) . \'"><a href="#" data-value="\' . get_time_as_iso8601( $block ) . \'">\' . date_i18n( get_option( \'time_format\' ), $block ) . \' <small class="booking-spaces-left">(\' . sprintf( _n( \'%d left\', \'%d left\', $quantity[\'available\'], \'woocommerce-bookings\' ), absint( $quantity[\'available\'] ) ) . \')</small></a></li>\';
} else {
$block_html .= \'<li class="block" data-block="\' . esc_attr( date( \'Hi\', $block ) ) . \'"><a href="#" data-value="\' . get_time_as_iso8601( $block ) . \'">\' . date_i18n( get_option( \'time_format\' ), $block ) . \'</a></li>\';
}
}
}
}
return apply_filters( \'wc_bookings_get_time_slots_html\', $block_html, $available_blocks, $blocks );
}
我添加到函数中的函数。php文件。我添加了新功能
de_:
function de_wc_bookings_get_time_slots_html( $bookable_product, $blocks, $intervals = array(), $resource_id = 0, $from = 0, $to = 0 ) {
$available_blocks = wc_bookings_get_time_slots( $bookable_product, $blocks, $intervals, $resource_id, $from, $to );
$block_html = \'\';
// If customer defined, we show two dropdowns start/end time.
if ( \'customer\' === $bookable_product->get_duration_type() ) {
$block_html .= wc_bookings_get_start_time_html( $bookable_product, $blocks, $intervals, $resource_id, $from, $to );
$block_html .= wc_bookings_get_end_time_html( $bookable_product, $blocks, \'\', $intervals, $resource_id, $from, $to );
} else {
$block_html .= \'<div class="please-select">Hi please select your session</div>\';
foreach ( $available_blocks as $block => $quantity ) {
if ( $quantity[\'available\'] > 0 ) {
if ( $quantity[\'booked\'] ) {
/* translators: 1: quantity available */
$block_html .= \'<li class="block" data-block="\' . esc_attr( date( \'Hi\', $block ) ) . \'"><a href="#" data-value="\' . get_time_as_iso8601( $block ) . \'">\' . date_i18n( get_option( \'time_format\' ), $block ) . \' <small class="booking-spaces-left">(\' . sprintf( _n( \'%d left\', \'%d left\', $quantity[\'available\'], \'woocommerce-bookings\' ), absint( $quantity[\'available\'] ) ) . \')</small></a></li>\';
} else {
$block_html .= \'<li class="block" data-block="\' . esc_attr( date( \'Hi\', $block ) ) . \'"><a href="#" data-value="\' . get_time_as_iso8601( $block ) . \'">\' . date_i18n( get_option( \'time_format\' ), $block ) . \'</a></li>\';
}
}
}
}
return apply_filters( \'wc_bookings_get_time_slots_html\', $block_html, $available_blocks, $blocks );
}
然后是add\\u过滤器(
de_ 功能开始)
add_filter(\'wc_bookings_get_time_slots_html\',\'de_wc_bookings_get_time_slots_html\', 10);
我在等电话:
$block_html .= \'<div class="please-select">Hi please select your session</div>\';
显示在可用日期上方。
但什么都没发生。当我单击某一天时,不会显示日期或我的文本。如果我删除我的函数并过滤,原始时隙选择器将按预期显示。
我已经阅读了有关使用过滤器的文档,并认为我这样做是正确的。
我还试着改变return apply_filters
名称中有一个前缀“de\\u0”,但没有做任何更改。