自定义感谢页面重定向基于WooCommerce产品变体

huangapple go评论67阅读模式
英文:

Custom Thank You page redirect based on product variation in WooCommerce

问题

I see your code, and it seems like you're trying to implement a custom redirect for specific product variations, but there are some issues in your code. Here's the translated code with some corrections:

add_action('woocommerce_thankyou', 'updated_redirectcustom');

function updated_redirectcustom($order_id) {
    $order = wc_get_order($order_id);
    $url = '/thank-you/';
    if (!$order->has_status('failed')) {
        wp_safe_redirect($url);
        exit;
    }

    if (!$order_id) {
        return;
    }

    if (is_a($order, 'WC_Order')) {
        // Initialize redirection flag as 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());

            // Check if specific product IDs are in the array
            if (in_array(12345, $product_ids) || in_array(123456, $product_ids)) {
                $redirection = true;
                break; // Exit the loop if a match is found
            }
        }

        if ($redirection) {
            wp_safe_redirect(home_url('/thank-you-2/'));
            exit;
        }
    }
}

I've corrected the code, specifically the in_array function parameters and added a break statement to exit the loop when a match is found. This should help you achieve the custom redirect for specific product variations.

英文:

I have a code that works great to redirect to a custom Thank You page after purchasing any product.

add_action( 'woocommerce_thankyou', 'updated_redirectcustom');
 
function updated_redirectcustom( $order_id ){
    $order = wc_get_order( $order_id );
    $url = '/thank-you/';
    if ( ! $order->has_status( 'failed' ) ) {
        wp_safe_redirect( $url );
        exit;
    }
}

I decided to implement a custom redirect to another page for a couple of specific product variations, but, unfortunately, the code that I changed does not work and all orders are still redirected to the /thank-you/ URL:

add_action( 'woocommerce_thankyou', 'updated_redirectcustom');
 
function updated_redirectcustom( $order_id ){
    $order = wc_get_order( $order_id );
    $url = '/thank-you/';
    if ( ! $order->has_status( 'failed' ) ) {
        wp_safe_redirect( $url );
        exit;
    }
	if( ! $order_id ) {
		return;
	}
	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( 12345, 123456, $product_ids ) ) {
				$redirection = true;
			}
		}
	}
	if ( $redirection ) {
		wp_safe_redirect( home_url( '/thank-you-2/' ) );
		exit;
	}
}

Where am I wrong?

答案1

得分: 1

array_intersect()替换以下代码行:

if ( in_array( 12345, 123456, $product_ids ) ) {

改成:

if ( count( array_intersect( array(12345, 123456), $product_ids ) ) > 0 ) {

现在它应该适用于所有产品类型,包括变体。

此外,为了缩短循环,您可以在$redirection = true;下面添加一个break;(可选)。

英文:

As finally you are comparing 2 arrays, use instead array_intersect(), replacing this code line:

if ( in_array( 12345, 123456, $product_ids ) ) {

with:

if ( count( array_intersect( array(12345, 123456), $product_ids ) ) > 0 ) {

Now it should work for all product types, including variations.

Also to shorten the loop, you should add a break; below $redirection = true; (optional).

答案2

得分: 0

你的代码问题在于对in_array()函数的使用。该函数期望第一个参数是要搜索的值,第二个参数是要在其中搜索的数组。然而,在你的代码中,你将搜索值作为第二个参数,并将两个整数作为第一个参数。以下是已更正的代码:

add_action('woocommerce_thankyou', 'updated_redirectcustom');

function updated_redirectcustom($order_id) {
    $order = wc_get_order($order_id);
    $url = '/thank-you/';

    if (!$order->has_status('failed')) {
        wp_safe_redirect($url);
        exit;
    }

    if (!$order_id) {
        return;
    }

    if (is_a($order, 'WC_Order')) {
        $redirection = false;

        foreach ($order->get_items() as $item_key => $item) {
            $product_ids = array($item->get_product_id(), $item->get_variation_id());

            if (in_array(12345, $product_ids) || in_array(123456, $product_ids)) {
                $redirection = true;
                break; // 如果找到匹配项,退出循环
            }
        }

        if ($redirection) {
            wp_safe_redirect(home_url('/thank-you-2/'));
            exit;
        }
    }
}

希望这对你有帮助。

英文:

The issue in your code lies in the usage of the in_array() function. The function expects the first parameter to be the search value, and the second parameter to be the array to search in. However, in your code, you have provided the search value as the second parameter and two integers as the first parameter. Here's the corrected code:

add_action( 'woocommerce_thankyou', 'updated_redirectcustom' );

function updated_redirectcustom( $order_id ) {
    $order = wc_get_order( $order_id );
    $url = '/thank-you/';

    if ( ! $order->has_status( 'failed' ) ) {
        wp_safe_redirect( $url );
        exit;
    }

    if ( ! $order_id ) {
        return;
    }

    if ( is_a( $order, 'WC_Order' ) ) {
        $redirection = false;

        foreach ( $order->get_items() as $item_key => $item ) {
            $product_ids = array( $item->get_product_id(), $item->get_variation_id() );

            if ( in_array( 12345, $product_ids ) || in_array( 123456, $product_ids ) ) {
                $redirection = true;
                break; // Exit the loop if a match is found
            }
        }

        if ( $redirection ) {
            wp_safe_redirect( home_url( '/thank-you-2/' ) );
            exit;
        }
    }
}

huangapple
  • 本文由 发表于 2023年6月29日 18:23:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580156.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定