首页  >  问答  >  正文

Woocommerce 通过 luginhive.com 的“Woocommerce 预订和约会”插件通过挂钩(或其他方式)更改价格

我的网站的总体背景

网站按小时出租房间。

我在我的网站上使用“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 Bookings 持续时间设置价格并对我的问题进行调整,但没有成功。

P粉409742142P粉409742142187 天前266

全部回复(1)我来回复

  • P粉237029457

    P粉2370294572024-03-20 22:05:37

    我创建了代码片段,但没有看到任何变化。 检查此 链接到实时预览

    <?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;
    }

    回复
    0
  • 取消回复