Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Cross-Resource Booking Blocker
*
* When a booking is paid using one resource, block the related resources
* to prevent double-booking of shared spaces.
*/
// Define the resource relationships (update these IDs after creating resources)
define('PRIMARY_RESOURCE_ID', 123); // Replace with actual resource ID (e.g., Entire Space)
<?php
/**
* Replace existing booking in cart with new one
*
* When a customer adds a new booking for a specific bookable product,
* any existing booking for the same product is automatically removed.
* This ensures cross-resource blocking works correctly since it triggers on payment.
*/
// Define the bookable product ID (update after creating the product)
@thisissandip
thisissandip / booking_date_coupon.php
Created February 10, 2026 15:56
WooCommerce Bookings: Bookings date based coupon
<?php
/**
* Booking Date-Based Coupon Rules
*
* Define which coupons are valid for which booking date ranges.
* Just edit the $coupon_rules array below.
*/
add_filter( 'woocommerce_coupon_is_valid', function( $valid, $coupon, $discount ) {
if ( ! $valid || ! WC()->cart ) {
@thisissandip
thisissandip / woocommerce_custom_roles.php
Last active February 3, 2026 09:04
Custom Roles for WooCommerce: Fulfilment Operator & Store Operator
function wccr_add_roles_snippet() {
// Uncomment these "Remove" lines ONCE if you change capabilities below and need to refresh the roles.
// remove_role( 'fulfilment_operator' );
// remove_role( 'store_operator' );
// 1. Fulfilment Operator
add_role(
'fulfilment_operator',
__( 'Fulfilment Operator', 'woocommerce' ),
@thisissandip
thisissandip / cancel_bookings_if_payment_fails.php
Created January 2, 2026 13:06
Cancel WooCommerce Bookings when an order status changes from Pending to Failed.
/**
* Cancel WooCommerce Bookings when an order status changes from Pending to Failed.
*/
add_action('woocommerce_order_status_changed', 'wc_cancel_bookings_on_failed_order', 10, 4);
function wc_cancel_bookings_on_failed_order($order_id, $from_status, $to_status, $order) {
// Check if the transition is from 'pending' to 'failed'
if ($from_status === 'pending' && $to_status === 'failed') {
// Ensure the Bookings classes exist
add_action('admin_init', function () {
$role = get_role('administrator');
if (!$role) {
return;
}
// Core Bookings capabilities
$caps = array(
@thisissandip
thisissandip / snippet.php
Last active July 19, 2025 06:02
WooCommerce Bookings: Auto Add 1-Hour Buffer After Booking Ends for Resource Availability
<?php
/*
Add a 1-hour buffer period to resource availability AFTER a paid booking ends by blocking availability
from the booking's end time to 1 hour later. This prevents back-to-back bookings.
*/
function auto_create_one_hour_buffer_availability_resource($booking_id) {
$booking = new WC_Booking($booking_id);
@thisissandip
thisissandip / booking-creation-logger.php
Last active January 2, 2026 13:08
booking-creation-logger: Logs details of the new booking in WooCommerce > status > logs
/**
* Monitor new booking creation using wc_get_logger
*/
function log_all_new_bookings( $booking_id ) {
if ( ! function_exists( 'get_wc_booking' ) ) {
return;
}
$booking = get_wc_booking( $booking_id );
@thisissandip
thisissandip / auto_select_booking_first_available_date
Last active June 10, 2024 07:31
Auto Select the first available booking date in WooCommerce Booking Calendar
add_action( 'wp_footer', 'bookable_product_script_js');
function bookable_product_script_js() {
global $product;
// Only on single bookable products
if( is_product() && $product->is_type('booking')) :
?>
<script type='text/javascript'>
var interval = setInterval(checkifDateisLoaded, 500); // set booking calendar load time
function checkifDateisLoaded() {
const datetobeselected = document.querySelector("[title='This date is available']");
@thisissandip
thisissandip / share_woocommerce_cart.php
Last active February 4, 2024 07:28
The cart page features a "Share Cart" button, allowing admins to generate a unique cart URL for easy cart sharing with customers. Additionally, admins can manage generated cart URLs by clearing them from the "WC > Status > Tools" section.
<?php
/**
* Function to get cart data from session
*
*/
function get_cart_data_from_session(){
$cart_session = array(
'cart' => '',
'cart_totals' => '',