1

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

3 Answers3

4

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.

enter image description here


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>';

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • It hide the add to cart button and show the message if month is not in the array. But when month is in array it shows the button and upon adding to cart it says " Sorry, this product cannot be purchased." – Muhammad Tauseef Sep 19 '19 at 09:26
  • done deleted the new questions and your previous answer accepted. And my apologies if my doing so made an negative impact wasn't aware of that. So issue is that it does display the message and remove the cart button if the months is not in the array but when month is in array it does show the cart button but when I click on add to cart it doesn't add the product in the cart and give the error "Sorry, this product cannot be purchased." For example September is in the list of months when product is available it is showing add to cart button on the product page but when I add it I get the error. – Muhammad Tauseef Sep 19 '19 at 10:34
  • @Muhammad My answer here was just answering your code question, showing some mistakes and as this was based on a custom ACF field, it was not really testable completely. I am going to test that and to find out how to solve the problem. Give me some time and I will comment you here, when it's ready and updated. – LoicTheAztec Sep 19 '19 at 10:46
  • @MuhammadTauseef I have tested that under last Woocommerce version 3.7 on storefront theme and everything works perfectly, when product is available, clicking on the add to cart button does add the product to cart and there is no message saying that: "You cannot purchase this product"… or something similar. So There is something else that making trouble: It can be another customization made by you, the theme or a plugin... – LoicTheAztec Sep 22 '19 at 17:36
  • I installed the new wordpress and just only installed woocommerce and advanced custom field only and used storefront theme same as you did but I am getting this error. There is no extra script added be me and there is no other plugin activated except these two. – Muhammad Tauseef Sep 23 '19 at 06:36
  • Do you mind if I ask my other question regarding not adding the product in the cart in different questions so someone else can answer it for me. I know you have answered what has been asked the first time regarding displaying a message which is working but the new problem is different you said you have checked with the stronfront theme and woocommerce 3.7 and not getting this error I have done the same as well but I am still getting the error. – Muhammad Tauseef Sep 26 '19 at 19:57
  • @MuhammadTauseef As said, for me this **works fine on last WooCommerce version 3.7**… Of course you can ask a new question if you like, but use a different title with something like ""issue on Woocommerce 3.7" for example adding this answer as a link in your question. If someone can help you, no problem for me. – LoicTheAztec Sep 27 '19 at 02:28
0

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

James
  • 128
  • 3
  • Thanks. That would work but problem is I am creating product page through Elementor page builder so I cannot put this code in product template page. Is there any way to do it though add_filter so it removes the add to cart and show the message instead of it. – Muhammad Tauseef Mar 30 '19 at 02:53
0

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 );
melvin
  • 2,571
  • 1
  • 14
  • 37
  • It hide the add to cart button and show the message if month is not in the array. But when month is in array it shows the button and upon adding to cart it says " Sorry, this product cannot be purchased." – Muhammad Tauseef Sep 19 '19 at 09:27