3

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

警告:在第 32 行的 /home//wp-content/themes/flat/functions.php 中为 foreach() 提供的参数无效

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

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;
}
4

3 回答 3

2

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

过滤器woocommerce_get_availability钩子:用于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在方法代码的末尾直接使用过滤钩子。

1)使用woocommerce_get_availability_text过滤钩(最佳选择)

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过滤钩。

在这里,您需要以'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 文件中或任何插件文件中。

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


参考:动作和过滤器挂钩参考

于 2017-02-24T05:03:09.407 回答
0

由于错误出现在 foreach 行上,因此您可能正在尝试迭代一个不是数组的变量。如果 $availability 变量是数组,则添加条件检查应该可以解决问题。像这样:

function backorder_text($available) {
    if (is_array($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 );
        }
    } else {
            $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;
}

但是,还有更好的方法。由于 str_replace 可以接受数组或字符串作为参数,因此这也适用于所有情况。这实际上是您的代码在工作时所做的事情。在您的代码中,foreach 循环是不必要的,因为您在 $availability 变量而不是该数组 ($i) 中的元素上调用 str_replace。以下更简单,无论 $availability 是数组还是字符串都可以使用。

function backorder_text($available) {
     return 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);
}
于 2017-02-24T05:04:41.817 回答
0

使用is_array函数检查它是否是一个数组

function backorder_text($available) {
    is_array($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);

   }
} else{

     $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;
}
于 2017-02-24T03:47:34.247 回答