2017-08-07 122 views
1

我在WooCommerce上使用Wordpress上的Lirox One主題。我想在付款後進行自定義重定向:
如果客戶購買產品ID 333,它將被重定向到產品444(例如)。WooCommerce結帳付款後自定義條件重定向

我已經做了一些自定義代碼,但它不起作用,我得到一個錯誤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; 
} 
+0

不要的理由'foreach'loop ...循環的第一次運行之後,你會重定向無論如何,所以循環贏得」有史以來第二次執行... – Twinfriends

回答

0

對於重定向,使用WordPress的鉤自定義函數template_redirect行動掛鉤,
,以避免錯誤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+

0

注:在你的代碼必須刪除您擁有wp_redirect之後使用的exit。其他一切都會正常工作。爲了更好地理解概念,下面提供了更多參考資料。

說明:那你只對特定產品 ID提供重定向不能單獨對所有的產品ID,因爲它可能會導致您的 內部服務器錯誤,因爲它是在foreach循環中。

很好,使用wp_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; ?> 

參考:https://developer.wordpress.org/reference/functions/wp_redirect/

+0

我刪除了退出,但500仍然存在 – Polo195pl

+0

這意味着您的訂單狀態可能不會在那裏收到。請確認這個 –

+0

我在整個網站上有500個:D – Polo195pl