您的实际代码有点过时,不太安全……因为WooCommerce 3订单项现在是对象$item[\'product_id\']
替换为$item->get_product_id()
.
您应该首先尝试使用woocommerce_get_return_url
例如:
add_action( \'woocommerce_get_return_url\', \'custom_order_received_return_url\', 10, 2 );
function custom_order_received_redirect( $return_url, $order ){
// HERE define your products IDs in the array
$product_ids = array(741);
// HERE define your redirection URL (with the order ID as argument if needed)
$redirection_url = home_url(\'/ar1m/\');
if ( is_ssl() || get_option( \'woocommerce_force_ssl_checkout\' ) == \'yes\' ) {
$redirection_url = str_replace( \'http:\', \'https:\', $redirection_url );
}
// Loop through order items
foreach( $order->get_items() as $item ) {
if( in_array( $item->get_product_id(), $product_ids ) ) {
return $redirection_url;
}
}
}
代码进入功能。活动子主题(或活动主题)的php文件。它应该会起作用。
如果没有,以下代码将在“订单已收到”页面上为定义的产品ID进行自定义重定向,emptying cart (如果需要)并检查订单是否有效:
add_action( \'template_redirect\', \'custom_order_received_redirect\' );
function custom_order_received_redirect(){
// Only on order received (thankyou) page for a valid Order ID
if( is_wc_endpoint_url(\'order-received\')
&& ( $order_id = absint( get_query_var(\'order-received\') ) )
&& get_post_type($order_id) === \'shop_order\'
&& isset( $_GET[\'key\'] ) ) {
// Empty cart
if ( ! WC()->cart->is_empty() ) {
WC()->cart->empty_cart();
}
// Get an instance of the WC_Order Object
$order = wc_get_order( $order_id );
// Check that the order key is valid
if( is_a($order, \'WC_Order\') && $order->get_order_key() === esc_attr($_GET[\'key\']) ) {
// HERE define your products IDs in the array
$product_ids = array(2781);
// HERE define your redirection URL (with the order ID as argument if needed)
$redirection_url = home_url("/ar1m/");
// Loop through order items
foreach( $order->get_items() as $item ) {
if( in_array( $item->get_product_id(), $product_ids ) ) {
wp_redirect( $redirection_url );
exit();
}
}
}
}
}
代码进入功能。活动子主题(或活动主题)的php文件。已测试并正常工作。
您可以通过Order ID 在重定向url中,如:
$redirection_url = home_url("/ar1m/?order_id=$order_id");
然后你就可以得到
Order ID 在重定向页面上,使用:
if( isset($_GET[\'order_id\']) && get_post_type($_GET[\'order_id\']) === \'shop_order\' ) {
$order_id = absint($_GET[\'order_id\']);
}