Skip to content

Instantly share code, notes, and snippets.

@JonS7
Last active February 22, 2023 05:49
Show Gist options
  • Save JonS7/ddadace1ae670af0245a to your computer and use it in GitHub Desktop.
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.
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";
}
}
@Progressed36
Copy link

`function fireRestAPI($method, $url, $data = false)
{
$curl = curl_init();

switch ($method)
{
    case "POST":
        curl_setopt($curl, CURLOPT_POST, 1);

        if ($data)
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        break;
    case "PUT":
        curl_setopt($curl, CURLOPT_PUT, 1);
        break;
    default:
        if ($data)
            $url = sprintf("%s?%s", $url, http_build_query($data));
}

// Optional Authentication:
//curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($curl, CURLOPT_USERPWD, "username:password");

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);

curl_close($curl);

return $result;

}

function getResourceSlots() {

}

function get_product_availability($checkdate, $booking_id) {

$url = get_site_url() . '/wp-json/wc-bookings/v1/products/slots?min_date=' . $checkdate . '&max_date=' . $checkdate . '&product_ids=' . $booking_id;

$resources = json_decode(fireRestAPI('GET', $url), true);


// Add all resource quantities of this product together
$qtyAvailable = 0;
$qtyBooked = 0;

$totals = array();
foreach ( $resources['records'] as $resource ) {
	$qtyBooked += $resource['available'] + $resource['booked'];
	$qtyAvailable += $resource['available'];
}
	$totals['qtyavailable'] = $qtyAvailable;
	$totals['qtybooked'] = $qtyBooked;

return $totals;

}`

use the new RestAPI. dates are still as pedroserpa said Y-m-d.

@solaceten
Copy link

I found out that woocommerce bookings plugin was storing dates without '-', so 2016-02-01 was going in as 20160201

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment