2016-12-06 70 views
1

我在添加「相關產品」到選項卡並使其適用於使用短代碼的帖子時遇到問題。這裏是一個的被放置在我的functions.php將「相關產品」添加到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); 
} 
/* 
* Place content in custom tab (related products in this sample) 
*/ 
function woo_custom_product_tab_content() { 
woocommerce_related_products(); 
} 
add_filter('woocommerce_product_tabs', 'woo_custom_product_tab'); 

下面是我收到的錯誤:

Fatal error: Call to a member function get_upsells() on a non-object in public_html/wp-content/plugins/woocommerce/templates/single-product/up-sells.php on line 25

回答

2

我認爲你需要使用全局$產品對象與WC_Product get_related() method避免這種錯誤...

然後將溶液可能是:

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; 
    $product->get_related(); 
} 
add_filter('woocommerce_product_tabs', 'woo_custom_product_tab'); 

由於這是未經測試,我不保證什麼...

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

+0

這工作!然而,這只是與短代碼一起工作,但不適用於woocommerce頁面......任何解決方案? – SlyMurphy

相關問題