所以我在產品中有自定義文本欄位元,所以如果有一個 url,我想在結帳后將用戶重定向到自定義 TY 頁面。這是我的代碼,而不是重定向!!
add_action('template_redirect', 'my_redirect_after_purchase');
function my_redirect_after_purchase() {
global $post, $wp;
$my_redirect = get_post_meta( get_the_ID(), '_my_redirect', true ); // This has the redirect URL entered in the field tested and working
if ( '' !== $my_redirect ) { // am checking here if the field is not empty
if (!empty($wp->query_vars['order-received'])) {
wp_redirect(esc_url($my_redirect));
exit;
}
}
}
我嘗試了其他幾種方法,但也沒有運氣。是在猜測訂單收到的查詢之后運行,因此元回傳空?
我應該怎么做?謝謝
uj5u.com熱心網友回復:
首先,您必須使用獲取訂單項,wc_get_order然后才能根據產品 ID 獲取元資料。試試下面的代碼。
add_action('template_redirect', 'my_redirect_after_purchase');
function my_redirect_after_purchase() {
/* return if we are not on order-received page */
if( !is_wc_endpoint_url( 'order-received' ) || empty( $_GET['key'] ) ) {
return;
}
$order_id = wc_get_order_id_by_order_key( $_GET['key'] );
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item ) {
$my_redirect = get_post_meta( $item['product_id'] , '_my_redirect', true );
if( $my_redirect != '' ){
wp_redirect( $my_redirect );
exit;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342800.html
標籤:php WordPress的 求购
