Created
March 2, 2026 14:51
-
-
Save plugin-republic/10b84de83e81d1135754a5c1bbefbc7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Flat rate with daily overage for Bookings for WooCommerce | |
| * | |
| * When a product has the "Flat rate" option enabled, charges the flat rate | |
| * for bookings up to $min_days, then adds $daily_rate | |
| * for each additional day beyond that. | |
| * | |
| * Add to your theme's functions.php or a site-specific plugin. | |
| * | |
| * Note: designed for products set to "day" or "night" booking unit. | |
| * For weekly products, $$min_days would need to be expressed in weeks. | |
| */ | |
| /** | |
| * Append a per-day overage charge to flat rate bookings longer than the minimum. | |
| * | |
| * Hooks into the AJAX cost response so the correct price is shown on the product | |
| * page and submitted to the cart via the hidden calculated_booking_cost field. | |
| * | |
| * @param array $return_json JSON sent back to the browser. | |
| * @param array $return_values Raw values from bfwc_calculate_booking_cost(). | |
| * @param array $posted Form POST data. | |
| * @return array | |
| */ | |
| function my_bfwc_flat_rate_with_overage( $return_json, $return_values, $posted ) { | |
| $daily_rate = 8; | |
| $min_days = 14; | |
| $product_id = absint( $return_json['post_id'] ); | |
| // Only applies to products with "Flat rate" enabled | |
| if ( ! function_exists( 'bfwc_is_flat_rate' ) || ! bfwc_is_flat_rate( $product_id ) ) { | |
| return $return_json; | |
| } | |
| // $return_values['num_units'] is the booking length in days (day unit) or nights (night unit) | |
| $num_units = (int) $return_values['num_units']; | |
| if ( $num_units <= $min_days ) { | |
| return $return_json; | |
| } | |
| $overage_days = $num_units - $min_days; | |
| $overage_cost = $overage_days * $daily_rate; | |
| // Apply the same tax treatment that bfwc_calculate_booking_cost() used for the base cost | |
| $product = wc_get_product( $product_id ); | |
| if ( is_object( $product ) ) { | |
| if ( 'incl' === get_option( 'woocommerce_tax_display_shop' ) ) { | |
| $overage_cost = wc_get_price_including_tax( $product, array( 'price' => $overage_cost ) ); | |
| } else { | |
| $overage_cost = wc_get_price_excluding_tax( $product, array( 'price' => $overage_cost ) ); | |
| } | |
| } | |
| $return_json['cost'] = (float) $return_json['cost'] + (float) $overage_cost; | |
| return $return_json; | |
| } | |
| add_filter( 'bfwc_booking_cost_return_json', 'my_bfwc_flat_rate_with_overage', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment