donglun7151 2013-08-12 14:29
浏览 16

WordPress和Woocommerce与动态定价未序列化的字符串不解析为数组foreach

The following serialized string is being pulled from the meta data of a Woocommerce product.

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

That's using Woocommerce with the Dynamic Pricing plugin.

I also have this function which I am writing in order to pull out the variable product data and display it on the site:

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

}

Everything is working fine apart from one thing. I am getting the following error:

Invalid argument supplied for foreach

The line in question is

foreach($variations as $curvar){

And the $variations variable is being populated using this:

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

Which returns the serialized string at the top of this post.

Can anyone shed any light on why this might be happening and what we might be able to do to either fix it or circumvent it?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测