隐藏特定页面WooCommerce描述和其他信息,以及使用Wordpress自定义字段的评论选项卡

问题描述 投票:0回答:1

我正在尝试建立一个系统,通过该系统我可以删除产品选项卡,但只能在某些单个产品页面上删除,我需要使用wordpress自定义字段定义要隐藏其产品选项卡的页面。我要调用的自定义字段名称:“ hide_product_page_tabs”,并且定义的值必须为“ 1”或“ 0”(是或否)。

我在所选的woocommerce产品页面上创建了一个新的Wordpress自定义字段。

自定义字段名称:hide_product_tabs

[自定义字段值:在自定义字段中定义了一个'1'来触发代码,或通过其他任何方式(例如'0'来关闭它)。

我放置在子主题的functions.php中:

/* WooCommerce hide product page tabs - hide_product_tabs */
/**
 * Remove existing tabs from single product pages.
 * https://gist.github.com/mikejolley/c75083db7f6110cbdbe4808e3af36fe3
 */
 function remove_woocommerce_product_tabs( $tabs ) {
    unset( $tabs['description'] );
    unset( $tabs['reviews'] );
    unset( $tabs['additional_information'] );
    return $tabs;
}
function hide_product_page_tabs() {
        global $post;
        $product_id = $post->ID;
        $HideProductTabsValue =  get_post_meta($product_id,'hide_product_tabs',true);
        if (strpos($HideProductTabsValue, '1') !== false) {
        return add_filter( 'woocommerce_product_tabs', 'remove_woocommerce_product_tabs', 98 );
        }
}       
add_action('woocommerce_single_product_summary','hide_product_page_tabs');

欢迎任何提示!

php wordpress woocommerce custom-fields
1个回答
0
投票

根据您的描述,没有看到用于此目的的代码,您只需使用以下内容即可

function hide_product_tabs( $tabs ) {

    // Get the global product object
    global $product;

    // Get product id
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    $HideProductTabsValue = get_post_meta( $product_id, 'hide_product_tabs', true);

    // 1 = true
    if( $HideProductTabsValue == true ) {
        unset( $tabs['description'] ); // (Description tab)  
        unset( $tabs['reviews'] ); // (Reviews tab)
        unset( $tabs['additional_information'] ); // (Additional information tab)       
    }

    return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'hide_product_tabs', 98 );
© www.soinside.com 2019 - 2024. All rights reserved.