0

I have create a custom meta key into the post table and it's value is in serialized form now when I dump the value it is giving me like this

Array ( [0] => a:3:{s:10:"product_id";s:4:"2592";s:7:"user_id";i:41;i:0;a:1:{i:0;s:63:"a:3:{s:10:"product_id";s:4:"2592";s:7:"user_id";i:2;i:0;a:0:{}}";}} )

I want to get user_id and product_id from it. The main idea is to hide a button for those user whose ID's are stored into this meta_key for the particular product whose ID are in this serialized array as well any idea how to do that I have tried the following code but nothing happens

$next_in_line = get_post_meta(esc_attr($product->get_id()), '_next_in_line');
print_r($next_in_line);
$unserilize_data = unserialize($next_in_line);
print_r($unserilize_data['0']);

But is giving me bool(false) and when I use this function

$next_in_line = get_post_meta(esc_attr($product->get_id()), '_next_in_line');
print_r($next_in_line);
$un_data = maybe_unserialize($next_in_line);
print_r($un_data['0']);

it is returning me values in the following format

a:3:{s:10:"product_id";s:4:"2592";s:7:"user_id";i:41;i:0;a:1:{i:0;s:63:"a:3:{s:10:"product_id";s:4:"2592";s:7:"user_id";i:2;i:0;a:0:{}}";}}

Now how can I make the condition that I have mentioned above I am blank at this point please some one help me out. Thank you in advance

UPDATE

I am storing data like this

function nextInLine(){
$product_id = $_POST['product_id'];
$user_id = get_current_user_id();

$next_customer_meta = get_post_meta($product_id, '_next_in_line');

if($next_customer_meta == ''){
    $meta_array = array(
        'product_id' => $product_id,
        'user_id' => $user_id
    );

    $meta_value = maybe_serialize($meta_array);
} else {
    $meta_array = array(
        'product_id' => $product_id,
        'user_id' => $user_id
    );

    array_push($meta_array, $next_customer_meta);
    $meta_value = maybe_serialize($meta_array);
}
var_dump($meta_value);
update_post_meta($product_id, '_next_in_line', $meta_value);
return true;    

}
 add_action('wp_ajax_nextInLine', 'nextInLine');
 add_action('wp_ajax_nopriv_nextInLine', 'nextInLine');
  • You shouldn’t need to unserialize it. update_post_meta and get_post_meta handle that process for you. How are you saving the data? – Jacob Peattie Dec 22 '17 at 7:20
  • Please see I have updated my question and added the code for storing data please see it – Mohsin Dec 22 '17 at 7:32
  • You're serializing the data when you store it. Don't. update_post_meta does that for you. – Jacob Peattie Dec 22 '17 at 9:06
0

Use this:

$next_in_line = get_post_meta(esc_attr($product->get_id()), '_next_in_line', true);
print_r($next_in_line['user_id']);
print_r($next_in_line['product_id']);

Let me know if works.

  • The code you give me returns aa that's it no user_id not any product_id – Mohsin Dec 22 '17 at 7:30
  • I am getting 41 & 2592 for user_id and product_id using your array value. Please check your code again – Sid Dec 22 '17 at 7:38
  • I have checked it again it is returning me aa please check have i stored the data in the correct way or not I have updated my question – Mohsin Dec 22 '17 at 7:43
  • But it is returning me only one user_id and one product_id but I got two user_id's and two product_id's how to get all the values in array format – Mohsin Dec 22 '17 at 8:01
  • you will have to insert a foreach loop in there somewhere to loop through all users and products. – Sid Dec 22 '17 at 9:02
0

I have solved it the other way by changing my logic what I have done is not a proper workaround but it is working like a charm here's what I have done.

I am now sending only the user_id into the database without serializing.

function nextInLine(){
$product_id = $_POST['product_id'];
$user_id = get_current_user_id();

$next_customer_meta = get_post_meta($product_id, '_next_in_line');
var_dump($next_customer_meta);

if( empty($next_customer_meta) ){
    $meta_value = $user_id;
} else {
    $user_id = $next_customer_meta['0'] . ',' . $user_id;
    $meta_value = $user_id;
}
update_post_meta($product_id, '_next_in_line', $meta_value);
return true;    

 }
  add_action('wp_ajax_nextInLine', 'nextInLine');
  add_action('wp_ajax_nopriv_nextInLine', 'nextInLine');

And then while getting it and writing the condition I used this code.

<?php
            if(is_user_logged_in()){
                $next_in_line = get_post_meta(esc_attr($product->get_id()), '_next_in_line', true);
                $status_data = explode(',', $next_in_line);
                if( !in_array(get_current_user_id(), $status_data) ){
        ?>
            <button class="btn btn-sm btn-default" id="next-btn" value="<?php echo $product->get_id(); ?>">
                Next 
            </button>
            <span class="text-tiny">
                    Press this button to get in line for this item
            </span>
        <?php
                } else {
                    echo "<h4>You are already in line for this item</h4>";
                } 
            } else { 
        ?>
            <a href="<?php echo get_site_url() . '/my-account'; ?>">Login 
            </a><span>to get in line for this item.</span>
        <?php } ?>
    </div>
    <?php } ?>

The idea was to hide the button if user has already hit it so from this logic I have achieved what I was intended to achieve.

Thank you all for your precious time and effort it really means a lot to me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.