Skip to content

Instantly share code, notes, and snippets.

@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 / block_resources.php
Created March 18, 2025 11:03
WooCommerce Bookings: Automatically Block Other Resources When One is Booked
/*
This snippet ensures that when a booking is paid for a booking product with multiple resources,
all other linked resources are automatically blocked for the same time slot. It dynamically adds
an availability rule within the booking product blocking the slot, preventing other resources bookings for that slot.
*/
function block_booking_slot_to_prevent_other_resource_bookings_on_payment($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' => '',
@thisissandip
thisissandip / Remove WooCommerce Bookings "Templates" tab
Created November 3, 2023 14:11
Remove WooCommerce Bookings "Templates" tab
function remove_tab_7252021 ($tabs){
unset($tabs['bookings_templates']);
return($tabs);
}
add_filter('woocommerce_product_data_tabs', 'remove_tab_7252021', 10, 1);