woocommerce_get_availability 过滤器 Hook 中的 PHP 错误

标签 php wordpress woocommerce product availability

在 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/

相关文章:

php - 以编程方式添加自定义设置选项卡以管理 WooCommerce 中的产品数据

php - 在 Woocommerce 3 中更改订单商品价格

php - Mysqli while 循环在明显非空结果集上返回空数组

php - 在 PHP 循环中将 'end' 基础类添加到 div 的样式不正确

php - 优化评级数组

jquery - 弹出窗口关闭时停止youtube视频

php - 从 WooCommerce 订单获取 protected 自定义订单项元数据数组

php - mysql 连接两个表

php - 提交表单后生成包含详细信息的唯一页面

php - 在 if 语句表达式中设置变量