我使用 wooCommerce 主題,這是我在每個產品代碼結帳后的作業重定向:
add_action( 'woocommerce_thankyou', 'redirect_product_based', 1 );
function redirect_product_based ( $order_id ){
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item ) {
// Add whatever product id you want below here
if ( $item['product_id'] == 3531 ) {
// URL
wp_redirect( 'www...' );
}
if ( $item['product_id'] == 35 ) {
// URL
wp_redirect( 'www....' );
}
}
}
現在我希望代碼適用于多種產品,例如:
add_action( 'woocommerce_thankyou', 'redirect_product_based', 1 );
function redirect_product_based ( $order_id ){
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item ) {
// Add whatever product id you want below here
if ( $item['product_id'] == 331, 332, ... ) {
// URL
wp_redirect( 'www...' );
}
if ( $item['product_id'] == 35, 36, ... ) {
// URL
wp_redirect( 'www....' );
}
}
}
請在這方面提供任何幫助以申請多種產品。
uj5u.com熱心網友回復:
盡可能少地修改您的作業代碼來實作此目的的最簡單方法是使用in_array()
.
另外,請注意exit;
after的使用wp_redirect
。你可以看到我為什么在這篇文章和檔案中添加它:
注意: wp_redirect() 不會自動退出,并且應該幾乎總是在呼叫退出之后進行;
此外,如果您想確保始終重定向到允許主機上的頁面,您應該查看wp_safe_redirect
并替換您的wp_redirect
呼叫
add_action( 'woocommerce_thankyou', 'redirect_product_based', 1 );
function redirect_product_based ( $order_id ){
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item ) {
// Add whatever product id you want below here
if ( in_array($item['product_id'], array(331, 332)) ) {
// URL
wp_redirect( 'www...' );
exit;
}
if ( in_array($item['product_id'], array(35, 36)) ) {
// URL
wp_redirect( 'www....' );
exit;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492733.html