2014-04-24 18 views
3

,因爲WooCommerce 2.1頁面(例如接收訂單)已被刪除並替換爲WC端點。我的結帳頁面有一個自定義頁面模板(page-checkout.php),現在所有結帳端點也使用此自定義頁面模板。如何檢查我們是否不在Woocommerce端點

只有當我的客戶在/ checkout /頁面中時,我需要修改我的頁眉和頁腳,但是我想在結賬終端中顯示不同的內容。我發現這個條件:

if(is_wc_endpoint_url("order-received")) echo "yes"; 

它適用於我們處於「訂單接收」結帳終點時。但我找,告訴我一個條件邏輯,當我們不在一個端點,是這樣的:

if(!is_wc_endpoint()) echo "yes"; 

謝謝。

回答

1

嘗試以下功能:

function is_wc_endpoint() { 
    if (empty($_SERVER['REQUEST_URI'])) return false; 
    $url = parse_url($_SERVER['REQUEST_URI']); 
    if (empty($url['query'])) return false; 
    global $wpdb; 
    $all_woocommerce_endpoints = array(); 
    $results = $wpdb->get_results("SELECT option_name, option_value FROM {$wpdb->prefix}options WHERE option_name LIKE 'woocommerce_%_endpoint'", 'ARRAY_A'); 
    foreach ($results as $result) { 
     $all_woocommerce_endpoints[$result['option_name']] = $result['option_value']; 
    } 
    foreach ($all_woocommerce_endpoints as $woocommerce_endpoint) { 
     if (strpos($url['query'], $woocommerce_endpoint) !== false) { 
      return true; 
     } 
    } 
    return false; 
} 

希望它會給你造成你期待。

+0

非常感謝,它的工作原理。但是我認爲它需要太多的查詢,不是嗎?不存在一個更簡單的條件? – retroriff

2

這是未來woocommerce版本中將包含的is​​_wc_endpoint_url函數的一個從未版本。所以給它一個不同的名字,例如放入你的functions.php中。

function is_wc_endpoint_url($endpoint = false) { 
global $wp; 

$wc_endpoints = WC()->query->get_query_vars(); 

if ($endpoint) { 
    if (! isset($wc_endpoints[ $endpoint ])) { 
     return false; 
    } else { 
     $endpoint_var = $wc_endpoints[ $endpoint ]; 
    } 

    return isset($wp->query_vars[ $endpoint_var ]); 
} else { 
    foreach ($wc_endpoints as $key => $value) { 
     if (isset($wp->query_vars[ $key ])) { 
      return true; 
     } 
    } 
    return false; 
} 
} 
+0

該功能已經在工作。我正在使用它: if(is_wc_endpoint_url(「order-received」))$ thanks-page = 1; – retroriff

4

這個問題似乎回答了一下,有點老了。但是我找到了一個更好的解決方案,可以幫助其他人。

您可以使用以下功能。 Documentation

is_wc_endpoint_url() 

如果在沒有參數的情況下使用,它將檢查所有端點的當前url。如果端點被指定爲

is_wc_endpoint_url('edit-account'); 

它將檢查URL是否是特定端點。

相關問題