Woocommerce Change prices via hooks (or other means) with the "Woocommerce Bookings and Appointments" plugin from luginhive.com
P粉409742142
P粉409742142 2024-03-19 23:51:59
0
1
332

General background of my website

The website rents out rooms by the hour.

I use the "WooCommerce Bookings And Appointments" plugin on my website. The plugin URL is https://www.pluginhive.com/product/woocommerce-booking-and-appointments/

The plugin supports cost rules and I can control the price at all times. But I can't find a solution for my specific scenario.

My goal is to add the following rules:

If the customer selects a time between 10pm and 10am.

And minimumorder timeis greater than 6 hours.

Then the price will be 6 hours.

Model house price

Booking period is set in the general settings, 30 minutes per period.

Booking fee settings: 3 hours Minimum price is $300 (we use rules from blocks 1 to 6).

Base room rate: $300 (customers can order less than 3 hours, but the price is a minimum of 3 hours).

Block Cost: $50 (starting with block number 7).

Scenario example:

If a customer orders 4 hours from 11pm to 3am (total blocks: 8), the price will be regular price: $400 (base cost: 300$100 [2 blocks, $50 each])

If a customer orders 5 hours from 11pm to 4am (total blocks: 10), the price will be the regular price: $500

If a customer orders 6 hours from 11pm to 5am (total blocks: 12), the price will be the regular price: $600

If a customer orders 7 hours from 11pm to 6am (total blocks: 14), the price will be $600 instead of $700

If a customer orders 8 hours from 11pm to 7am (total blocks: 16), the price will be $600 instead of $800

If the customer orders 6 hours from 9pm to 3am (total blocks: 12), the price will be $600

If a customer orders 9 PM to 4 AM 7 hours (total blocks: 14), the price will be $600 instead of $700

  • 9pm-10pm = $100
  • 10pm-4am = $600

If a customer orders 9 PM to 5 AM 8 hours (total blocks: 16), the price will be $600 instead of $800

  • 9pm-10pm = $100
  • 10pm to 5am = $600 (Rule triggers because order is older than 6 hours)

If a customer orders within the 14 hours 9pm to 11am (Total blocks: 28), the price will be $800 instead of $1400

  • 9pm-10pm = $100
  • 10pm to 10am = $600 (Rule triggers because order is older than 6 hours)
  • 10am-11am = $100

I tried following this article to set prices based on WooCommerce Bookings duration and making adjustments to my question, but with no success.

P粉409742142
P粉409742142

reply all(1)
P粉237029457

I created the code snippet but don't see any changes. Check this Link to the live preview

<?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;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template