Jump to content

Unserialized string not parsing as array


Recommended Posts

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


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


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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.