在启用了WooCommerce和WooCommerce插件之后,我尝试在产品时间选择器部分上添加一个文本行,以通知客户他们需要选择其中的一个选项。
我在WooCommerce Bookings插件中找到了一个函数,位于wc-bookings-functions.php php文件中的"includes“文件夹中,我将该函数复制到主题的functions.php文件中,将其重新命名为"de_”,将其置于函数名称的开头,然后添加一个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 .= 'Hi please select your session';
foreach ( $available_blocks as $block => $quantity ) {
if ( $quantity['available'] > 0 ) {
if ( $quantity['booked'] ) {
/* translators: 1: quantity available */
$block_html .= '' . date_i18n( get_option( 'time_format' ), $block ) . ' (' . sprintf( _n( '%d left', '%d left', $quantity['available'], 'woocommerce-bookings' ), absint( $quantity['available'] ) ) . ')';
} else {
$block_html .= '' . date_i18n( get_option( 'time_format' ), $block ) . '';
}
}
}
}
return apply_filters( 'wc_bookings_get_time_slots_html', $block_html, $available_blocks, $blocks );
}添加到我的functions.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 .= 'Hi please select your session';
foreach ( $available_blocks as $block => $quantity ) {
if ( $quantity['available'] > 0 ) {
if ( $quantity['booked'] ) {
/* translators: 1: quantity available */
$block_html .= '' . date_i18n( get_option( 'time_format' ), $block ) . ' (' . sprintf( _n( '%d left', '%d left', $quantity['available'], 'woocommerce-bookings' ), absint( $quantity['available'] ) ) . ')';
} else {
$block_html .= '' . date_i18n( get_option( 'time_format' ), $block ) . '';
}
}
}
}
return apply_filters( 'wc_bookings_get_time_slots_html', $block_html, $available_blocks, $blocks );
}然后是add_filter (de_函数的开头)
add_filter('wc_bookings_get_time_slots_html','de_wc_bookings_get_time_slots_html', 10);我在等电话:
$block_html .= 'Hi please select your session';显示在可用日期之上。
但什么都没发生。当我点击一天,没有日期或我的文本出现。如果我删除了我的函数并过滤了原来的时隙选择器,就会像预期的那样出现。
我读过关于使用过滤器的文档,并认为我这样做是正确的。
我还试着用名字中的“return apply_filters”来更改de_,但没有做任何更改。
发布于 2019-04-14 17:00:24
首先,在原始的wc_bookings_get_time_slots_html()函数中,行如下:
$block_html .= 'Hi please select your session';不存在,因此,对于试图在活动主题的函数php文件中复制(重命名和自定义)原始函数和复制(重命名和自定义)的函数之间的区别的人来说,这是令人困惑的。
现在,<#>copying一个插件函数到您的主题(重命名它并在其中进行自定义) will什么都不做。
相反,可以使用可用的钩子wc_bookings_get_time_slots_html,这将允许您以这样的方式进行定制:
add_filter( 'wc_bookings_get_time_slots_html', 'filter_bookings_get_time_slots_html_callback', 10, 6 );
function filter_bookings_get_time_slots_html_callback( $block_html, $blocks ) {
global $product;
if ( 'customer' !== $product->get_duration_type() ) {
$block_html = 'Hi please select your session' . $block_html;
}
return $block_html;
}代码在您的活动子主题(或活动主题)的function.php文件中。测试和工作。
https://wordpress.stackexchange.com/questions/333263
复制相似问题