在Woocommerce管理员中创建手动订单时替换订单币种

时间:2019-04-21 13:43:05

标签: php wordpress woocommerce metadata orders

在WooCommerce中,通过Admin创建和保存手动订单时,我试图将订单货币值替换为自定义元数据值(其元键为_wcj_order_currency < / p>

以下是相关元数据(键/值对)的2个屏幕截图:

  • 订单币种:

    enter image description here

  • 自定义货币(来自Booster插件)

    enter image description here

因此,我想从EUR元键将订单货币 USD 替换为自定义货币 _order_currency >保存

我使用的参考文献:

我的代码尝试:

// Saving (Updating) or doing an action when submitting
add_action( 'save_post', 'update_order_custom_field_value' );
function update_order_custom_field_value( $post_id ){

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

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

    // Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
    if ( ! current_user_can( 'edit_shop_order', $post_id ) )
        return $post_id;

//Up to above is fine for Admin order

    // Updating custom field data
    if( isset( $_POST['_wcj_order_currency'] ) ) {
        $order = wc_get_order( $post_id );

        // Replacing and updating the value
        update_post_meta( $post_id, '_order_currency', $_POST['_wcj_order_currency'] );
}}

// Testing output in order edit pages (below billing address):
//This displays the existing values well
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_order_custom_field_value' );
function display_order_custom_field_value( $order ){
    echo '<p><strong>'.__('Order Currency').':</strong> <br/>' . get_post_meta( $order->get_id(), '_order_currency', true ) . '</p>';
    echo '<p><strong>'.__('Booster Order Currency').':</strong> <br/>' . get_post_meta( $order->get_id(), '_wcj_order_currency', true ) . '</p>';
}

在订单编辑页面(在帐单地址下方)测试输出。该代码运行良好。

Testing output in order edit pages (below billing address)

但是我无法使其工作并在创建订单时更新订单货币。

欢迎对此提供任何帮助。

1 个答案:

答案 0 :(得分:1)

要使您的脚本仅适用于新订单创建,请尝试以下重新访问的代码(有注释):

// Save order data
add_action( 'save_post_shop_order', 'update_order_currency_on_creation', 1000 );
function update_order_currency_on_creation( $order_id ){
    // Ensure that this is a manual new order 
    if( $created = get_post_meta( $order_id, '_created_via', true ) ) {
        return $order_id;
    }

    // Checking that is not an autosave  (not sure that this is really needed on Woocommerce orders)
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $order_id;
    }

    // Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
    if ( ! current_user_can( 'edit_shop_order', $order_id ) ) {
        return $order_id;
    }

    ## ---- Updating order currency ---- ##

    // Get the WC_Order object
    $order    = wc_get_order($order_id); 

    // HERE below the Booster meta key for Order currency
    $meta_key = '_wcj_order_currency';

    // If Booster currency is already in database (in case of, to be sure)
    if ( $value = $order->get_meta($meta_key) ) {
        $order->set_currency( esc_attr($value) );
        $order->save(); // Save order data
    }
    // If not, we get the posted Booster currency value (else)
    elseif ( isset($_POST[$meta_key]) && ( $value = esc_attr($_POST[$meta_key]) ) ) {
        $order->set_currency( esc_attr($_POST[$meta_key]) );
        $order->save(); // Save order data
    }
}


// Testing output in order edit pages (below billing address): This displays the existing values as well.
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_specific_order_details' );
function display_specific_order_details( $order ){
    echo '<div><p><strong>'.__('Order Currency').':</strong> ' . $order->get_currency() . '</p>
    <p><strong>'.__('Booster Order Currency').':</strong> ' . $order->get_meta( '_wcj_order_currency' ) . '</p></div>';
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。