网站按小时出租房间。
我在我的网站上使用“WooCommerce Bookings And Appointments”插件。 插件 URL 为 https://www.pluginhive.com/product/woocommerce-booking-and-appointments/
该插件支持成本规则,我可以控制所有时间的价格。 但我找不到特定场景的解决方案。
如果客户选择晚上10点到上午10点之间的时间。
并且最小订单时间大于6小时。
那么价格将是6小时。
预订时间一般设置,30分钟为一组。
预订费用设置: 3 小时的最低价格 为 300 美元(我们使用第 1 至 6 块的规则)。
房间基本费用: 300$(客户可以订购少于3小时,但价格至少为3小时)。
区块成本: 50$(从区块编号 7 开始)。
场景举例:
如果客户从晚上 11 点到凌晨 3 点订购 4 小时(总块数:8),价格将是常规价格:400 美元(基本成本:300 美元 + 100 美元 [2 块,每块 50 美元])
如果客户从晚上 11 点到凌晨 4 点订购 5 小时(总块数:10),价格将是常规价格:500 美元
如果客户从晚上 11 点到凌晨 5 点订购 6 小时(总块数:12),价格将是常规价格:600 美元
如果客户从晚上 11 点到早上 6 点订购 7 小时(总块数:14),价格将为 600 美元,而不是 700 美元
如果客户订购从晚上 11 点到早上 7 点的 8 小时(总区块数:16),价格将为 600 美元,而不是 800 美元
如果客户从晚上9点到凌晨3点订购6小时(总区块:12),价格将为600美元
如果客户从晚上9点到凌晨4点订购7小时(总区块:14),价格将为600美元,而不是700美元
如果客户从晚上9点到凌晨5点订购8小时(总区块:16),价格将为600美元,而不是800美元
如果客户在晚上9点到上午11点这14小时内下单(总块数:28),价格将为800$而不是1400$
我尝试按照这篇文章根据 WooCommerce 预订持续时间设置价格并对我的问题进行调整,但没有成功。
我创建了代码片段,但没有看到任何变化。 检查此链接进行实时预览
<?php
// Calculates price based on selected booking start time and minimum order hours
add_action( 'woocommerce_before_calculate_totals', 'cwpai_booking_price_calculation', 10, 1 );
function cwpai_booking_price_calculation( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
foreach ( $cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
// Get start time and minimum order hours
$start_time = strtotime($_POST['wc_bookings_field_start_date'][0] . ' ' . $_POST['wc_bookings_field_start_time'][0]);
$minimum_hours = get_post_meta( $product_id, '_minimum_duration', true );
// Check if start time and minimum order hours meet condition
if ( date('H:i', $start_time) >= '22:00' && date('H:i', $start_time) <= '10:00' && $minimum_hours >= 6 ) {
// Calculate maximum cost for 6 hours
$max_cost = $cart_item['data']->get_price() * 6;
// Get current cost based on duration
$duration = WC_Bookings_Cart::calculate_booking_duration( $cart_item['booking'], true, true );
$current_cost = $duration['cost'];
// Update cost to maximum cost for 6 hours
if ( $current_cost > $max_cost ) {
$cart_item['data']->set_price( $max_cost );
}
}
}
}
// Update post meta when product is saved
add_action( 'woocommerce_process_product_meta', 'cwpai_update_booking_meta' );
function cwpai_update_booking_meta( $post_id ) {
// Only run for bookable products
if ( get_post_meta( $post_id, '_wc_booking_type', true ) !== 'booking' ) {
return;
}
// Get minimum order hours
$minimum_hours = isset( $_POST['_minimum_duration'] ) ? absint( $_POST['_minimum_duration'] ) : 0;
// Update post meta with new booking cost
if ( $minimum_hours >= 6 ) {
$max_cost = get_post_meta( $post_id, '_price', true ) * 6;
update_post_meta( $post_id, '_new_booking_cost', $max_cost );
} else {
delete_post_meta( $post_id, '_new_booking_cost' );
}
}
// Modify product costs to new booking cost
add_filter( 'woocommerce_product_get_price', 'cwpai_modify_product_costs', 10, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'cwpai_modify_product_costs', 10, 2 );
function cwpai_modify_product_costs( $price, $product ) {
$new_booking_cost = get_post_meta( $product->get_id(), '_new_booking_cost', true );
if ( $new_booking_cost ) {
$price = $new_booking_cost;
}
return $price;
}
我错过了一些东西:)检查这个链接实时预览
/**
* Custom price calculation for WooCommerce Bookings and Appointments plugin.
*/
// Hook into the 'woocommerce_before_calculate_totals' action
add_action('woocommerce_before_calculate_totals', 'custom_booking_price_calculation', 10, 1);
function custom_booking_price_calculation($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
// Get current time
$current_time = current_time('timestamp');
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
if ($product->is_type('booking')) {
$booking_data = $product->get_data();
$start_time = strtotime($booking_data['date_from']);
$end_time = strtotime($booking_data['date_to']);
$selected_blocks = ceil(($end_time - $start_time) / 1800); // 1800 seconds = 30 minutes
// Condition #1: Start time is between 10:00 PM and 10:00 AM
$start_hour = date('H', $start_time);
if ($start_hour >= 22 || $start_hour <= 10) {
// Condition #2: Minimum order blocks greater than or equal to 12
if ($selected_blocks >= 12) {
// Condition #3: Calculate price for maximum 12 blocks based on rules
$base_cost = 300; // Base cost from product settings
$cost_per_block = 50; // Cost per block from product settings
$max_blocks = min($selected_blocks, 12);
$price = $base_cost + ($cost_per_block * $max_blocks);
// Set new price for the booking
$booking_data['cost'] = $price;
$booking = new WC_Booking($booking_data['booking_id']);
$booking->set_cost($price);
// Update booking data
$product->set_props($booking_data);
$product->save();
// Update cart item data
$cart_item['data'] = $product;
$cart->cart_contents[$cart_item_key] = $cart_item;
// Update cart totals
$cart->set_cart_contents_total();
$cart->set_cart_contents_tax();
$cart->set_total($cart->subtotal + $cart->tax_total, $cart->shipping_total);
}
}
}
}
}
// Hook into the 'update_post_meta' action
add_action('update_post_meta', 'custom_booking_price_update_post_meta', 10, 3);
function custom_booking_price_update_post_meta($meta_id, $object_id, $meta_key) {
if ($meta_key === '_cost') {
$booking = new WC_Booking($object_id);
$booking_data = $booking->get_data();
$booking_cost = $booking_data['cost'];
// Update booking cost if necessary
if ($booking_cost) {
update_post_meta($object_id, '_cost', $booking_cost);
}
}
}
// Hook into the 'new_booking_cost' filter
add_filter('new_booking_cost', 'custom_booking_price_new_booking_cost', 10, 4);
function custom_booking_price_new_booking_cost($cost, $resource_id, $start, $end) {
// Check if the calculation has already been performed
if (did_action('woocommerce_before_calculate_totals') >= 2) {
return $cost;
}
// Calculate the number of blocks
$selected_blocks = ceil(($end - $start) / 1800);
// Condition #1: Start time is between 10:00 PM and 10:00 AM
$start_hour = date('H', $start);
$start_minute = date('i', $start);
if (($start_hour == 22 && $start_minute >= 0) || ($start_hour >= 23 || $start_hour <= 9)) {
// Condition #2: Minimum order blocks greater than or equal to 12
if ($selected_blocks >= 12) {
// Condition #3: Calculate price for maximum 12 blocks based on rules
$base_cost = 300; // Base cost from product settings
$cost_per_block = 50; // Cost per block from product settings
$max_blocks = min($selected_blocks, 12);
$price = $base_cost + ($cost_per_block * $max_blocks);
return $price;
}
}
return $cost;
}
// Function to modify the base cost of a bookable product
function modify_baseprice($base_cost, $product_id, $booking_id) {
$product = wc_get_product($product_id);
if ($product->is_type('booking')) {
$booking_base_cost = $product->get_meta('_cost');
if ($booking_base_cost) {
$booking_base_cost = max(0, (float)$booking_base_cost);
$product->set_base_cost($booking_base_cost);
$product->save();
return $booking_base_cost;
}
}
return $base_cost;
}
// Hook into the 'woocommerce_bookings_after_booking_base_cost' action
add_action('woocommerce_bookings_after_booking_base_cost', 'modify_baseprice', 10, 3);
// Hook into the 'woocommerce_process_product_meta_booking' action
add_action('woocommerce_process_product_meta_booking', 'modify_bookable_product_base_cost', 100, 1);
function modify_bookable_product_base_cost($product_id) {
if (isset($_POST['_cost'])) {
$booking_base_cost = max(0, (float)$_POST['_cost']);
update_post_meta($product_id, '_base_cost', $booking_base_cost);
}
}