• Resolved ritu31dodiya

    (@ritu31dodiya)


    I am currently working on a WooCommerce project and encountering an issue with retrieving a specific meta key value for orders. I have a function (Myplugin_admin_order_details) hooked to the woocommerce_admin_order_data_after_order_details action, and I’m attempting to retrieve the value of the _stripe_currency meta key for a given order.

    Here is the relevant code snippet:

    add_action( ‘woocommerce_admin_order_data_after_order_details’, ‘Myplugin_admin_order_details’, 10, 3 );

    function Myplugin_admin_order_details( $order ) {
    $order_id = $order->get_id();

    echo "<pre>"; 
    
    // This line prints all meta data, including 'my_custom_key'
    print_r( $order->get_meta_data() );
    
    // However, this line does not return any value for 'my_custom_key'
    echo get_post_meta( $order_id, "my_custom_key", true );

    }

    While $order->get_meta_data() correctly displays all meta data, the get_post_meta function does not return any value for the my_custom_key meta key, even though I am certain that the key exists for the order.

    why this might be happening and how I can successfully retrieve the value of the my_custom_key meta key.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Make carlos

    (@make-carlos)

    Hi

    have you tried this ?

    $order_data = $order->get_meta(‘_my_custom_key’);

    get_post_meta( $order_id, 'your_meta_key', true );

    This is NOT compatible with HPOS – High Performance Order Storage

    Thanks

    Plugin Support ckadenge (woo-hc)

    (@ckadenge)

    Hi @ritu31dodiya,

    From your code, it seems like you’re trying to retrieve the value of the “_stripe_currency” meta key, but in your get_post_meta function, you’re trying to retrieve “my_custom_key”.

    If you’re trying to get the value of “_stripe_currency”, you should replace “my_custom_key” with “_stripe_currency” in your get_post_meta function. So, it should look like this:

    get_post_meta( $order_id, "_stripe_currency", true );

    If you’re trying to retrieve the value of “my_custom_key”, make sure that “my_custom_key” is the correct meta key and that it exists for the order.

    I hope this helps!

    Thread Starter ritu31dodiya

    (@ritu31dodiya)

    I have saved both meta keys, but I am unable to retrieve the values for either of the meta keys.

    Saif

    (@babylon1999)

    Hello @ritu31dodiya,

    Most of the post-related functions/WordPress APIs like get_post, update_post_meta, wp_insert_post will no longer work with HPOS.

    To learn more about HPOS compatibility, please check: https://github.com/woocommerce/woocommerce/wiki/High-Performance-Order-Storage-Upgrade-Recipe-Book

    I have saved both meta keys, but I am unable to retrieve the values for either of the meta keys.

    You can use the get_meta method and pass the key:

    $order->get_meta('_my_key');

    If you need more help with development topics, I suggest posting in the?#developers?channel of the?Woo Community Slack,?as custom requests are not something we can generally assist with per our?support policy.

    Let us know if you have any other questions!

    Thread Starter ritu31dodiya

    (@ritu31dodiya)

    Thank you so much for your help!

    I’m glad we were able to help! If you have a few minutes, we’d love if you could leave us a review: https://www.ads-software.com/support/plugin/woocommerce/reviews/

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘get_post_meta not worked’ is closed to new replies.