2017-09-14 92 views
1

在WooCommerce中,我可以使用“WPB WooCommerce相关产品滑块”和“WooCommerce自定义相关产品”第三方插件。如果没有相关产品,请移除WooCommerce自定义相关产品选项卡

下面我增加了自定义选项卡以显示相关的产品代码:

remove_action('woocommerce_after_single_product_summary', 'wpb_wrps_related_products',22); 
add_filter('woocommerce_product_tabs', 'wpb_wrps_adding_related_products_slider_to_product_tab'); 
if(!function_exists('wpb_wrps_adding_related_products_slider_to_product_tab')){ 
    function wpb_wrps_adding_related_products_slider_to_product_tab($tabs) { 
     $tabs['wpb_wrps_related_products_slider'] = array(
      'title'  => __('Related Products','wpb-wrps'), 
      'priority' => 30, 
      'callback' => 'wpb_wrps_related_products' 
     ); 
     return $tabs; 
    } 
} 

由于我的一些产品没有相关产品,如何才能让刚刚当有相关显示此选项卡产品?

回答

0

以下是获取当前产品相关产品数量的方法。有了这些信息,我们可以有条件地让你的自定义选项卡显示或不基于该计数:

if(!function_exists('wpb_wrps_adding_related_products_slider_to_product_tab')){ 
    add_filter('woocommerce_product_tabs', 'wpb_wrps_adding_related_products_slider_to_product_tab'); 
    function wpb_wrps_adding_related_products_slider_to_product_tab($tabs) { 
     global $product; 
     // Get the related products count 
     $related_count = count(maybe_unserialize(get_option('_transient_wc_related_'.$product->get_id()))); 
     // If no related products we exit 
     if(empty($related_count) || $related_count == 0) return $tabs; 

     $tabs['wpb_wrps_related_products_slider'] = array(
      'title'  => __('Related Products','wpb-wrps'), 
      'priority' => 30, 
      'callback' => 'wpb_wrps_related_products' 
     ); 
     return $tabs; 
    } 
    // Just for testing 
    function wpb_wrps_related_products() { 
     echo '<h3>HERE your custom related products loop (fake)</h3>'; 
    } 
} 

代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。

此代码对Woocommerce 3+测试和工程

+0

你好LoicTheAztec。我有一个错误:致命错误:无法重新声明wpb_wrps_related_products()(以前在/../../public_html/wp-content/plugins/wpb-woocommerce-related-products-slider/inc/wpb-wrps-functions中声明。 php:20)在/../../public_html/wp-content/themes/clean-commerce/functions.php在线49 – arz

+0

我删除了函数wpb_wrps_related_products(),现在我没有致命错误,但输出的相关产品不好 – arz

+0

@arz这段代码经过测试,完美工作......现在您需要的是在我的功能中加入对您有用的东西:重要的缺失是将相关产品计数代码用于条件函数,在你的代码中......所以你应该接受这个答案。这是我可以用所提供的代码做的唯一的事情......您应该添加完整的代码为'wpb_wrps_related_products()'丢失... – LoicTheAztec

相关问题