2017-10-18 5 views
0

WooCommerceのお客様は、購入した製品に基づいて特定の感謝のページにどのようにリダイレクトできますか?私はさらに詳しい情報を得るためにフォームに記入する必要がある製品を1つ持っており、そのフォームを「ありがとう」ページに配置したいと考えています。私がこれまでに持っているコードは以下の通りですが、それはすべての製品の一般的な感謝のページです。ここでWooCommerce Custom ThankyouリダイレクトプロダクトID

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を保存するにはmetaプロパティが必要です – 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

あなたはときに、この項目のためのカスタムリダイレクトを取得するには、あなたの目標と製品IDまたは製品カテゴリを設定する必要があります彼らは順序である:

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で動作確認済みです。

関連する問題