【发布时间】:2019-09-10 23:44:14
【问题描述】:
在 WooCommerce 中,通过管理员创建和保存手动订单时,我试图用自定义元数据值替换订单货币值(元键是 _wcj_order_currency)
以下是相关元数据(键/值对)的 2 个屏幕截图:
所以我想将订单货币 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>';
}
订单编辑页面(帐单地址下方)中的测试输出。代码运行良好。
但我无法使其工作并在创建订单时更新订单货币。
欢迎对此提供任何帮助。
【问题讨论】:
标签: php wordpress woocommerce metadata orders