我基本上想根据订单状态和订单密钥对来更改通知状态代码。
例如,当订单被取消或失败时,当前woocommerce显示失败消息。
现在,当我将订单接收页面的URL中的order_key更改为其他内容(hghghsgyd3)时,它会加载感谢页面,尽管特定的订单应该显示失败的消息。为什么会这样,而且url中的密钥与我从$var = $object->key_is_valid( $key );
获得的密钥不同吗?如果你们中的任何一个可以解释我,那将是很棒的。我的以下代码在stackoverflow的帮助下得到答案:
add_action( 'template_redirect', 'test_key_is_valid' );
function test_key_is_valid()
{
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
$object = new WC_Order($order_id);
$var = $object->key_is_valid( $key );
// print_r($var)."OK";
$object->save();
$test_order_key = $object->order_key;
// $test_order_key.'<br>';
$current_url = home_url(add_query_arg(array(),$wp->request)).'/?key='.$test_order_key;
// echo $current_url.'<br>';
/*$newurl = site_url().'/checkout/order-received/'.$current_url.'?key='.$test_order_key;
echo $newurl.'<br>';*/
//$current_url = home_url(add_query_arg(array(),$wp->request));
//echo $currlent_url.'<br>';
//$order = $object->get_id();
//$order_id = $object->get_user_id();
//$order = wc_get_order( $order_id );
//$order_data = $order->get_data();
//$order_status = $object['status'];
//echo $order_status;
/*if ( 'processing' == $order_status &&
( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) {
echo "failed";
}*/
$uri = $_SERVER['REQUEST_URI'];
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
if ($current_url === $url){
echo "OK";
}
else
{
do_action( 'wc_custom_thankyou_failed', $order );}
}
}
add_action( 'wc_custom_thankyou_failed', 'wc_custom_thankyou_failed', 10 );
function wc_custom_thankyou_failed( $order ) {
wc_get_template( 'failed', array( 'order' => $order ) );
}