Php 在Woocommerce中,有没有更好的方法来编写条件自定义产品选项卡?

Php 在Woocommerce中,有没有更好的方法来编写条件自定义产品选项卡?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我已经为Woocommerce中的单个产品页面添加了一个自定义产品选项卡 我希望自定义选项卡仅在有内容时才有条件地显示。为此,我使用以下代码: // Set a global variable for the custom tab content to pass it to the callback function that displays the tab content. $features_tab_content = ''; function woo_new_product_tab($t

我已经为Woocommerce中的单个产品页面添加了一个自定义产品选项卡

我希望自定义选项卡仅在有内容时才有条件地显示。为此,我使用以下代码:

// Set a global variable for the custom tab content to pass it to the callback function that displays the tab content.
$features_tab_content = '';
function woo_new_product_tab($tabs) {
    global $post, $features_tab_content;
    $custom_fields = array(
        'field_one'=>'Field One', 
        'field_two'=>'Field Two'
    );
    $fields_to_display = array();
    foreach ($custom_fields as $fieldname=>$fieldtitle) {
            $$val = get_post_meta( $post->ID, $fieldname, true );
            if ($$val != '') {
                $fields_to_display[$fieldtitle] = $$val;
            }
    }
    if (count($fields_to_display) > 0) {
        $features_tab_content = '<h2>Product Features</h2><table class="shop_attributes"><tbody>';
        foreach ($fields_to_display as $fieldtitle=>$val) {
            $features_tab_content .= '<tr><th>' . $fieldtitle . '</th><td>' . $val . '</td></tr>';
        }
        $features_tab_content .= '</tbody></table>';
        $tabs['features'] = array(
                'title' => __( 'Features', 'woocommerce' ),
                'priority' => 20,
                'callback' => 'woo_new_product_tab_content'
            );
    }
    return $tabs;
}
function woo_new_product_tab_content() {
    global $features_tab_content;
    echo $features_tab_content;
}
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
//为自定义选项卡内容设置一个全局变量,以将其传递给显示选项卡内容的回调函数。
$features\u tab\u content='';
功能woo_新产品_选项卡($tabs){
全局$post、$features\u选项卡\u内容;
$custom_fields=数组(
'field_one'=>'field one',
“字段二”=>“字段二”
);
$fields_to_display=array();
foreach($fieldname=>$fieldtitle形式的自定义字段){
$$val=get\u post\u meta($post->ID,$fieldname,true);
如果($$val!=''){
$fields_to_display[$fieldtitle]=$$val;
}
}
如果(计数($fields\u to\u display)>0){
$features_tab_content='productfeatures';
foreach($fields_至_显示为$fieldtitle=>$val){
$features_tab_content.='.$fieldtitle.'.$val'.';
}
$features\u tab\u content.='';
$tabs['features']=数组(
'title'=>uuu('Features','woocommerce'),
“优先级”=>20,
“回调”=>“求购新产品”选项卡“内容”
);
}
返回$tabs;
}
功能woo_新产品_选项卡_内容(){
全局$features\u选项卡\u内容;
echo$features\u tab\u内容;
}
添加过滤器(“woocommerce\u product\u tab”、“woo\u new\u product\u tab”);
代码在
woo_new_product_tab
函数中测试是否存在自定义选项卡的内容,并使用名为
$features_tab_content
的变量来保存实际内容

问题是,我不知道如何将内容传递给实际显示内容的回调函数,因此我在函数中设置了
$features\u tab\u content
变量为全局变量,并在回调函数
woo\u new\u product\u tab\u content
中使用它


这看起来有点笨拙,但比测试两个函数的内容要好。我不想使用插件来实现这一点,但我想知道是否有更干净的编码方法。

您可以在筛选$tabs时检查是否存在您计划显示的元:

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
   global $post;

   if( get_post_meta( $post->ID, 'field_one', true ) || get_post_meta( $post->ID, 'field_two', true ) ){
    // Adds the new tab

    $tabs['features'] = array(
        'title'     => __( 'Features', 'stackoverflow' ),
        'priority'  => 20,
        'callback'  => 'woo_new_product_tab_content'
    );
   }
    return $tabs;

}
不带全局变量的回调:

function woo_new_product_tab_content() {
    global $post;

    $custom_fields = array(
            'field_one'=>'Field One', 
            'field_two'=>'Field Two'
    );

    $fields_to_display = array();

    foreach ($custom_fields as $fieldname=>$fieldtitle) {
        $$val = get_post_meta( $post->ID, $fieldname, true );
        if ($$val != '') {
            $fields_to_display[$fieldtitle] = $$val;
        }
    }

    // The new tab content
    if (count($fields_to_display) > 0) {
        echo '<h2>Product Features</h2><table class="shop_attributes"><tbody>';

        foreach ( $fields_to_display as $fieldtitle => $val ) {
            echo '<tr><th>' . $fieldtitle . '</th><td>' . $val . '</td></tr>';
        }

        echo '</tbody></table>';

    }

}
function-woo\u-new\u-product\u-tab\u-content(){
全球$员额;
$custom_fields=数组(
'field_one'=>'field one',
“字段二”=>“字段二”
);
$fields_to_display=array();
foreach($fieldname=>$fieldtitle形式的自定义字段){
$$val=get\u post\u meta($post->ID,$fieldname,true);
如果($$val!=''){
$fields_to_display[$fieldtitle]=$$val;
}
}
//新的标签内容
如果(计数($fields\u to\u display)>0){
回应“产品特征”;
foreach($fields_至_显示为$fieldtitle=>$val){
回显“.$fieldtitle.”.$val.”;
}
回声';
}
}

谢谢,但我想这和我现在的处境是一样的。我仍然需要在woo_new_product_选项卡中创建内容,并将其作为全局变量传递。不,您需要在
woo_new_product_选项卡内容()中构建内容。
。我不能说这是否更有效,但它不需要全局。我认为这比使用全局的效率要低,因为我会调用get_post_meta来获取两个函数中的元值,而不仅仅是一个。是的,但是你可以通过连接
save_post并保存一个元字段来缓解这一问题。。。类似于
\u show\u features\u tab`如果任何相关字段有数据。然后,在每个函数中只检查1个元,而不使用全局元。