【问题标题】:PHP error in woocommerce_get_availability filter hookwoocommerce_get_availability 过滤器挂钩中的 PHP 错误
【发布时间】:2017-02-24 03:29:20
【问题描述】:

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

【问题讨论】:

    标签: php wordpress woocommerce product availability


    【解决方案1】:

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

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


    参考:Action and Filter Hook Reference

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        由于错误出现在 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);
        }
        

        【讨论】:

          猜你喜欢
          • 2021-11-06
          • 2019-08-22
          • 1970-01-01
          • 1970-01-01
          • 2017-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多