Woocommerce 单一产品自定义选项卡上显示带有所见即所得字段的元框

2023-12-10

需要在单个产品页面上显示相关产品中的自定义字段。

我想添加一个元框到Add New Product字段并在评论下的单个产品页面上的自定义选项卡上显示结果。我尝试使用代码,但页面上没有显示任何内容。添加额外的产品选项卡可以帮助我添加额外的信息。所以添加、保存和显示产品就是我正在寻找的。

add_filter( 'add_meta_boxes', 'bhww_core_cpt_metaboxes' );

function bhww_core_cpt_metaboxes( $meta_boxes ) {

    //global $prefix;
    $prefix = '_bhww_'; // Prefix for all fields

    // Add metaboxes to the 'Product' CPT
    $meta_boxes[] = array(
        'id'         => 'bhww_woo_tabs_metabox',
        'title'      => 'Additional Product Information - <strong>Optional</strong>',
        'pages'      => array( 'product' ), // Which post type to associate with?
        'context'    => 'normal',
        'priority'   => 'default',
        'show_names' => true,                   
        'fields'     => array(
            array(
                'name'    => __( 'Ingredients', 'cmb' ),
                'desc'    => __( 'Anything you enter here will be displayed on the Ingredients tab.', 'cmb' ),
                'id'      => $prefix . 'ingredients_wysiwyg',
                'type'    => 'wysiwyg',
                'options' => array( 'textarea_rows' => 5, ),
            ),
            array(
                'name'    => __( 'Benefits', 'cmb' ),
                'desc'    => __( 'Anything you enter here will be displayed on the Benefits tab.', 'cmb' ),
                'id'      => $prefix . 'benefits_wysiwyg',
                'type'    => 'wysiwyg',
                'options' => array( 'textarea_rows' => 5, ),
            ),
        ),
    );

    return $meta_boxes;

}

/////////////////////////////////////////

add_filter( 'woocommerce_product_data_tabs', 'bhww_woo_extra_tabs' );

function bhww_woo_extra_tabs( $tabs1 ) {

    global $post;
    $product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );
    $product_benefits    = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );

    if ( ! empty( $product_ingredients ) ) {

        $tabs1['ingredients_tab'] = array(
            'title'    => __( 'Ingredients', 'woocommerce' ),
            'priority' => 15,
            'callback' => 'bhww_woo_ingredients_tab_content'
        );

    }

    if ( ! empty( $product_benefits ) ) {

        $tabs1['benefits_tab'] = array(
            'title'    => __( 'Benefits', 'woocommerce' ),
            'priority' => 16,
            'callback' => 'bhww_woo_benefits_tab_content'
        );

    }

    return $tabs1;

}

function bhww_woo_ingredients_tab_content() {

    global $post;
    $product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );

    if ( ! empty( $product_ingredients ) ) {

        echo '<h2>' . esc_html__( 'Product Ingredients', 'woocommerce' ) . '</h2>';

        // Updated to apply the_content filter to WYSIWYG content
        echo apply_filters( 'the_content', $product_ingredients );

    }

}

function bhww_woo_benefits_tab_content() {

    global $post;
    $product_benefits = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );

    if ( ! empty( $product_benefits ) ) {

        echo '<h2>' . esc_html__( 'Product Benefits', 'woocommerce' ) . '</h2>';

        // Updated to apply the_content filter to WYSIWYG content
        echo apply_filters( 'the_content', $product_benefits );

    }

}

back end requirement


以下是在管理编辑产品页面中添加 2 个自定义字段(编辑器所见即所得)并在前端单个产品页面自定义选项卡中显示值的方法:

## ---- 1. Backend ---- ##

// Adding a custom Meta container to admin products pages
add_action( 'add_meta_boxes', 'create_custom_meta_box' );
if ( ! function_exists( 'create_custom_meta_box' ) )
{
    function create_custom_meta_box()
    {
        add_meta_box(
            'custom_product_meta_box',
            __( 'Additional Product Information <em>(optional)</em>', 'cmb' ),
            'add_custom_content_meta_box',
            'product',
            'normal',
            'default'
        );
    }
}

//  Custom metabox content in admin product pages
if ( ! function_exists( 'add_custom_content_meta_box' ) ){
    function add_custom_content_meta_box( $post ){
        $prefix = '_bhww_'; // global $prefix;

        $ingredients = get_post_meta($post->ID, $prefix.'ingredients_wysiwyg', true) ? get_post_meta($post->ID, $prefix.'ingredients_wysiwyg', true) : '';
        $benefits = get_post_meta($post->ID, $prefix.'benefits_wysiwyg', true) ? get_post_meta($post->ID, $prefix.'benefits_wysiwyg', true) : '';
        $args['textarea_rows'] = 6;

        echo '<p>'.__( 'Ingredients', 'cmb' ).'</p>';
        wp_editor( $ingredients, 'ingredients_wysiwyg', $args );

        echo '<p>'.__( 'Benefits', 'cmb' ).'</p>';
        wp_editor( $benefits, 'benefits_wysiwyg', $args );

        echo '<input type="hidden" name="custom_product_field_nonce" value="' . wp_create_nonce() . '">';
    }
}



//Save the data of the Meta field
add_action( 'save_post', 'save_custom_content_meta_box', 10, 1 );
if ( ! function_exists( 'save_custom_content_meta_box' ) )
{

    function save_custom_content_meta_box( $post_id ) {
        $prefix = '_bhww_'; // global $prefix;

        // We need to verify this with the proper authorization (security stuff).

        // Check if our nonce is set.
        if ( ! isset( $_POST[ 'custom_product_field_nonce' ] ) ) {
            return $post_id;
        }
        $nonce = $_REQUEST[ 'custom_product_field_nonce' ];

        //Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $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;
        }

        // Check the user's permissions.
        if ( 'product' == $_POST[ 'post_type' ] ){
            if ( ! current_user_can( 'edit_product', $post_id ) )
                return $post_id;
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) )
                return $post_id;
        }

        // Sanitize user input and update the meta field in the database.
        update_post_meta( $post_id, $prefix.'ingredients_wysiwyg', wp_kses_post($_POST[ 'ingredients_wysiwyg' ]) );
        update_post_meta( $post_id, $prefix.'benefits_wysiwyg', wp_kses_post($_POST[ 'benefits_wysiwyg' ]) );
    }
}

## ---- 2. Front-end ---- ##

// Create custom tabs in product single pages
add_filter( 'woocommerce_product_tabs', 'custom_product_tabs' );
function custom_product_tabs( $tabs ) {
    global $post;

    $product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );
    $product_benefits    = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );

    if ( ! empty( $product_ingredients ) )
        $tabs['ingredients_tab'] = array(
            'title'    => __( 'Ingredients', 'woocommerce' ),
            'priority' => 45,
            'callback' => 'ingredients_product_tab_content'
        );

    if ( ! empty( $product_benefits ) )
        $tabs['benefits_tab'] = array(
            'title'    => __( 'Benefits', 'woocommerce' ),
            'priority' => 50,
            'callback' => 'benefits_product_tab_content'
        );

    return $tabs;
}

// Add content to custom tab in product single pages (1)
function ingredients_product_tab_content() {
    global $post;

    $product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );

    if ( ! empty( $product_ingredients ) ) {
        echo '<h2>' . __( 'Product Ingredients', 'woocommerce' ) . '</h2>';

        // Updated to apply the_content filter to WYSIWYG content
        echo apply_filters( 'the_content', $product_ingredients );
    }
}

// Add content to custom tab in product single pages (2)
function benefits_product_tab_content() {
    global $post;

    $product_benefits = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );

    if ( ! empty( $product_benefits ) ) {
        echo '<h2>' . __( 'Product Benefits', 'woocommerce' ) . '</h2>';

        // Updated to apply the_content filter to WYSIWYG content
        echo apply_filters( 'the_content', $product_benefits );
    }
}

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

经过测试并有效。你会得到:

1)在后台:

enter image description here

2)在前端:

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Woocommerce 单一产品自定义选项卡上显示带有所见即所得字段的元框 的相关文章

  • 扩展 PHP 正则表达式以涵盖“srcset”和“style”属性

    我创建了一个 WordPress 插件 可以将所有链接变成协议相关 URL https en wikipedia org wiki Wikipedia Protocol relative URL 删除http and https 基于我在中
  • 在 PHP 中调用用户定义的函数 eval()

    我一直在使用 eval 函数进行 php 测试 但 eval 似乎无法正确调用用户定义的函数 请看我的例子 function equals a b if t r return true else throw new Exception ex
  • 如何在 PHP 中实现前向索引?

    我希望在 PHP 中实现一个简单的前向索引器 是的 我确实知道 PHP 并不是完成这项任务的最佳工具 但无论如何我还是想这样做 其背后的理由很简单 我想要一个 并且是 PHP 版本 让我们做一些基本假设 整个互联网包括 大约五千个 HTML
  • UTF-8、PHP 和 XML Mysql

    我在解决这个问题时遇到了很大的问题 我有一个编码 latin1 swedish ci 的 mysql 数据库和一个存储名称和地址的表 我正在尝试输出 UTF 8 XML 文件 但在使用以下字符串时遇到问题 Otiv gen它被输出为Otiv
  • Laravel 8 图像不显示

    我在用Laravel 8 试图显示来自storage app subject image 路径 但不显示 下面是我的代码blade img src asset subject image data gt subject image alt
  • 更改管理仪表板 WooCommerce 小部件标题

    我想将 WooCommerce 仪表板小部件标题从 WooCommerce 状态 更改为其他内容 我知道我需要在 Functions php 中使用钩子 但我正在努力使这项工作正常进行 我在 WooCommerce 中找到了生成仪表板小部件
  • 无法将外键值插入链接表

    我目前正在尝试将数据插入名为的表中 客户报价 该表充当 顾客 表和 客户关税 桌子 它还记录通过以下方式提交数据的用户 user table 这是我的数据库的架构 https i stack imgur com gyCdb png http
  • 如何在使用 Piwik 进行分析的页面上显示点击/访问计数器

    我想在主页上显示当天的访问量 该页面由 Piwik 跟踪 如何将 API 与 PHP 结合使用来获取今天的 唯一 访问量和点击量 以便我可以将它们显示在页面上的某个位置 result file get contents http mysit
  • 配置 htaccess 以使用 Angular 和 PHP 路由

    我正在尝试使用 Angular 4 和 PHP 路由 但我无法配置它以便同时使用两者 我可以让它与其中之一一起工作 但不能同时与两者一起工作 这是我的文件夹结构 root index html vendor bundle js other
  • 如何使用 PHP 向用户发送每日电子邮件通知?

    我有一个简单的用户注册表单 其中有一个复选框 如果用户的任何项目有活动 用户可以每天收到电子邮件通知 就像 Stack Overflow 有一个 通知 电子邮件受保护 cdn cgi l email protection每天都有新的答案 我
  • 如何按年和月对 WordPress 帖子进行分组?

    我正在尝试创建一个带有后查询并在输出中具有以下结构的函数 2021 January 1 Post Title 2 Post Title March 3 Post Title 2020 May 4 Post Title 这是我到目前为止所做的
  • 使用 Poedit 创建 POT 文件

    我正在拼命地尝试为我的 php 新应用程序创建一个目录 in 1 我创建了一个文件 trans php 其中放置了所有要翻译的值 例如 这是我的文件 2 我打开Poedit 在 路径 中这是我输入的内容 见图 然后我保存文件 php 的相同
  • 比较两个关联数组的键顺序

    假设我们有 2 个关联数组
  • Laravel 9.x 登录应用程序时目标类不存在错误

    尝试为管理面板制作一个登录应用程序以轻松编辑网站的其余部分 我有一个名为AuthController它执行多种操作 例如登录 注销 我决定只使用一个 而不是使用两个不同的控制器 当我去 login在我的浏览器上它返回Target class
  • rewrite_mod 已启用,但 .htaccess 不起作用

    我在 Amazon EC2 的 ubuntu 12 04 中使用 apache 2 2 我使用启用了 mod rewrite sudo a2enmod rewrite 并能够看到 apache2ctl M 现在我编写了以下 htaccess
  • PHP 中的 __DIR__ 和 dirname(__FILE__) 有什么区别吗?

    对我来说看起来是一样的 但我不确定 因为有很多项目使用dirname FILE 他们的结果是完全一样的 所以 这没有什么区别 例如 以下两行 var dump dirname FILE var dump DIR 两者都会给出相同的输出 st
  • PHP随机输出数组元素

    我如何从大约 20 个元素的数组中随机回显 5 个元素 Thanks 这有效吗 values array rand input 5 或者 作为更灵活的功能 function randomValues input num 5 return a
  • SQL 未插入到 Yii 中具有关系的表中

    我正在尝试创建一个用户 但所有值都没有插入到数据库中 Systems user 表与partys 表有关系 因为party id 是sytems user 的主键 没有插入任何内容 甚至没有错误 它只是返回到 创建 页面 这是我的架构 Ta
  • 性能方面插值(直接插入字符串)VS串联[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何将十进制转换为二进制并将其位值恢复到数组中?

    例如 result func 14 The result应该 array 1 1 1 0 如何实施func decbin http docs php net decbin会产生一个字符串二进制字符串 echo decbin 14 outpu

随机推荐