# How to Disable the Buy Button in WooCommerce Using Functions
Are you looking to temporarily remove or completely disable the “Add to Cart” or “Buy Now” button on your WooCommerce product pages? Perhaps you’re preparing for a sale, updating inventory, or need to temporarily restrict purchases for specific products. This guide shows you how to achieve this using custom functions in your WooCommerce theme’s `functions.php` file – a powerful and flexible method offering precise control. Please note: directly editing your `functions.php` file carries risks. Always back up your website before making any changes.
Understanding the Methods: Temporary vs. Permanent Removal
Before diving into the code, let’s clarify the different approaches:
* Temporary Removal: This method hides the button only for a specified period. This is ideal for sales events or brief inventory issues. You’ll typically use conditional logic based on dates or other criteria.
* Permanent Removal (for specific products): This disables the button for certain products, allowing you to manage individual product availability. You’ll often use product IDs or other product attributes for targeting.
* Permanent Removal (site-wide): This completely removes the “Add to Cart” button from all products across your entire site. This is generally not recommended unless your site serves a different purpose entirely.
Choosing the right approach depends on your specific needs. We’ll cover methods for temporary and permanent removal for specific products.
Disabling the Buy Button with Custom Functions
Here are some examples of how to disable the “Add to Cart” button using custom functions within your `functions.php` file. Remember to replace placeholders like `[product_id]` with the actual ID of your product.
Method 1: Temporarily Disable the Buy Button for All Products
This method disables the button for a specific date range. Replace the dates with your desired start and end dates.
add_action( 'woocommerce_single_product_summary', 'disable_add_to_cart_button', 10, 0 );
function disable_add_to_cart_button() {
$start_date = strtotime( ‘2024-03-01’ ); // Start date
$end_date = strtotime( ‘2024-03-15’ ); // End date
$current_date = current_time( ‘timestamp’ );
if ( $current_date >= $start_date && $current_date <= $end_date ) {
remove_action( ‘woocommerce_single_product_summary’, ‘woocommerce_template_single_add_to_cart’, 30 );
}
}
Method 2: Permanently Disable the Buy Button for Specific Products
This method disables the button for products with specific IDs. Replace `[product_id_1]`, `[product_id_2]` etc. with your actual product IDs.
add_action( 'woocommerce_single_product_summary', 'disable_add_to_cart_button_specific_products', 10, 0 );
function disable_add_to_cart_button_specific_products() {
global $product;
$product_ids_to_disable = array( [product_id_1], [product_id_2] ); // Array of product IDs
if ( in_array( $product->get_id(), $product_ids_to_disable ) ) {
remove_action( ‘woocommerce_single_product_summary’, ‘woocommerce_template_single_add_to_cart’, 30 );
}
}
Method 3: Using a Custom Field for Targeted Control
For greater flexibility, create a custom product field (e.g., “disable_add_to_cart”) and use it to control button visibility. This allows you to easily enable/disable the button for each product individually via the product edit screen. This requires creating the custom field first through your WooCommerce settings or a plugin.
add_action( 'woocommerce_single_product_summary', 'disable_add_to_cart_by_custom_field', 10, 0 );
function disable_add_to_cart_by_custom_field() {
global $product;
if ( $product->get_meta( ‘disable_add_to_cart’ ) == ‘yes’ ) {
remove_action( ‘woocommerce_single_product_summary’, ‘woocommerce_template_single_add_to_cart’, 30 );
}
}
Conclusion
Disabling the “Add to Cart” button in WooCommerce offers significant control over your product availability and sales processes. By using custom functions within your `functions.php` file, you can implement temporary or permanent solutions tailored to your specific needs. Remember to always back up your website before making any code changes and test thoroughly after implementing these functions. Choosing the method that best suits your requirements will ensure a seamless and controlled shopping experience for your customers. If you’re unsure about editing your theme’s files, consider consulting with a WordPress developer.