woocommerce_get_availability过滤器钩子中的PHP错误

时间:2017-02-24 03:29:20

标签: php wordpress woocommerce product availability

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

1 个答案:

答案 0 :(得分: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;
}