首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WooCommerce结账后的自定义条件重定向

WooCommerce结账后的自定义条件重定向
EN

Stack Overflow用户
提问于 2017-08-07 12:05:02
回答 3查看 3.7K关注 0票数 1

我使用Lirox的一个主题在Wordpress与WooCommerce。我想在付款后进行定制的重定向:

如果客户购买产品ID 333,它将被重定向到产品444 (例如)。

我已经做了一些自定义代码,但是它不起作用,我得到了一个错误500 (调试是空的)。

我做错了什么,怎么才能让它起作用?

这是我的密码:

代码语言:javascript
复制
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;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-08-07 16:09:23

对于重定向,使用自定义函数在template_redirect动作钩子中钩住,

为了避免错误,500…

如您所见,我已经更改了您的代码,以满足您的要求。此代码用于WooCommerce版本3+:

代码语言:javascript
复制
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
                }
            }
        }
    }
}

代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

代码是在WC 3+上测试和工作的。

票数 1
EN

Stack Overflow用户

发布于 2017-08-07 12:10:53

注意:在您的代码中,您必须删除在wp_redirect之后使用的exit。其他人都会很好的。为了更好地理解这一概念,下文提供了更多的参考资料。

解释:然后您只为特定的产品ID提供重定向,而不是为所有的Product提供重定向,因为它可能会导致内部服务器错误,就像Foreach循环中的情况一样。

使用wp_redirect()进行重定向是很好的。

您检查的产品ID是正确的。问题是,如果要显式传递URL,您必须遵循另一种方法。

代码语言:javascript
复制
// 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' );

更多参考:

代码语言:javascript
复制
<?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; ?>

参考: redirect/

票数 0
EN

Stack Overflow用户

发布于 2018-04-11 22:20:01

这个接受的答案适用于Woocommerce 3+,对于较早的版本(我使用的是2.6.4)它不适用。对于使用此版本的其他任何人,请尝试以下代码:

代码语言:javascript
复制
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
            }
        }
    }
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45546488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档