我目前正在开发一个 WooCommerce 项目,我正在尝试实现一项功能,当用户在 WooCommerce 管理面板中的订单编辑过程中更改付款方式时,会添加订单备注。但是,我在获取新的付款方式时遇到了问题。
这是一个例子
这是我正在使用的代码:
add_action('woocommerce_admin_order_data_after_billing_address', 'order_update_note');
function order_update_note()
{
global $post;
$order_id = get_the_ID();
$order = wc_get_order($order_id);
$current_user = wp_get_current_user();
$user_now = $current_user->display_name;
$current_payment_method = $order->get_payment_method();
?>
<script>
jQuery(document).ready(function($) {
$('.save_order').on('click', function() {
var orderid = <?php echo $order_id; ?>;
var user_now = '<?php echo $user_now; ?>';
var payment_method = $('#order_data .wc-order-data-row .order_data_column .order_data_column-inner select[name="_payment_method"]').val();
console.log(orderid);
console.log(user_now);
console.log(payment_method);
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type: 'POST',
data: {
action: 'update_order_add_note',
orderid: orderid,
user_now: user_now,
payment_method: payment_method
},
success: function( response ) {
console.log( 'Custom field updated successfully.' );
},
error: function( response ) {
console.log( 'Failed to Submit Expense.' );
}
});
});
});
</script>
<?php
}
add_action('wp_ajax_update_order_add_note', 'update_order_add_note');
add_action('wp_ajax_nopriv_update_order_add_note', 'update_order_add_note');
function update_order_add_note()
{
$orderid = $_POST['orderid'];
$user_now = $_POST['user_now'];
$order = wc_get_order($orderid);
$payment_method = $_POST['payment_method'];
$current_payment_method = $order->get_payment_method();
if ($payment_method !== $current_payment_method) {
$note = "Order details updated by ".$user_now . " Payment method changed from ".$current_payment_method." to ".$payment_method."\n";
$order->add_order_note($note);
$order->save();
wp_send_json_success();
} else {
wp_send_json_error();
}
}
这是我的代码的细分:
我添加了一个自定义 JavaScript 函数,该函数在 WooCommerce 订单编辑屏幕中单击“保存订单”按钮时运行。此函数捕获订单 ID、进行更改的用户以及用户选择的付款方式。
在 PHP 端,我设置了一个 AJAX 回调函数,用于接收 JavaScript 函数发送的数据。在此回调函数中,我尝试捕获新的付款方式并将其与订单的现有付款方式进行比较。
问题是我没有成功获取新的付款方式。变量
$payment_method
如果用户在订单编辑过程中更改新的付款方式,有关如何正确捕获新付款方式的任何见解或建议,我将不胜感激
您可以使用以下不同且最有效的方法:
// When order is created after checkout, add a custom field
add_action( 'woocommerce_checkout_create_order', 'save_payment_method_origin', 20, 2 );
function save_payment_method_origin( $order, $data ) {
if( $payment_method = $order->get_payment_method() ) {
$order->add_meta_data('_payment_origin', $payment_method );
}
}
// Add 2 hidden fields on admin single order
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'admin_order_custom_hidden_input_fields' );
function admin_order_custom_hidden_input_fields( $order ) {
global $current_user;
echo '<input type="hidden" name="user_name" value="'.$current_user->display_name.'" />
<input type="hidden" name="user_id" value="'.$current_user->ID.'" />';
}
// Adding an order note when payment method is changed
add_action( 'woocommerce_process_shop_order_meta', 'admin_order_payment_method_change' );
function admin_order_payment_method_change( $order_id ) {
$order = wc_get_order( $order_id );
$payment_method = $order->get_payment_method();
$payment_origin = $order->get_meta('_payment_origin');
if ( ! empty($payment_origin) && $payment_method != $payment_origin
&& isset($_POST['user_name']) && isset($_POST['user_id']) ) {
$order->update_meta_data('_payment_origin', $payment_method);
$order->add_order_note( sprintf( __('Payment method changed from %s to %s by %s (Id %d)'),
$payment_origin, $payment_method, esc_attr($_POST['user_name']), intval($_POST['user_id']) ) . "\n" );
$order->save();
} elseif ( empty($payment_origin) && ! empty($payment_method) ) {
$order->update_meta_data('_payment_origin', $payment_method);
$order->save();
}
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。