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
|
Show 4 more comments
1 Answer
Reset to default 0You'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
版权声明:本文标题:post meta - get_post_meta and add_post_meta not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745301547a2652399.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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