首页 > 解决方案 > 将所有购买重定向到自定义感谢页面 - 特定页面 ID 除外

问题描述

我有两个相互干扰的脚本。我对这个网站需要的是:所有成功的购买都应该转到页面https://aefcoaching.com/gracias-por-tu-compra除了成功购买产品 ID1813应该转到https://aefcoaching.com /gracias-gimnasio-mental/

/* @ Redirect to the Thank You page after successful payment in WooCommerce */
if( !function_exists('sc_custom_redirect_after_purchase') ):
 
function sc_custom_redirect_after_purchase() {
 
  global $wp;
 
      if ( is_checkout() && !empty($wp->query_vars['order-received']) ) :
 
        $order_id = absint($wp->query_vars['order-received']);
 
        $order_key = wc_clean($_GET['key']);
 
        $th_page_url = 'https://aefcoaching.com/gracias-por-tu-compra';
 
        $redirect = add_query_arg(
                    array(
                          'order' => $order_id,
                          'key' => $order_key,
                      ), $th_page_url);
 
          wp_safe_redirect($redirect);
          exit;
 
    endif;
}
add_action('template_redirect', 'sc_custom_redirect_after_purchase');
 
endif;



/* @ Redirect Gimnasio Mental purchases to a specific Thank You Page */

function action_woocommerce_thankyou( $order_id ) {
    if( ! $order_id ) {
        return;
    }

    // Instannce of the WC_Order Object
    $order = wc_get_order( $order_id ); 

    // Is a WC_Order
    if ( is_a( $order, 'WC_Order' ) ) {
        // False
        $redirection = false;
        
        // Loop through order items
        foreach ( $order->get_items() as $item_key => $item ) {
            // Product ID(s)
            $product_ids = array( $item->get_product_id(), $item->get_variation_id() );
            
            // Product ID in array
            if ( in_array( 1813, $product_ids ) ) {
                $redirection = true;
            }
        }
    }

    // Make the custom redirection when a targeted product has been found in the order
    if ( $redirection ) {
        wp_safe_redirect( home_url( '/gracias-gimnasio-mental/' ) );
        exit;
    }
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 );

第一段效果很好。第二个不是因为第一个。我不确定如何整合这两个而不是有两个单独的代码。你能帮助我吗?

标签: phpwordpresswoocommerce

解决方案


请通过将 sc_custom_redirect_after_purchase 函数更改为以下内容,将这两个逻辑合并为一个:

function sc_custom_redirect_after_purchase() {
 
  global $wp;
 
      if ( is_checkout() && !empty($wp->query_vars['order-received']) ) :
 
        $order_id = absint($wp->query_vars['order-received']);
 
        $order_key = wc_clean($_GET['key']);
 
 $th_page_url = 'https://aefcoaching.com/gracias-por-tu-compra'; 
 
 //////////////////////
 $order = wc_get_order( $order_id ); 
         foreach ( $order->get_items() as $item_key => $item ) {
            // Product ID(s)
            $product_ids = array( $item->get_product_id(), $item->get_variation_id() );
            
            // Product ID in array
            if ( in_array( 1813, $product_ids ) ) {
//                $redirection = true;
         $th_page_url = 'https://aefcoaching.com/gracias-gimnasio-mental/';

            }
        }
/////////////////////       
 
 
 
        $redirect = add_query_arg(
                    array(
                          'order' => $order_id,
                          'key' => $order_key,
                      ), $th_page_url);
 
          wp_safe_redirect($redirect);
          exit;
 
    endif;
}

推荐阅读