2016-07-27 51 views
4

我有一個選項卡,用於添加包含WooCommerce中的規格的選項卡。我想將它包裝到if語句中,以便只在產品是某個類別的一部分時才設置選項卡。如何設置選項卡(僅)如果產品處於特定類別WooCommerce

add_filter('woocommerce_product_tabs', 'woo_custom_product_tabs'); 

function woo_custom_product_tabs($tabs) { 

    global $post; 

    if ($product->is_category("Mobile Phones")) { 
     $tabs['custom_specification'] = array('title' => __('Specification', 'woocommerce'), 'priority' => 50, 'callback' => 'woo_custom_specification_content'); 
    } 
} 

什麼是檢查WooCommerce類別的if語句括號正確的代碼?

+0

我沒有忘記我只是不添加它以節省空間我解決了這個問題,大概十分鐘後,我張貼的問題將上傳答案 – Scott

+0

是你的就是那種相同的,我的結論,除了我用'''if($ product-> is_type('grouped')&& has_term('SIM Cards','product_cat')|| has_term('SIM Cards','product_cat'))''' – Scott

+0

顯然在聲明瞭''' ''''''''' – Scott

回答

3

如果您處於類別存檔頁面,條件is_category()將返回true。

,因爲你需要一個條件的單品頁,你將針對與is_product()條件相結合這樣的單品頁:

if (is_product() && has_term('Mobile Phones', 'product_cat')) { 
    $tabs['custom_specification'] = array('title' => __('Specification', 'woocommerce'), 'priority' => 50, 'callback' => 'woo_custom_specification_content'); 
} 

或者你可以嘗試同樣,在情況下,這一個了:

if(is_product() && has_category('Mobile Phones')) { 
    $tabs['custom_specification'] = array('title' => __('Specification', 'woocommerce'), 'priority' => 50, 'callback' => 'woo_custom_specification_content'); 
} 

@edit:你已經錯過了return $tabs;在最後一個右括號前的功能結束}


參考文獻:

2

試試下面的代碼。此代碼僅在產品有手機類別時添加woocommerce選項卡。

add_filter('woocommerce_product_tabs', 'woo_custom_product_tabs'); 

    function woo_custom_product_tabs($tabs) { 

     global $post; 
    if (is_product() && has_term('Mobile Phones', 'product_cat')) 
    { 
     $tabs['custom_specification'] = array('title' => __('Specification', 'woocommerce'), 'priority' => 50, 'callback' => 'woo_custom_specification_content'); 
     } 
     return $tabs; 
    } 
+0

很好,但你只是複製我的答案! – LoicTheAztec

+0

我更正了問題代碼並添加了問題的解決方案。返回$ tabs;在問題代碼中缺少行。 – pallavi

+0

是的,但你只是複製我的條件...所以避免這...謝謝。 Op問題與條件有關。 'return $ tabs;'只是OP的一個疏忽。您可以在OP問題中添加註釋... – LoicTheAztec

相關問題