Display a custom message when a product is not purchassable in Woocommerce single pages

woocommerce single product page
woocommerce product gallery hook
woocommerce product page template hooks
woocommerce single product description
woocommerce hooks
woocommerce notices wrapper
woocommerce single product title
woocommerce after short description

I am creating a website which is flower shop. Some flowers are seasonally available. Using Advanced Custom Fields plugin, I have added a custom field in Woocommerce product post type (check box) list of months to chose from in which product will be available.

I have been able to disable the add to cart button for the months in which product will not be available using code below:

add_filter('woocommerce_is_purchasable', 'is_available', 10, 2);
function is_available() {
    // this is a field added using 'Advance Custom Fields' plugin 
    $months = get_field('availability');
    $currentMonth = date('F');

    if(in_array($currentMonth, $months))
        return true;
    else
        return false;
}

The code I'm using works, it removes add to cart button from the related single product page. I would like to add some message, so customers will know why it's not available. How can I do that?

I just need to know how I can add message as well, when the product is not available.


There are some errors in your code, like the 2 missing function variables declared for this hook.

The following revisited code includes the displayed custom message, when the product is not available:

add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
    $months      = (array) get_field('availability');
    $purchasable = in_array( date('F'), $months ) ? $purchasable : false;

    return $purchasable;
}

add_action( 'woocommerce_single_product_summary', 'unavailable_product_display_message', 20 );
function unavailable_product_display_message() {
    global $product;

    if(! $product->is_purchasable() ){
        echo '<p style="color:#e00000;">' . __("This product is currently unavailable.") . '</p>';
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


You can also display instead a disabled button with a short text, replacing in my code:

echo '<p style="color:#e00000;">' . __("This product is currently unavailable.") . '</p>';

By this:

echo '<a class="button alt disabled">' . __("Currently unavailable.") . '</a>';

Non purchasable products with Call to Order buttons in , Non purchasable products with Call to Order buttons in WooCommerce custom “Call to Order” text and the single product pages should show custom message along the lines WooCommerce archive pages like Shop: $not_ready_to_sell ) { echo '<p class="content-box-blue">Call to order this product. Custom notice to specific products in WooCommerce. To add a custom notice/message to specific products in WooCommerce you first need to find the id number of the product where you want to add the custom message. Go to edit the product and get the product id from the browser search/address bar as shown in the picture below.


You can use the is_purchasable within your template to display a message

global $product;
if ( ! $product->is_purchasable()): ?>
  <p>Sorry Not Available</p>
<?php endif;

Slight sidenote if you only wanted to return true or false you could have done

return in_array($currentMonth, $months)

as in_array returns a boolean value anyway

WooCommerce: Display "FREE" Instead of $0.00 or Empty Price , display as “FREE!” and products with empty prices were not publishable/​purchasable. woocommerce-customize-single-product-page-PHP  Non purchasable products with Call to Order buttons in WooCommerce August 5, 2017 by Sridhar Katakam 8 Comments Favorited: 0 times In a recent Genesis customization project I completed, the requirement was to be able to mark specific products as “Not (yet) available for sale” when adding/editing the product.


Since you have a method to check for product available month, you can write that code as a seperate function in your functions.php. So that function can be called from hook to remove add to cart button and from the hook that is used to show message.

function get_product_availiblity(){
  $months = get_field('availability');
  $currentMonth = date('F');  
  if(in_array($currentMonth, $months))
    return true;
  else
    return false;
}

So your woocommerce_is_purchasable function can be modified as

function is_available() {
  $availibilty = get_product_availiblity();
  return $availibilty;
}
add_filter('woocommerce_is_purchasable', 'is_available', 10, 2);

And to show the message you can add the following hook

function show_availibilty_message() {
  $availibilty_msg = get_product_availiblity();
  if(!$availibilty_msg){
    echo '<p>This product is not available for this month</p>';
  }
}
add_action( 'woocommerce_single_product_summary', 'show_availibilty_message', 20 );

Non-Purchasable WooCommerce Products – WordPress-plugin , Set certain WooCommerce products to be non-purchasable (display-only), with a Set custom text for the archive page button or single product page message. When you run a WooCommerce online store, sometimes you want to place a custom message or other content strategically on your shop. Of course, there are many reasons you might want to place content specific parts of your store, from limited stock messages to updates on sales— and many other unique uses.


Product Visibility by Country for WooCommerce – WordPress plugin , Display WooCommerce products by customer's country. will hide prices for selected products (i.e. will make products non-purchasable). Outputting customizable “product is not available in your country” message on single product and/or Customize redirect URL for hidden products (i.e. set it different from 404 page). 1/ Go to Appearance > Customize > WooCommerce > Store Notice to view and access the current/published promotion. 2/ Untick box for Enable Store Notice. 3/ Select the gear wheel. A panel opens. 4/ Select Schedule on the right menu, then enter the Date and Time you want the change to take effect.


WooCommerce Memberships Settings, If a non-member tries to visit the URL for restricted content, a 404 page will be displayed. Viewing restricted: image removed, non-purchasable it can dynamically display the correct restriction message (i.e., one for products or one for content). You can opt to customize this page or use a generic landing page instead by  Display a custom Product badge on the WooCommerce Single Product Page Part 1 – PHP Snippet: Display Checkbox @ Product Edit Page. First we need to create a new checkbox which will give us control over showing the badge or not. This is pretty simple to do.


Product Availability Slots for WooCommerce, If you wish to hide Expired/Non-Purchasable products from the shop page, When enabled, Expired/Non-Purchasable Simple Products will be moved to draft. By default, timezone details will be displayed to the user in the product page. In this section, the messages displayed while interacting with Auction Products can  You can choose to not use Layouts on Products, this is made by just assigning the layout from products, and setting the WooCommerce Views Settings to not use our template but WooCommerce's template. Then, Products single pages won't display with the layout.