简体   繁体   English

在Woocommerce单页中无法购买产品时显示自定义消息

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

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. 使用Advanced Custom Fields插件,我在Woocommerce产品发布类型(复选框)月份列表中添加了一个自定义字段,以从中选择可用的产品。

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. 您的代码中存在一些错误,例如为此钩子声明了2个缺少的函数变量。

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). 代码进入您的活动子主题(或活动主题)的function.php文件中。 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>';

在此处输入图片说明

You can use the is_purchasable within your template to display a message 您可以在模板中使用is_purchasable来显示消息

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 如果您只想返回true或false,则可以做些简短的旁注

return in_array($currentMonth, $months)

as in_array returns a boolean value anyway 因为in_array无论如何都会返回一个布尔值

Since you have a method to check for product available month, you can write that code as a seperate function in your functions.php . 由于您有检查产品可用月份的方法,因此可以将该代码作为单独的函数编写在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 因此,您的woocommerce_is_purchasable函数可以修改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Woocommerce 单个产品页面中显示自定义分类 - Display a custom taxonomy in Woocommerce single product pages 在 Woocommerce 单个产品页面中显示特定产品标签的自定义内容 - Display custom content for specific product tags in Woocommerce single product pages 在 Woocommerce 的单个产品页面上显示特定的自定义产品属性 - Display specific custom product attributes on single product pages in Woocommerce 在Woocommerce单个产品页面中显示高于产品价格的自定义字段 - Display a custom field above product price in Woocommerce single product pages 显示自定义字段吹产品标题Woocommerce产品单页 - Display custom field blow product title Woocommerce product single pages 通过钩子在 WooCommerce 单个产品页面中显示自定义字段值 - Display custom field value in WooCommerce single product pages via hook 在 Woocommerce 单个产品页面的简短描述下显示自定义字段 - Display a custom field under short description in Woocommerce single product pages Woocommerce 上单个产品页面中的自定义数量 - Custom quantities in single product pages on Woocommerce 在Woocommerce的单个产品页面上显示优惠券代码 - Display a coupon code on single product pages in Woocommerce 当价格字段为空时,在 WooCommerce 中的单个产品页面上显示产品库存状态 - Display product stock status on single product pages in WooCommerce when price field is empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM