Php Woocommerce预订-如何更改购物车中已存在的项目元?
我正在尝试更改所有可预订产品的meta,这些产品已经在购物车中,更具体地说是-结束日期和持续时间,但在几天内都没有结果。。。我需要在计算总数之前在woocommerce_中完成。我尝试过以下代码:Php Woocommerce预订-如何更改购物车中已存在的项目元?,php,wordpress,woocommerce,woocommerce-bookings,Php,Wordpress,Woocommerce,Woocommerce Bookings,我正在尝试更改所有可预订产品的meta,这些产品已经在购物车中,更具体地说是-结束日期和持续时间,但在几天内都没有结果。。。我需要在计算总数之前在woocommerce_中完成。我尝试过以下代码: if ( $my_condition ) { foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { if ( isset( $cart_item['booking'] ) )
if ( $my_condition ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( isset( $cart_item['booking'] ) ) {
$cart_item['booking']->set_duration( $my_new_duration );
}
}
}
还有成千上万的其他方法,但都没有成功
类似的方法也适用于set_price(),它可以更改价格,但对于购物车中已经存在的产品,无法更改可预订的产品元(如持续时间和结束日期)!谷歌似乎对此束手无策,因为在搜索数天的时间里,我只能找到如何改变价格本身,而不是持续时间或其他元数据
有人能帮我吗?谢谢
编辑:
简单地说,以下是我试图实现的目标——假设我的购物车中有以下物品:
- 产品1预订时间为2018-05-10 10:00至2018-05-10 16:00
- 产品2预订时间为2018-05-10 10:00至2018-05-10 17:00
13:30
),那么请尝试以下函数,我已经尝试并测试了该函数在WooCommerce 3.3.5和1.11.1的WordPress 4.9.5上正常工作
但是,产品的预订持续时间(在“管理”中“编辑产品”页面的“常规”选项卡中)预计为客户定义的{n}
小时数;i、 e.类型为客户
,单位为小时
// 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;
}
}
当my函数触发时,这两种产品在my
购物车应改为2018-05-10 10:00至2018-05-10 13:30
如果您只想设置一个固定的结束时间(例如,在您的示例中,13:30
),那么请尝试以下函数,我已经尝试并测试了该函数在WooCommerce 3.3.5和1.11.1的WordPress 4.9.5上正常工作
但是,产品的预订持续时间(在“管理”中“编辑产品”页面的“常规”选项卡中)预计为客户定义的{n}
小时数;i、 e.类型为客户
,单位为小时
// 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;
}
}
先生,你应该得到一枚奖章。多好的回答。。有这么多额外的代码,我甚至没有问,但它非常有用。非常感谢。虽然我不得不使用WC()->cart->get_cart(),但它工作得非常好代码>而不是$cart->get_cart_contents()代码>,和$cart->设置购物车内容($cart\u项目)
由于某种原因导致我的购物车崩溃,所以我只是将其删除,但除此之外,它的工作效果比预期的要好。非常感谢。先生,你应该得到一枚奖章。多好的回答。。有这么多额外的代码,我甚至没有问,但它非常有用。非常感谢。虽然我不得不使用WC()->cart->get_cart(),但它工作得非常好代码>而不是$cart->get_cart_contents()代码>,和$cart->设置购物车内容($cart\u项目)
由于某种原因导致我的购物车崩溃,所以我只是将其删除,但除此之外,它的工作效果比预期的要好。非常感谢。