Changing is_purchasable
in WooCommerce 3: A Comprehensive Guide
As an ecommerce store owner, one of the essential features you need to manage is the ability to control which products are available for purchase on your website. In WooCommerce 3, this functionality is handled by the is_purchasable
function, which determines whether a product can be added to the cart and purchased.
In this comprehensive guide, we'll explore how you can modify the is_purchasable
function to suit your specific business needs, ensuring that your customers have a seamless shopping experience.
Understanding the Default is_purchasable
Function
The default is_purchasable
function in WooCommerce 3 checks several conditions to determine whether a product can be purchased. These conditions include:
- Product Existence: The product must exist and have a valid ID.
- Product Status: The product must have a "publish" status, or the current user must have the ability to edit the product.
- Product Price: The product must have a non-zero price.
The default implementation of the is_purchasable
function looks like this:
add_filter( 'woocommerce_is_purchasable', 'woocommerce_is_purchasable', 10, 2 );
function woocommerce_is_purchasable( $purchasable, $product ) {
if ( $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) ) && $product->get_price() !== '' && $product->get_price() !== '0' && $product->get_price() !== 0 ) {
$purchasable = true;
} else {
$purchasable = false;
}
return $purchasable;
}
While this default behavior may work well for many WooCommerce stores, there may be situations where you need to customize the is_purchasable
function to fit your specific requirements.
Customizing the is_purchasable
Function
To customize the is_purchasable
function, you can use the woocommerce_is_purchasable
filter and create your own custom function. Here's an example of how you can modify the default behavior:
add_filter( 'woocommerce_is_purchasable', 'customizing_is_purchasable', 20, 2 );
function customizing_is_purchasable( $purchasable, $product ) {
if ( $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) ) ) {
$purchasable = true;
} else {
$purchasable = false;
}
return $purchasable;
}
In this example, we've removed the condition that checks if the product has a non-zero price. This means that even if a product has a price of 0, it will still be considered purchasable. This can be useful in scenarios where you want to offer free products or services on your WooCommerce store.
Customizing is_purchasable
for Product Variations
If you're working with product variations in WooCommerce, you may also want to customize the is_purchasable
function for those as well. Here's an example of how you can do that:
add_filter( 'woocommerce_variation_is_visible', 'customizing_variation_is_visible', 20, 4 );
function customizing_variation_is_visible( $visible, $product_id, $parent_id, $product ) {
if ( 'publish' === get_post_status( $product->get_id() ) ) {
$visible = true;
} else {
$visible = false;
}
return $visible;
}
In this example, we're using the woocommerce_variation_is_visible
filter to control the visibility of product variations. If the variation's post status is "publish", it will be considered visible and, therefore, purchasable. If the variation's post status is anything else, it will be considered not visible and not purchasable.
Real-World Examples and Use Cases
Let's explore a few real-world examples of how you can use the customized is_purchasable
function to meet your specific business needs.
Example 1: Offering Free Products
Imagine you have a WooCommerce store that offers both paid and free products. You want to ensure that all free products are always considered purchasable, regardless of their price. Here's how you can modify the is_purchasable
function to achieve this:
add_filter( 'woocommerce_is_purchasable', 'customizing_is_purchasable', 20, 2 );
function customizing_is_purchasable( $purchasable, $product ) {
if ( $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) ) ) {
if ( $product->get_price() === '0' || $product->get_price() === 0 ) {
$purchasable = true;
} else {
$purchasable = true;
}
} else {
$purchasable = false;
}
return $purchasable;
}
In this example, we're checking if the product's price is 0 or '0' (a string representation of 0). If the product has a 0 price, we set the $purchasable
flag to true
, regardless of the other conditions. This ensures that all free products are always considered purchasable in 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.
Example 2: Restricting Purchases Based on User Roles
Suppose you want to restrict certain products in your WooCommerce store to specific user roles. For example, you might want to make certain products available only to registered customers or to users with a specific role, such as "VIP" customers. Here's how you can modify the is_purchasable
function to achieve this:
add_filter( 'woocommerce_is_purchasable', 'customizing_is_purchasable', 20, 2 );
function customizing_is_purchasable( $purchasable, $product ) {
if ( $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) ) ) {
$user = wp_get_current_user();
if ( in_array( 'customer', (array) $user->roles ) || in_array( 'vip_customer', (array) $user->roles ) ) {
$purchasable = true;
} else {
$purchasable = false;
}
} else {
$purchasable = false;
}
return $purchasable;
}
In this example, we're checking the current user's roles. If the user has the "customer" or "vip_customer" role, we set the $purchasable
flag to true
. Otherwise, we set it to false
, effectively restricting the product from being purchased by users without the specified roles.
By customizing the is_purchasable
function, you can create a more tailored shopping experience for your WooCommerce customers, addressing specific business requirements and enabling you to better control the availability of your products.
Remember, the code snippets provided in this guide are just examples, and you may need to adjust them to fit your specific use case and WordPress/WooCommerce setup. Always test your changes thoroughly before implementing them on a live site.
If you're looking for a comprehensive solution to optimize your WooCommerce store's conversion rates, consider using Flowpoint.ai. Flowpoint's advanced analytics and AI-powered recommendations can help you identify and fix technical issues, improve user experience, and drive more sales for your online business