[英]Woocommerce custom redirection based on purchased multiple products after payment
I use wooCommerce theme and this my working redirect after checkout per product code :我使用 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....' );
}
}
}
Now i want code working for multiple products like :现在我希望代码适用于多种产品,例如:
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....' );
}
}
}
Any help with this to apply for multiple products please.请在这方面提供任何帮助以申请多种产品。
The easiest way to achieve this with as little modification to your working code as possible would be to use in_array()
.在尽可能少地修改工作代码的情况下实现此目的的最简单方法是使用in_array()
。
Also, please note the use of exit;
另外,请注意exit;
after wp_redirect
.在wp_redirect
之后。 You can see why I added it on this post and in the docs:你可以看到我为什么在这篇文章和文档中添加它:
Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit;注意: wp_redirect() 不会自动退出,并且应该几乎总是在调用退出之后进行;
Furthermore, if you want to make sure that you always redirect to a page on an allowed host, you should have a look at wp_safe_redirect
and replace your wp_redirect
calls此外,如果您想确保始终重定向到允许主机上的页面,您应该查看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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.