在 WooCommerce 订单接收页面中通过 AJAX 更新订单状态
当客户通过 AJAX 单击感谢页面上的按钮时,我正在尝试将我的 woocommerce 订单的状态从处理更改为已完成.
I'm trying to change the status of my woocommerce orders from processing to completed when a client clicks a button on the thank you page via AJAX.
我的代码如下:
$.ajax({
type: 'post',
dataType: 'json',
url: ajax_url,
data: {action: 'redeem_complete'},
success: function(response){
alert("Order data successfully fetched.");
}
});
和我的functions.php中的php
and the php in my functions.php
add_action( 'wp_ajax_redeem_complete', 'fairbooks_redeem_complete');
add_action( 'wp_ajax_nopriv_redeem_complete', 'fairbooks_redeem_complete');
function fairbooks_redeem_complete($order_id){
$order = wc_get_order( $order_id );
$order->update_status( 'completed' );
echo 'test';
die();
}
我知道 ajax 在没有 $order->update_status( 'completed' );
语句的情况下可以工作,但是使用它我得到了 500 内部服务器错误.我也知道,如果我不使用 ajax 并连接到感谢页面,它会毫无问题地更改状态.是什么导致了错误,或者是否有更好的方法来解决这个问题.提前致谢!
I know the ajax works without the $order->update_status( 'completed' );
statement, but with it I get a 500 internal server error. I also know that If I don't use ajax and hook into the thank you page it changes the status without a problem. What is causing the error or is there perhaps a better way to go about this. Thanks in advance!
您需要将订单 ID 从您的 javascript 代码传递到 PHP,因为它没有在您的 PHP WordPress Ajax 接收器函数中定义,所以由于 $order
变量未定义并在使用方法 update_status()
时抛出错误,因此出现错误 500 是正常的.
You need to pass the order Id from your javascript code to PHP as its not defined in your PHP WordPress Ajax receiver function, so it's normal to get an error 500 as $order
variable is not defined and throw an error when using the method update_status()
.
以下完整示例基于您的代码(并且没有错误):
The following complete example is based on your code (and works without errors):
add_action( 'woocommerce_thankyou', 'customer_completes_order_thankyou' );
function customer_completes_order_thankyou( $order_id ) {
$order = wc_get_order( $order_id );
// print_r($order->get_status()); // Uncomment for testing
?>
<div><button class="button alt complete-status"><?php _e("Complete your order"); ?></button><br><br>
<span class="response" style="color:green;"></span></div>
<script type="text/javascript">
jQuery(function($){
if (typeof woocommerce_params === 'undefined')
return false;
$('button.complete-status').click( function(e){
e.preventDefault();
$.ajax({
type: 'POST',
dataType: 'json',
url: woocommerce_params.ajax_url,
data: {
'action': 'redeem_complete',
'order_id': <?php echo $order_id; ?>, // Here we send the order Id
},
success: function (response) {
$('.response').text("Order data successfully fetched.");
console.log("Order data successfully fetched.");
}
});
});
});
</script>
<?php
}
add_action( 'wp_ajax_redeem_complete', 'fairbooks_redeem_complete');
add_action( 'wp_ajax_nopriv_redeem_complete', 'fairbooks_redeem_complete');
function fairbooks_redeem_complete(){
if ( isset($_POST['order_id']) && $_POST['order_id'] > 0 ) {
$order = wc_get_order($_POST['order_id']);
$order->update_status('completed');
die();
}
}
代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.