从 Aelia Currency Switcher 获取 '_order_total_base_currency' 值并显示在自定义字段元框

Get the '_order_total_base_currency' value from the Aelia Currency Switcher and display at custom field meta-box

我喜欢从 Aelia Currency Switcher 插件获取值,即“_order_total_base_currency”并进行简单的美元转换,然后将其显示在我的自定义字段元数据框中。如何获取该值以便我可以将其用于计算,然后显示?

这是我的代码:

// Adding the metabox (on the right side)
add_action( 'add_meta_boxes', 'cdmb_add_meta_box');
function cdmb_add_meta_box() {

    add_meta_box(
        'woocommerce-order-my-custom',
        __('USD Currency display'),
        'cdmb_display_meta_box',
        'shop_order',
        'side',
        'core'
    );
}
// The metabox content
function cdmb_display_meta_box() {
    // Get

    $total_usd = (get_post_meta( $post->ID, '_order_total_base_currency', true )) / 0.75;
    $total_usd .= get_post_meta( $post->ID, '_order_total_base_currency', true );

    echo '<p>' . $total_usd . '</p>';

}

// Save/Update the meta data
add_action( 'save_post', 'cdmb_save_meta_box_data' );
function cdmb_save_meta_box_data( $post_id ) {

// Only for shop order
if ( 'shop_order' != $_POST[ 'post_type' ] )
    return $post_id;

// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return $post_id;

## SETTING AND UPDATING DATA ##

update_post_meta( $post_id, 'total-usd', sanitize_text_field( $_POST[ 'total-usd' ] ) );
}

?>

花时间研究后,我找到了答案。我现在可以从 Aelia Currency Switcher 插件中获取“_order_total_base_currency”的值。

需要全局$post;在变量 $total_usd.

之前

代码应该是这样的:

function cdmb_display_meta_box() {
    // Get

global $post;
    $total_usd = get_post_meta( $post->ID, '_order_total_base_currency', true );

    echo '<p>' . $total_usd . '</p>';