Jump to content

Unserialized string not parsing as array


mushroominternet

Recommended Posts

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

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 £$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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.