Php WooCommerce在专门针对特定产品签出后进行自定义重定向

Php WooCommerce在专门针对特定产品签出后进行自定义重定向,php,wordpress,redirect,woocommerce,product,Php,Wordpress,Redirect,Woocommerce,Product,我想在Woocommerce中的单个产品签出后重定向到自定义URL,让所有其他产品继续重定向到默认的感谢/订单摘要页面 我试着在这篇文章的第一个答案中使用代码片段: 我将此代码段中作为示例列出的产品ID替换为要在签出后生成重定向的产品的单个产品ID,将类别数组留空,并更新URL。当我测试该特定产品的签出重定向时,但当我测试其他产品时,他们也在重定向,而不是转到默认的感谢/订单确认页面 我知道这篇帖子有点旧,但上面说代码是在WooCommerce 3上测试的。是否有什么地方我遗漏了,或者我应该换

我想在Woocommerce中的单个产品签出后重定向到自定义URL,让所有其他产品继续重定向到默认的感谢/订单摘要页面

我试着在这篇文章的第一个答案中使用代码片段:

我将此代码段中作为示例列出的产品ID替换为要在签出后生成重定向的产品的单个产品ID,将类别数组留空,并更新URL。当我测试该特定产品的签出重定向时,但当我测试其他产品时,他们也在重定向,而不是转到默认的感谢/订单确认页面


我知道这篇帖子有点旧,但上面说代码是在WooCommerce 3上测试的。是否有什么地方我遗漏了,或者我应该换一条路线而不是这个片段?

当您说您将数组留空时,最好删除该数组,因为它将为每个产品返回true

也就是说,
has_term()
正在询问,该产品是否有一个术语/(类别)?在Woocommerce中,答案几乎总是正确的,因为这些产品位于商店类别中

因此,当您在下面的答案中看到原始代码时,它是针对您在$product_categories变量中设置的术语进行测试的

// Iterating through order items and finding targeted products
foreach( $order->get_items() as $item ){
    if( in_array( $item->get_product_id(), $product_ids ) || has_term( $product_categories, 'product_cat', $item->get_product_id() ) ) {
        $redirection = true;
        break;
    }
}
解决方案是,完全去掉has_术语,并用以下内容替换上述部分:

// Iterating through order items and finding targeted products
foreach( $order->get_items() as $item ){
    if( in_array( $item->get_product_id(), $product_ids ) ) {
        $redirection = true;
        break;
    }
}

若要在签出后启用重定向,当订单中只有目标产品是独占的时,请改用以下重新访问的代码:

add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
    if ( ! is_wc_endpoint_url( 'order-received' ) ) 
        return;

    $targeted_ids = array( 37, 25, 50 ); // <== Targeted product IDs in this array

    global $wp;
    
    $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) ); // Order ID
    $order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object
    $targeted_found = $others_found = false; // Initializing

    // Iterating through order items and finding targeted products
    foreach( $order->get_items() as $item ){
        if( in_array( $item->get_product_id(), $targeted_ids ) ) {
            $targeted_found = true;
        } else {
            $others_found = true;
        }
    }

    // Enable redirection when only targeted product(s) are exclusively in the order
    if( $targeted_found && ! $others_found ){
        wp_redirect( home_url( '/your-page/' ) );
        exit;
    }
}
add_action('template_redirect'、'wc_custom_redirect_after_purchase');
函数wc_custom_redirect_在购买后(){
如果(!is_wc_endpoint_url('order received'))
返回;
$targeted_ID=array(37,25,50);//request();//订单ID
$order=wc\u get\u order($order\u id);//获取wc\u order对象的实例
$targeted\u found=$others\u found=false;//正在初始化
//遍历订单项并查找目标产品
foreach($order->get_items()作为$item){
if(在数组中($item->get_product_id(),$targeted_id)){
$targeted_found=true;
}否则{
$others\u found=true;
}
}
//在订单中仅包含目标产品时启用重定向
如果($targeted_found&!$others_found){
wp_重定向(home_url('/your page/');
出口
}
}

代码进入活动子主题(或活动主题)的functions.php文件。经过测试,可以正常工作。

代码仍然可以很好地工作,但它没有处理独占重定向,这就是为什么在混合项上会得到重定向。请看下面的答案。如果您对下面的答案有任何反馈,我们将不胜感激。