继续这个问题:Custom metabox content displayed in single product additional tabs on Woocommerce

当我使用this answer中的以下代码时:

到了this method,当我不想显示空制表符时,我得到了一个错误.我使用以下代码:

// Add a custom metabox
add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
    add_meta_box(
        'add_product_metabox_additional_tabs',
        __( 'Additional product Tabs', 'woocommerce' ),
        'additional_product_tabs_metabox_content',
        'product',
        'normal',
        'high'
    );
}

//  Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
    // Key Features
    echo '<h4>' . __( 'Key Features', 'woocommerce' ) . '</h4>';
    $value = get_post_meta( $post->ID, '_key_features', true );
    wp_editor( $value, '_key_features', array( 'editor_height' => 100 ) );


    // What News?
    echo '<br><hr><h4>' . __( 'What News?', 'woocommerce' ) . '</h4>';
    $value = get_post_meta( $post->ID, '_what_news', true );
    wp_editor( $value, '_what_news', array( 'editor_height' => 100 ) );


    // FAQ
    echo '<br><hr><h4>' . __( 'FAQ', 'woocommerce' ) . '</h4>';
    $value = get_post_meta( $post->ID, '_faq', true );
    wp_editor( $value, '_faq', array( 'editor_height' => 100 ) );


    // Nonce field (for security)
    echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}


// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {

    // Security check
    if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
        return $post_id;
    }

    //Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
        return $post_id;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    if ( ! current_user_can( 'edit_product', $post_id ) ) {
        return $post_id;
    }

    // Sanitize user input and save the post meta fields values.

    if( isset($_POST[ '_key_features' ]) )
        update_post_meta( $post_id, '_key_features', wp_kses_post($_POST[ '_key_features' ]) );

    if( isset($_POST[ '_what_news' ]) )
        update_post_meta( $post_id, '_what_news', wp_kses_post($_POST[ '_what_news' ]) );
    
    if( isset($_POST[ '_faq' ]) )
        update_post_meta( $post_id, '_faq', wp_kses_post($_POST[ '_faq' ]) );

}

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
    
    $values = get_field('key_features_tab');
    if ( ! empty($values) ) { 
        $tabs['key_features_tab'] = array(
            'title'     => __( 'Key Features', 'woocommerce' ),
            'priority'  => 10,
            'callback'  => 'woo_key_features_tab_content'
        );
    }
    
    $what_news_values = get_field('what_news_tab');
    if ( ! empty($what_news_values) ) {
        $tabs['what_news_tab'] = array(
            'title'     => __( 'What News?', 'woocommerce' ),
            'priority'  => 20,
            'callback'  => 'woo_what_news_tab_content'
        );
    }
    
    $faq_values = get_field('faq_tab');
    if ( ! empty($faq_values) ) {
        $tabs['faq_tab'] = array(
            'title'     => __( 'FAQ', 'woocommerce' ),
            'priority'  => 30,
            'callback'  => 'woo_faq_tab_content'
        );
    }
    
    $tabs['reviews']['priority'] = 40;

    return $tabs;
}


function woo_key_features_tab_content()  {
    global $product;

    echo'<div><p>'. $product->get_meta( '_key_features' ) . '</p></div>';
}

function woo_what_news_tab_content()  {
    global $product;

    echo'<div><p>'. $product->get_meta( '_what_news' ) . '</p></div>';
}

function woo_faq_tab_content()  {
    global $product;

    echo'<div><p>'. $product->get_meta( '_faq' ) . '</p></div>';
}

这段代码中的问题在哪里?

另一个问题是,我知道当产品有体重、身高等时,会显示附加信息.是否可以永远删除附加信息选项卡,因为我的产品是虚拟的,并且我没有实体产品. 另外,我可以创建一个与WooCommerce的附加信息选项卡无关的名为Additional Info的选项卡吗?

推荐答案

问题是您使用的是ACF get_field()函数,而不是WooCommerce方法get_meta() Inside woo_custom_product_tabs()挂钩函数.此外,您正在使用错误的元密钥.

因此,将其更改为:

add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
    global $product;

    $key_features = $product->get_meta('_key_features');
    if ( ! empty($key_features) ) { 
        $tabs['key_features_tab'] = array(
            'title'     => __( 'Key Features', 'woocommerce' ),
            'priority'  => 10,
            'callback'  => 'woo_key_features_tab_content'
        );
    }
    
    $what_news = $product->get_meta('_what_news');;
    if ( ! empty($what_news) ) {
        $tabs['what_news_tab'] = array(
            'title'     => __( 'What News?', 'woocommerce' ),
            'priority'  => 20,
            'callback'  => 'woo_what_news_tab_content'
        );
    }
    
    $faq_values = $product->get_meta('_faq');
    if ( ! empty($faq_values) ) {
        $tabs['faq_tab'] = array(
            'title'     => __( 'FAQ', 'woocommerce' ),
            'priority'  => 30,
            'callback'  => 'woo_faq_tab_content'
        );
    }
    
    $tabs['reviews']['priority'] = 40;

    return $tabs;
}

现在,它应该会像预期的那样发挥作用.

Php相关问答推荐

Laravel:从匹配的数组数据中获取记录

在不超过PHP时间限制的情况下,在Laravel中高效地插入批量数据(25万条记录)?(队列、多租户)

在ShipStation for WooCommerce中使用自定义订单项目元数据

获取要删除的图像的公共ID

如何在php中生成包含第100秒的时间序列?

目标类[Set_Locale]不存在.拉威尔

如何使用php一次更新两个数据库中的MySQL表

免费送货柜台,考虑到折扣码

FlySystem v3:使用本地适配器时出现问题

如何在PHP中根据会话将用户重定向到不同的页面?

使用简码在 WooCommerce 我的帐户中显示一些用户数据

用于更新 WooCommerce 管理产品列表中库存数量的自定义输入字段

WooCommerce 按产品运输插件:仅收取较高的运输费用

Google Drive API 服务->文件->get('root') 失败并显示找不到文件. (范围问题?)

如何使用不同的方法编写嵌套连接查询并将两列连接成一列在 laravel 查询生成器中

htaccess Rewriterule 无法以任何方式工作

从图像URL中获取文件扩展名?

如何使用相同的变量使函数参数对于数组和字符串都是可选的

PHP 中是否有 is_closure() 函数?

PHP 中的 curl 验证失败(openssh 连接正常)