简体   繁体   English

基于付款后购买的多个产品的 Woocommerce 自定义重定向

[英]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.

相关问题 Woocommerce 付款后基于购买产品的自定义重定向 - Woocommerce custom redirection based on purchased product after payment 基于地理位置的WooCommerce中特定产品的自定义重定向 - Custom Redirection based on Geolocation for specific products in WooCommerce Woocommerce 自定义支付网关重定向 - Woocommerce custom payment gateway redirection 基于地理位置的WooCommerce中多个国家/地区的自定义重定向 - Custom Redirection based on Geolocation for multiple countries in WooCommerce WooCommerce 自定义结帐后重定向,专为特定产品 - WooCommerce custom redirection after checkout exclusively for specific products WooCommerce 将多个产品的自定义重定向添加到购物车 - WooCommerce Add to Cart custom redirection for multiple products Variations 结帐后基于WooCommerce中的自定义结帐字段进行自定义重定向 - Custom redirection after checkout based on a custom checkout field in WooCommerce 使用重定向的Woocommerce自定义支付网关工作流程 - Woocommerce custom payment gateway workflow using redirection WooCommerce 根据特定产品的购买数量收取额外费用 - WooCommerce additional fees based on purchased quantity for specific products 付款后将产品添加到订单中(WooCommerce/Wordpress) - Add products to order after payment (WooCommerce/Wordpress)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM