Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 电子商务结帐付款后的自定义条件重定向_Php_Wordpress_Woocommerce_Product_Orders - Fatal编程技术网

Php 电子商务结帐付款后的自定义条件重定向

Php 电子商务结帐付款后的自定义条件重定向,php,wordpress,woocommerce,product,orders,Php,Wordpress,Woocommerce,Product,Orders,我在WooCommerce的Wordpress上使用Lirox One主题。我想在付款后进行自定义重定向: 如果客户购买产品ID 333,它将被重定向到产品444(例如) 我已经做了一些自定义代码,但它不工作,我得到一个错误500(调试是空的) 我做错了什么,我怎样才能让它工作 这是我的代码: add_action( 'woocommerce_thankyou', 'check_order_product_id', 1 ); function check_order_product_id( $o

我在WooCommerce的Wordpress上使用Lirox One主题。我想在付款后进行自定义重定向:
如果客户购买产品ID 333,它将被重定向到产品444(例如)

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

我做错了什么,我怎样才能让它工作

这是我的代码:

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;
}

注意:在您的代码中,您必须删除在wp\u重定向后使用的exit。其他的都可以。为了更好地理解这一概念,下文提供了更多的参考资料

解释:然后您只为特定产品提供重定向 ID本身并不是所有产品ID的唯一标识,因为它可能会引导您找到 Foreach循环中的内部服务器错误

可以使用
wp\u redirect()
进行重定向

您为产品ID检查的内容是正确的。问题是,如果要显式传递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' );
更多参考资料:

<?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; ?>

重定向也可以是外部的,和/或使用“永久移动”代码:
下面的代码重定向到父帖子URL,该URL可用于将附件页重定向回父帖子。

参考:

对于重定向,请使用WordPress
模板中挂钩的自定义函数\u重定向
操作挂钩,
以避免错误500

正如您将看到的,我已经更改了您的代码,以符合您的要求。此代码适用于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
                }
            }
        }
    }
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中


代码经过测试并在WC 3+上运行

接受的答案适用于Woocommerce 3+,而旧版本(我使用的是2.6.4)则不适用。对于其他使用此版本的用户,请尝试以下代码:

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
            }
        }
    }
}

}

不要获取
foreach
循环的原因。。。在循环的第一次运行之后,您将重定向,这样循环就不会再执行第二次…我删除了退出,但它仍然存在,这意味着您的订单状态可能不会在那里收到。请确认我在整个网站上有500个:d删除你添加的代码并检查网站没有此代码正常,我没有任何错误,日志调试日志为空