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

自作多情 提交于 2020-01-03 05:20:09

问题


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.


回答1:


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




回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/55427175/display-a-custom-message-when-a-product-is-not-purchassable-in-woocommerce-singl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!