1

以下序列化字符串是从 Woocommerce 产品的元数据中提取的。

a:2:{s:17:"set_51fb76f97cc57";a:6:{s:15:"conditions_type";s:3:"all";s:10:"conditions";a:1:{i:1;a:2:{s:4:"type";s:8:"apply_to";s:4:"args";a:2:{s:10:"applies_to";s:5:"roles";s:5:"roles";a:1:{i:0;s:14:"trade_customer";}}}}s:9:"collector";a:1:{s:4:"type";s:7:"product";}s:4:"mode";s:5:"block";s:5:"rules";a:1:{i:1;a:4:{s:4:"from";s:0:"";s:2:"to";s:0:"";s:4:"type";s:14:"price_discount";s:6:"amount";s:0:"";}}s:10:"blockrules";a:1:{i:1;a:5:{s:4:"from";s:1:"*";s:6:"adjust";s:1:"1";s:4:"type";s:16:"fixed_adjustment";s:6:"amount";s:4:"8.37";s:9:"repeating";s:3:"yes";}}}s:17:"set_51fb76f97d6a2";a:6:{s:15:"conditions_type";s:3:"all";s:10:"conditions";a:1:{i:1;a:2:{s:4:"type";s:8:"apply_to";s:4:"args";a:2:{s:10:"applies_to";s:5:"roles";s:5:"roles";a:1:{i:0;s:19:"bulk_trade_customer";}}}}s:9:"collector";a:1:{s:4:"type";s:7:"product";}s:4:"mode";s:5:"block";s:5:"rules";a:1:{i:1;a:4:{s:4:"from";s:0:"";s:2:"to";s:0:"";s:4:"type";s:14:"price_discount";s:6:"amount";s:0:"";}}s:10:"blockrules";a:1:{i:1;a:5:{s:4:"from";s:1:"*";s:6:"adjust";s:1:"1";s:4:"type";s:16:"fixed_adjustment";s:6:"amount";s:5:"9.428";s:9:"repeating";s:3:"yes";}}}}

那是使用带有动态定价插件的 Woocommerce。

我也有我正在编写的这个函数,以便提取可变产品数据并将其显示在网站上:

function mi_price_adjust(){
    global $post, $current_user, $user_roles;
    $meta = get_post_meta($post->ID);
    $curPrice = (float)$meta['_regular_price'][0];
    $variations = unserialize($meta['_pricing_rules'][0]);
    $user_roles = $current_user->roles; 
    $theRoll = '';
    $thePrice = $curPrice;

    foreach($user_roles as $miroll){
        if(in_array($miroll, $user_roles)){
            $theRoll =  $miroll;
        }
    }

    if($theRoll != ''){ 
        foreach($variations as $curvar){
            $var_roll = $curvar['conditions'][1]['args']['roles'][0];
            $var_cost = (float)$curvar['blockrules'][1]['amount'];
            if($var_roll == $theRoll){
                $thePrice = $curPrice - $var_cost;  
            }
        }

    }

    if($thePrice != $curPrice){
        return "<strike>".$curPrice."</strike><br /><span class=\"youpayText\">You pay &pound;$thePrice</span>";
    }else{
        return  $thePrice;
    }

}

除了一件事,一切都运行良好。我收到以下错误:

为 foreach 提供的参数无效

有问题的行是

foreach($variations as $curvar){

并且 $variations 变量正在使用这个填充:

$variations = unserialize($meta['_pricing_rules'][0]);

它返回本文顶部的序列化字符串。

谁能解释为什么会发生这种情况以及我们可以做些什么来解决它或规避它?

4

0 回答 0