[英]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.