简体   繁体   English

WooCommerce 结帐付款后的自定义条件重定向

[英]Custom conditional redirections after WooCommerce checkout payment

I use Lirox One theme on Wordpress with WooCommerce.我在 WooCommerce 的 Wordpress 上使用 Lirox One 主题。 I want make custom redirections after payment:我想在付款后进行自定义重定向:
If a customer buy product ID 333, It will be redirected to product 444 (for example).如果客户购买产品 ID 333,它将被重定向到产品 444(例如)。

I have make some custom code but it doesn't works, I get an error 500 (and debug is empty).我制作了一些自定义代码但它不起作用,我收到错误 500(并且调试为空)。

What I am doing wrong and how can I make it work?我做错了什么,我怎样才能让它发挥作用?

This is my code:这是我的代码:

add_action( 'woocommerce_thankyou', 'check_order_product_id', 1 );
function check_order_product_id( $order_id ){
$order = new WC_Order( $order_id );
    $items = $order->get_items();
    foreach ( $items as $item ) {
       $product_id = $item['product_id'];

          //* single product id
          if ( $product_id == 399 ) {
            // Content Title line
            $url = 'http://yoursite.com/custom-url1';
            }
          if ( $product_id == 358 ) {
            $url = 'http://yoursite.com/custom-url2';
          }
          if ( $product_id == 398 ) {
            $url = 'http://yoursite.com/custom-url3';
          }

          if ( $product_id == 357) {
            $url = 'http://yoursite.com/custom-url5';
          }

          if ( $product_id == 356) {
            $url = 'http://yoursite.com/custom-url6';
          }

          if ( $product_id == 335) {
            $url = 'http://yoursite.com/custom-url';
          }
          if ( $order->status != 'failed' ) {
    wp_redirect($url);
    exit;
}

For redirections, use a custom function hooked in WordPress template_redirect action hook,对于重定向,请使用 WordPress template_redirect操作挂钩中挂钩的自定义函数,
to avoid errors 500…避免错误 500…

I have changed your code as you will see, to match your requirements.如您所见,我已经更改了您的代码,以满足您的要求。 This code is made for WooCommerce version 3+:此代码适用于 WooCommerce 版本 3+:

add_action( 'template_redirect', 'conditional_redirection_after_payment');
function conditional_redirection_after_payment(){
    // When "thankyou" order-received page is reached …
    if ( is_wc_endpoint_url( 'order-received' ) ) {
        global $wp;

        // Get the order ID from the browser url
        $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );

        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        // If the order status is 'failed' we stop the function
        if( $order->has_status( 'failed' ) ) return;

        // HERE set in the array for each product ID the coresponding url final path
        $product_id_url_paths = array(
            '399' => '/custom-url1',
            '358' => '/custom-url2',
            '398' => '/custom-url3',
            '357' => '/custom-url4',
            '356' => '/custom-url5',
            '335' => '/custom-url6',
        );

        // Iterating through each order items
        foreach( $order->get_items() as $item_id => $item_values ){
            // The Product ID
            $product_id = $item_values->get_product_id();

            foreach( $product_id_url_paths as $key_id => $url_path ){
                if( $key_id == $product_id ){
                    // Product is found and ID match: we got our path url. We redirect
                    wp_redirect( home_url( $url_path ) );
                    exit(); // always exit
                }
            }
        }
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

Code is tested and works on WC 3+代码已经过测试并适用于 WC 3+

Note: In your Code you have to remove the exit which you have used after the wp_redirect .注:在你的代码,你需要删除的exit ,你已经在使用后wp_redirect Other all will work fine.其他一切都会正常工作。 Some more References are provided below in order to have better understanding of the Concept.下面提供了更多参考资料,以便更好地理解该概念。

Explanation : Then you Provide Redirect only for the Particular Product ID alone not For all the Product IDS since it may lead you to the Internal Server Error as it is in the Foreach Loop.说明:然后您只为特定产品 ID 提供重定向,而不是为所有产品 IDS 提供重定向,因为它可能会导致您在 Foreach 循环中出现内部服务器错误。

It is Fine using wp_redirect() for the redirection purpose.wp_redirect()用于重定向目的很好。

It is correct what have you checked for the Product ID.您检查产品 ID 的内容是正确的。 The Thing is that you have to follow another method if the URL are to be explicitly passed.事情是,如果要显式传递 URL,您必须遵循另一种方法。

// We don't know for sure whether this is a URL for this site,
// so we use wp_safe_redirect() to avoid an open redirect.
wp_safe_redirect( $url );

// We are trying to redirect to another site, using a hard-coded URL.
wp_redirect( 'https://example.com/some/page' );

Some More Reference:更多参考:

<?php wp_redirect( home_url() ); exit; ?>

Redirects can also be external, and/or use a “Moved Permanently” code :

<?php wp_redirect( 'http://www.example-one.com', 301 ); exit; ?>

The code below redirects to the parent post URL which can be used to redirect attachment pages back to the parent.

<?php wp_redirect( get_permalink( $post->post_parent ) ); exit; ?>

Reference: https://developer.wordpress.org/reference/functions/wp_redirect/参考: https : //developer.wordpress.org/reference/functions/wp_redirect/

Th accepted answer works for Woocommerce 3+, for older versions (I'm using 2.6.4) it does not.接受的答案适用于 Woocommerce 3+,但对于旧版本(我使用的是 2.6.4)则无效。 For anyone else using this version, try the following code:对于使用此版本的其他人,请尝试以下代码:

add_action( 'template_redirect', 'conditional_redirection_after_payment');
function conditional_redirection_after_payment(){
    // When "thankyou" order-received page is reached …
    if ( is_wc_endpoint_url( 'order-received' ) ) {
        global $wp;

    $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
    // Get an instance of the WC_Order object
    $order = new WC_Order( $order_id );

    // If the order status is 'failed' we stop the function
    if( $order->has_status( 'failed' ) ) return;

    // HERE set in the array for each product ID the coresponding url final path
   $product_id_url_paths = array(
        '1234' => '/your-path/'
    );
    // Iterating through each order items
    foreach( $order->get_items() as $item){
       // echo $item_id;
        // The Product ID
        $product_id = $item['product_id'];

        foreach( $product_id_url_paths as $key_id => $url_path ){
            if( $key_id == $product_id ){
                // Product is found and ID match: we got our path url. We redirect
                wp_redirect( home_url( $url_path ) );
                exit(); // always exit
            }
        }
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM