将自定义字段添加到woo产品页面(仅适用于某些产品ID)

时间:2018-09-22 23:39:39

标签: php woocommerce

我需要在产品页面上添加自定义标签,但我希望它仅对某些产品ID显示它。

这里是我的代码,用于在前端添加自定义标签。

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
    $tabs['desc_tab'] = array(
        'title'     => __( 'Additional Information', 'woocommerce' ),
        'priority'  => 50,
        'callback'  => 'woo_new_product_tab_content'
    );

    return $tabs;
}

然后我也应该添加以下功能来显示内容。

function woo_new_product_tab_content() {
    echo '<p>some text</p>';
}

但是现在这些功能适用于所有产品ID。我只想为某些产品ID加载这两个函数,有人可以帮我吗?

我很难在两个功能上都添加if(is_product() && get_the_id() == 8),但是$tabs不会出现在其他产品ID页面上(在产品ID为“ 8”的$tabs上效果很好)

1 个答案:

答案 0 :(得分:0)

该代码的问题不是要忘记返回$tabs

if(is_product()) {
//if(is_product())) {
return $tabs;
}
return $tabs;
}

所以最终的代码可能是这样的。

function woo_new_product_tab( $tabs ) {
if(condition) {
$tabs['desc_tab'] = array(
    'title'     => __( 'Additional Information', 'woocommerce' ),
    'priority'  => 50,
    'callback'  => 'woo_new_product_tab_content'
);
return $tabs;
}
return $tabs;
}
相关问题