2017-02-24 59 views
2

在WooCommerce,由於某種原因,我得到這個錯誤:PHP錯誤鉤

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

回答

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

您可以使用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文件或也以任何插件文件。

我沒有測試你的代碼,因爲它非常具體,但它應該工作。


參考:Action and Filter Hook Reference

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變量上調用str_replace而不是該數組中的元素($ i)。以下更簡單,無論$ 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); 
}