Skip to content

Instantly share code, notes, and snippets.

@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);
add_action('admin_init', function () {
$role = get_role('administrator');
if (!$role) {
return;
}
// Core Bookings capabilities
$caps = array(
@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
@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 / 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 ) {
<?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)
<?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)