2017-10-18 191 views
0

如何根據他們購買的產品將WooCommerce客戶重定向到特定的感謝頁面?我有一種產品需要填寫表格才能獲得更多信息,我希望將該表格放在感謝頁面上。我到目前爲止的代碼如下,但這僅僅是爲所有產品的通用感謝頁面。WooCommerce基於產品ID的自定義Thankyou重定向

add_action('template_redirect', 'wc_custom_redirect_after_purchase'); 

function wc_custom_redirect_after_purchase() { 
    global $wp; 

    if (is_checkout() && ! empty($wp->query_vars['order-received'])) { 
     wp_redirect('http://www.yoururl.com/your-page/'); 
     exit; 
    } 
} 
+0

您將需要一個元屬性來保存您要在購買後重定向到的自定義頁面的ID – madalinivascu

+0

什麼對您的代碼不起作用?你有什麼要求。就目前而言,我認爲這個問題過於寬泛。 –

回答

-1

下面是一個簡單的頁面重定向的例子:

add_action('template_redirect', 'wc_custom_redirect_after_purchase'); 

function bbloomer_redirectcustom($order_id){ 
$order = new WC_Order($order_id); 

$url = 'http://yoursite.com/custom-url'; 

if ($order->status != 'failed') { 
    wp_redirect($url); 
    exit; 
    } 
} 
0

低於該功能,您將不得不設置你的目標產品標識或產品類別,獲得這個項目時,自定義重定向它們的順序:

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

    // Define the product IDs in this array 
    $product_ids = array(37, 25, 50); // or an empty array if not used 
    // Define the product categories (can be IDs, slugs or names) 
    $product_categories = array('clothing'); // or an empty array if not used 
    $redirection = false; 

    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 

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

    // Make the custom redirection when a targeted product has been found in the order 
    if($redirection){ 
     wp_redirect(home_url('/your-page/')); 
     exit; 
    } 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

在WooCommerce 3上測試並工作。

相關問題