2016-12-07 75 views
2

在WooCommerce中,我有以下代碼用於將「相關產品」添加到自定義選項卡並使其適用於短代碼使用的帖子[product_page id="99"]在頁面和帖子上。在常規單一產品頁面的自定義選項卡中顯示「相關產品」

我在functions.php中使用的代碼工作:

remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20); 
/* 
* Register custom tab 
*/ 
function woo_custom_product_tab($tabs) { 

    $custom_tab = array( 
     'custom_tab' => array( 
      'title' => __('Custom Tab','woocommerce'), 
      'priority' => 9, 
      'callback' => 'woo_custom_product_tab_content' 
     ) 
    ); 
    return array_merge($custom_tab, $tabs); 
} 
add_filter('woocommerce_product_tabs', 'woo_custom_product_tab'); 

/* 
* Place content in custom tab (related products in this sample) 
*/ 
function woo_custom_product_tab_content() { 
    global $product; 
    $product->get_related(); 
} 

現在的問題是,我得到了正常的產品單頁空白標籤。

我怎樣才能使它在普通產品單頁上工作呢?

感謝

回答

1

爲了使它也行你需要添加一些條件和重用woocommerce_related_products()只是單一產品單頁產品頁面。

所以,你的代碼將是現在:

remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20); 
/* 
* Register custom tab 
*/ 
function woo_custom_product_tab($tabs) { 

    $custom_tab = array( 
     'custom_tab' => array( 
      'title' => __('Custom Tab','woocommerce'), 
      'priority' => 9, 
      'callback' => 'woo_custom_product_tab_content' 
     ) 
    ); 
    return array_merge($custom_tab, $tabs); 
} 

/* 
* Place content in custom tab (related products in this sample) 
*/ 
function woo_custom_product_tab_content() { 
    global $product; 
    if(!is_product()) 
     $product->get_related(); 
    else 
     woocommerce_related_products(); 
} 

這應該適用於網頁上的產品簡碼,並張貼在一側,還對單woocommerce正常的產品頁面。

代碼發送到您活動的子主題(或主題)的function.php文件中。或者也可以在任何插件php文件中使用。

+0

謝謝你,感謝你的所有WooCommerce知識!你在各種問題上都非常有幫助! – SlyMurphy

相關問題