gpt4 book ai didi

woocommerce_get_availability 过滤器 Hook 中的 PHP 错误

转载 作者:行者123 更新时间:2023-12-04 03:14:22 25 4
gpt4 key购买 nike

在 WooCommerce 中,出于某种原因我收到此错误:

Warning: Invalid argument supplied for foreach() in /home//wp-content/themes/flat/functions.php on line 32

该错误仅出现在简单产品中,不会出现具有多种变体的可变产品。这个错误似乎是在这一行:

foreach($available as $i) {

任何帮助都会很棒!!

这是我的代码:

/**
* Backorder Hook
**/

function backorder_text($available) {

foreach($available as $i) {

$available = str_replace('Available on backorder', 'This size is on backorder : Dont Miss out!<BR><span style="font-weight: normal;">Buy it now and we will dispatch as soon as they arrive</span>', $available);

}

return $available;
}
add_filter('woocommerce_get_availability', 'backorder_text');




add_filter( 'woocommerce_get_availability' , 'revised_woocommerce_get_availability' , 10, 2 );

function revised_woocommerce_get_availability( $available_array , $product) {

if ( $product->managing_stock() ) {

if ( !($product->is_in_stock() && $product->get_stock_quantity() > get_option( 'woocommerce_notify_no_stock_amount' )) && ($product->backorders_allowed() && $product->backorders_require_notification()) ) {

$custom_meta_value = get_post_meta( $product->id, 'Out_of_stock_message', true );
$available_array["availability"] = $custom_meta_value;

}

}
return $available_array;
}

最佳答案

您可以为此使用 2 个不同的钩子(Hook)。由于您对 2 个函数使用相同的 Hook ,因此您可以将其合并为一个函数。

woocommerce_get_availability 过滤器 Hook :用于 get_availability()方法:

public function get_availability() {
return apply_filters( 'woocommerce_get_availability', array(
'availability' => $this->get_availability_text(),
'class' => $this->get_availability_class(),
), $this );
}

因此您还可以看到它是一个包含 2 个键 'availability''class' 的数组.关键 'availability' 是您需要并使用 get_availability_text() 方法的那个,您可以使用直接在方法代码末尾的 woocommerce_get_availability_text 过滤 Hook 。

1) 使用 woocommerce_get_availability_text 过滤器 Hook (最佳选择):

add_filter( 'woocommerce_get_availability_text', 'customizing_availability_text', 10, 2);
function customizing_availability_text( $availability, $product ) {

if ( $_product->is_in_stock() )
$availability = str_replace('Available on backorder', 'This size is on backorder : Dont Miss out!<BR><span style="font-weight: normal;">Buy it now and we will dispatch as soon as they arrive</span>', $availability);

if ( $product->managing_stock() && !($product->is_in_stock() && $product->get_stock_quantity() > get_option( 'woocommerce_notify_no_stock_amount' )) && ($product->backorders_allowed() && $product->backorders_require_notification()) ) {
$availability = get_post_meta( $product->id, 'Out_of_stock_message', true );

return $availability;
}

2) 使用 woocommerce_get_availability 过滤器钩子(Hook)。

这里您需要以数组中的 'availability' 为目标,这样:

add_filter( 'woocommerce_get_availability', 'customizing_availability_text', 10, 2);
function customizing_availability_text( $availability, $product ) {

if ( $_product->is_in_stock() )
$availability['availability'] = str_replace('Available on backorder', 'This size is on backorder : Dont Miss out!<BR><span style="font-weight: normal;">Buy it now and we will dispatch as soon as they arrive</span>', $availability['availability']);

if ( $product->managing_stock() && !($product->is_in_stock() && $product->get_stock_quantity() > get_option( 'woocommerce_notify_no_stock_amount' )) && ($product->backorders_allowed() && $product->backorders_require_notification()) ) {
$availability['availability'] = get_post_meta( $product->id, 'Out_of_stock_message', true );

return $availability;
}

代码进入您活跃的子主题(或主题)的 function.php 文件或任何插件文件。

我没有测试你的代码,因为它非常具体,但它应该可以工作。


引用:Action and Filter Hook Reference

关于woocommerce_get_availability 过滤器 Hook 中的 PHP 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42430228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com