admin管理员组

文章数量:1419220

I have a code like below but it returns the false. There is data in $ggowlccpy_transaction_details_capture and the order id is being correctly passed. This is for woocommerce order meta.

I don't know why it is getting saved.

<?php
$ggowlccpy_transaction_details_capture = array(
    'merchant_ref'    => $ggowlccpy_order_id,
    'transaction_id'  => $ggowlccpy_response['transaction_id'],
    'transaction_tag' => $ggowlccpy_response['transaction_tag'],
    'method'          => $ggowlccpy_response['method'],
    'amount'          => $ggowlccpy_response['amount'],
    'currency_code'   => $ggowlccpy_response['currency'],
    'positionofsave'  => 'captured',
);

update_post_meta(
    $post->ID,
    '_ggowlccpy_trasaction_capture',
    $ggowlccpy_transaction_details_capture
);

add_post_meta(
    $post->ID,
    '_ggowlccpy_refund_details_get',
    base64_encode( serialize( $ggowlccpy_transaction_details_capture ) )
);

$data = get_post_meta(
    $ggowlccpy_order_id,
    $key = '_ggowlccpy_refund_details_get',
    $single = false
);

$tb_meta_unserialized = unserialize( base64_decode( $data ) );

error_log( var_export( $tb_meta_unserialized, 1 ) );

echo $tb_meta_unserialized;

I have a code like below but it returns the false. There is data in $ggowlccpy_transaction_details_capture and the order id is being correctly passed. This is for woocommerce order meta.

I don't know why it is getting saved.

<?php
$ggowlccpy_transaction_details_capture = array(
    'merchant_ref'    => $ggowlccpy_order_id,
    'transaction_id'  => $ggowlccpy_response['transaction_id'],
    'transaction_tag' => $ggowlccpy_response['transaction_tag'],
    'method'          => $ggowlccpy_response['method'],
    'amount'          => $ggowlccpy_response['amount'],
    'currency_code'   => $ggowlccpy_response['currency'],
    'positionofsave'  => 'captured',
);

update_post_meta(
    $post->ID,
    '_ggowlccpy_trasaction_capture',
    $ggowlccpy_transaction_details_capture
);

add_post_meta(
    $post->ID,
    '_ggowlccpy_refund_details_get',
    base64_encode( serialize( $ggowlccpy_transaction_details_capture ) )
);

$data = get_post_meta(
    $ggowlccpy_order_id,
    $key = '_ggowlccpy_refund_details_get',
    $single = false
);

$tb_meta_unserialized = unserialize( base64_decode( $data ) );

error_log( var_export( $tb_meta_unserialized, 1 ) );

echo $tb_meta_unserialized;
Share Improve this question edited Jul 23, 2019 at 16:08 asked Jul 23, 2019 at 13:59 user145078user145078 9
  • Which bit returns false? It's the wp_postmeta table. – Rup Commented Jul 23, 2019 at 14:06
  • 2 get_post_meta( $ggowlccpy_order_id, - no, that should be the post ID not the order ID. So that's what's failing: you actually want to search the post meta for a serialized value and you don't know the post ID? That's not get_post_metae. – Rup Commented Jul 23, 2019 at 14:08
  • @Rup, you should make it an answer. – Max Yudin Commented Jul 23, 2019 at 14:26
  • @MaxYudin Thanks, but the answer would be how to do that search correctly. And I don't have a good solution for that. I'd guess you'd have to save the order ID in a separate post meta value, or leaving it as-is do a LIKE search for the ID and then unserialize all the results to see which ones actually match. – Rup Commented Jul 23, 2019 at 14:28
  • 1 @Latheesh, WooCommerce's database tables are off-topic here until they are not native WordPress tables. @ Rup pointed you to the right code line. – Max Yudin Commented Jul 23, 2019 at 15:00
 |  Show 4 more comments

1 Answer 1

Reset to default 0

You're saving the transaction details as a serialized object in post-meta and then you're trying to search for a specific order ID. Your problem is here:

$data = get_post_meta( $ggowlccpy_order_id, $key = '_ggowlccpy_refund_details_get',
                       $single = false );

get_post_meta requires a post ID but you're passing it the order ID you want to search for.

How to search for the order ID? The problem is that it's packed into a serialized object. One way would be to also store the order ID as a separate meta value so that you can search for it directly, e.g. (untested sorry)

add_post_meta( $post->ID, '_ggowlccpy_merchant_ref', $ggowlccpy_order_id);

$meta_query = new WP_Meta_Query(array(
                  meta_key = '_ggowlccpy_merchant_ref',
                  meta_value = $ggowlccpy_order_id,
                  compare = '='));
$orders = $wpdb->get_results($meta_query->get_sql( 'post', $wpdb->posts, 'ID' ));

It won't be possible to match the existing serialized value: you're base64-encoding it to obscure the value in the database. (At least I guess this is to obscure it: you don't generally need to base64-encode the output of PHP serialize value.) If this wasn't base64-encoded then you could e.g. serialize the order ID and then search for _ggowlccpy_refund_details_get values that are LIKE (i.e. contain as a substring) the serialized order ID, then deserialize all of the results and check each one for merchant_ref = order ID until you find the correct one. However if you do need to query by this then I think a separate new post_meta value is easier and better.

本文标签: post metagetpostmeta and addpostmeta not working