get_post_meta not worked

  • Unknown's avatar

    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.

  • Unknown's avatar

    Hi there!

    Since your question is specific to the WooCommerce plugin, I recommend posting it on the WooCommerce support forum, as we can only help with WordPress.com sites here.

    Thanks!

  • Unknown's avatar

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

  • Unknown's avatar

    @ritu31dodiya do read the answer Staff gave you above and act accordingly as we can’t help at all.

  • The topic ‘get_post_meta not worked’ is closed to new replies.