当我的功能触发时,我购物车中的这两个产品预订范围都应更改为 2018-05-10 10:00 至 2018-05-10 13:30
如果您只想设置一个固定的结束时间(例如13:30
,在您的示例中),那么请尝试以下function
方法,我已经尝试并测试了它在 WordPress 4.9.5 与 WooCommerce 3.3.5 和WooCommerce Bookings 1.11.1 上正常工作。
但是,产品的预订持续时间(在管理员编辑产品页面的常规选项卡中)预计为客户定义的时间段;即类型是,单位是。{n}
customer
hour
// Use `WC_Cart::set_cart_contents()` to actually update the cart (contents).
// See `recalc_bookings_duration()` for an example of how to use this function.
function change_booking_end_time( array &$cart_item, $end_time ) {
if ( ! isset( $cart_item['data'] ) ) {
return false;
}
if ( ! preg_match( '/^\d{2}:\d{2}$/', $end_time ) ) {
return false;
}
if ( ! is_wc_booking_product( $cart_item['data'] ) ) {
//echo 'Not a Bookable product..<br>';
return false;
}
if (
'hour' !== $cart_item['data']->get_duration_unit() ||
'customer' !== $cart_item['data']->get_duration_type()
) {
//echo 'Duration unit/type error.<br>';
return false;
}
$booking_id = $cart_item['booking']['_booking_id'];
$booking = get_wc_booking( $booking_id );
if ( $booking ) {
// Set the end time in 24-hour clock format.
$new_end_time = $end_time;
// Get the timestamp of the `$new_end_time`.
$start_time = $booking->get_start();
$end_time = @strtotime( $new_end_time, $start_time );
if ( ! $end_time ) {
return false;
}
// Update the booking data; in the database.
$_end_time = (int) $booking->get_end();
if ( $end_time !== $_end_time ) {
$booking->set_end( $end_time );
$booking->save();
// Update failed.
$_end_time = (int) $booking->get_end();
if ( $end_time !== $_end_time ) {
//echo '$booking->save() error.<br>';
return false;
}
// The end time / duration already changed.
} else {
//echo 'End time already match.<br>';
return false;
}
// Re-calculate the duration (number of hours).
$duration = abs( $end_time - $start_time ) / ( 60 * 60 );
$cart_item['data']->set_duration( $duration );
// Check/adjust the maximum duration.
$max_duration = $cart_item['data']->get_max_duration();
if ( is_numeric( $max_duration ) && $duration > $max_duration ) {
$cart_item['data']->set_max_duration( ceil( $duration ) );
}
// Check/adjust the minimum duration.
$min_duration = $cart_item['data']->get_min_duration();
if ( is_numeric( $min_duration ) && $duration < $min_duration ) {
$cart_item['data']->set_min_duration( floor( $duration ) );
}
// Update the product data; in the cart.
$cart_item['data']->apply_changes();
// Update the booking data; in the cart.
$cart_item['booking']['_duration'] = $duration;
$cart_item['booking']['duration'] = $duration . ' ' .
_n( 'hour', 'hours', $duration, 'woocommerce-bookings' );
$cart_item['booking']['_end_date'] = $booking->get_end();
return true;
}
}
示例用法
add_action( 'woocommerce_before_calculate_totals', 'recalc_bookings_duration' );
function recalc_bookings_duration( WC_Cart $cart ) {
$cart_items = $cart->get_cart_contents();
$update_cart = false;
foreach ( $cart_items as $cart_item_key => $cart_item ) {
if ( change_booking_end_time( $cart_item, '13:30' ) ) {
$cart_items[ $cart_item_key ] = $cart_item;
$update_cart = true;
}
}
if ( $update_cart ) {
$cart->set_cart_contents( $cart_items );
}
}