Get is_purchasable Hook Working for WooCommerce Product Variations Too
As an ecommerce store owner, it's essential to have full control over when your products can be purchased by customers. The is_purchasable
hook in WooCommerce provides a great way to achieve this, allowing you to customize the purchasing behavior of your products based on specific criteria.
However, one common issue faced by WooCommerce store owners is making the is_purchasable
hook work not just for simple products, but also for product variations. In this article, we'll dive into the code provided and explain how you can successfully implement it to manage the purchasability of both simple products and product variations on your WooCommerce store.
Understanding the is_purchasable
Hook
The is_purchasable
hook in WooCommerce is a powerful tool that allows you to control whether a product can be purchased or not. By default, WooCommerce considers a product as purchasable if it has a positive stock quantity, is visible, and is not on backorder.
However, you may have additional criteria that you want to consider when determining the purchasability of a product, such as a specific date range during which the product can be purchased. This is where the is_purchasable
hook comes in handy.
Implementing the is_purchasable
Hook for Simple Products
Let's start by looking at the code provided for simple products:
add_filter( 'woocommerce_is_purchasable', 'purchasable_product_date_range', 20, 2 );
function purchasable_product_date_range( $purchasable, $product ) {
$date_from = (int) get_post_meta( $product->get_id(), '_availability_schedule_dates_from', true );
$date_to = (int) get_post_meta( $product->get_id(), '_availability_schedule_dates_to', true );
if( empty($date_from) || empty($date_to) )
return $purchasable; // Exit (fields are not set)
$current_date = (int) current_time('timestamp');
if( ! ( $current_date >= $date_from && $current_date <= $date_to ) )
$purchasable = false;
return $purchasable;
}
This code adds a new filter to the woocommerce_is_purchasable
hook, which checks if the product is within a specific date range. Here's how it works:
- The code retrieves the
_availability_schedule_dates_from
and _availability_schedule_dates_to
custom fields from the product's post meta data. These fields store the start and end dates of the product's availability.
- If either of these fields is empty, the function returns the original
$purchasable
value, effectively skipping the date range check.
- If the custom fields are set, the code checks if the current date (in timestamp format) is within the specified date range. If the current date is outside the range, the function sets
$purchasable
to false
.
- Finally, the function returns the modified
$purchasable
value.
Implementing the is_purchasable
Hook for Product Variations
Now, let's address the issue of making the is_purchasable
hook work for product variations as well. The provided code includes an additional function for this purpose:
add_filter( 'woocommerce_variation_is_purchasable', 'purchasable_variation_date_range', 20, 2 );
function purchasable_variation_date_range( $purchasable, $product ) {
$date_from = (int) get_post_meta( $product->get_parent_id(), '_availability_schedule_dates_from', true );
$date_to = (int) get_post_meta( $product->get_parent_id(), '_availability_schedule_dates_to', true );
if( empty($date_from) || empty($date_to) )
return $purchasable; // Exit (fields are not set)
$current_date = (int) current_time('timestamp');
if( ! ( $current_date >= $date_from && $current_date <= $date_to ) )
$purchasable = false;
return $purchasable;
}
This function is very similar to the one for simple products, but with one key difference: it retrieves the custom fields from the parent product, not the variation itself. This is because product variations don't have their own custom fields, and they inherit the settings from their parent product.
The code works as follows:
- The function retrieves the
_availability_schedule_dates_from
and _availability_schedule_dates_to
custom fields from the parent product's post meta data, using the get_parent_id()
method.
- If either of these fields is empty, the function returns the original
$purchasable
value, effectively skipping the date range check.
- If the custom fields are set, the code checks if the current date (in timestamp format) is within the specified date range. If the current date is outside the range, the function sets
$purchasable
to false
.
- Finally, the function returns the modified
$purchasable
value.
By combining these two functions, you can ensure that the is_purchasable
hook works correctly for both simple products and product variations in your WooCommerce store.
Testing and Implementation
To implement this functionality, you'll need to add the provided code to your active child theme's functions.php
file (or the active theme's functions.php
file if you don't have a child theme). Once the code is in place, you can start setting the _availability_schedule_dates_from
and _availability_schedule_dates_to
custom fields for your products and product variations.
You can test the functionality by creating a simple product and a product variation, setting the custom fields, and then checking the product's purchasability on the frontend. Ensure that the products can only be purchased within the specified date range, and that the behavior is consistent across both simple products and product variations.
Flowpoint.ai can help you identify any technical issues that may be impacting the purchasability of your products and provide recommendations to fix them, ensuring a seamless shopping experience for your customers.
Conclusion
In this article, we've explored how to make the is_purchasable
hook in WooCommerce work for both simple products and product variations. By implementing the provided code, you can now control the availability of your products based on a specific date range, ensuring that customers can only purchase your products during the desired time frame.
Remember to thoroughly test the functionality and make any necessary adjustments to ensure a smooth and consistent user experience across your WooCommerce store.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.