Last active
February 22, 2023 05:49
-
-
Save JonS7/ddadace1ae670af0245a to your computer and use it in GitHub Desktop.
Couldn't figure out how to use Woocommerce Bookings functions to show if a product was fully booked or not, so came up with this.
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
function get_product_availability() { | |
global $product, $post; | |
// Get dates from custom field | |
$start_date = get_field('start_date'); | |
$end_date = get_field('end_date'); | |
// Get into class | |
$WC_Product_Booking = new WC_Product_Booking($product); | |
// Get resources for this product | |
$resources = $WC_Product_Booking->get_resources($product->ID); | |
// Add all resource quantities of this product together | |
$qtyResources = 0; | |
foreach ( $resources as $resource ) { | |
$qtyResources += get_post_meta($resource->ID, 'qty', true); | |
} | |
// get the number of bookings for this product on the start date | |
$bookedPosts = get_posts(array( | |
'post_type' => 'wc_booking', | |
'post_status' => array('unpaid', 'pending-confirmation', 'confirmed', 'paid'), | |
'posts_per_page' => -1, | |
'fields' => 'ids', | |
'meta_query' => array( | |
array( | |
'key' => '_booking_product_id', | |
'value' => $post->ID | |
), | |
array( | |
'key' => '_booking_start', | |
'value' => $start_date."000000" | |
), | |
) | |
) | |
); | |
$productBookings = count($bookedPosts); | |
// If total resource quantity is equal to confirmed bookings then it's fully booked | |
if ($qtyResources <= $productBookings) { | |
$fullyBooked = true; | |
} else { | |
$fullyBooked = false; | |
} | |
// Do something with it | |
if ($fullyBooked == true) { | |
echo "fully-booked"; | |
} | |
if ($fullyBooked == false) { | |
echo "available"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for sharing this.... OMG you saved my world there - I have been pulling my hair out wondering why a UNIX timestamp was converting to a very strange date - when all along the _booking_start like this "20230224000000" is actually the date like this 2023-02-24-000000