WooCommerce 自定义产品类型选项不隐藏自定义产品选项卡

2024-05-16

我刚刚向我的 WC 管理产品页面添加了自定义产品类型选项:

add_filter( 'product_type_options', [ $this, 'filter_product_type_options' ], 99, 1 );
public function filter_product_type_options( array $product_type_options ): array {
    $product_type_options['batches'] = [
        'id'            => '_batches',
        'wrapper_class' => 'show_if_simple show_if_variable',
        'label'         => esc_html__( 'Batches', 'woo-batches' ),
        'description'   => esc_html__( 'Product is sold from batches.', 'woo-batches' ),
        'default'       => 'no',
    ];

    return $product_type_options;
}

我还添加了一个自定义产品数据选项卡,我只想在选中该选项时显示该选项卡:

add_filter( 'woocommerce_product_data_tabs', [ $this, 'filter_woocommerce_product_data_tabs' ] );
public function filter_woocommerce_product_data_tabs( array $tabs ): array {
    $tabs['woo_batches'] = [
        'label'    => esc_html__( 'Batches', 'woo-batches' ),
        'target'   => 'woo_batches',
        'class'    => [ 'show_if_simple', 'show_if_variable', 'show_if_batches' ],
        'priority' => 25
    ];

    return $tabs;
}

但是当我选中/取消选中我的选项时,该选项卡绝对不会执行任何操作。我是否需要自己的隐藏 JS 函数,还是我遗漏了一些东西?我通常认为我可以显示/隐藏所有内容

show_if_xxx
hide_if_xxx

您确实必须自己提供一段 jQuery。如何将其应用于其他复选框(默认)可以在js/admin/meta-boxes-product.js https://github.com/woocommerce/woocommerce/blob/b19500728b4b292562afb65eb3a0c0f50d5859de/assets/js/admin/meta-boxes-product.js(第 122 行...)

注意:我还对您现有的代码做了一些小的更改,其中某些类

所以你得到:

// Add a checkbox as Woocommerce admin product option
function filter_product_type_options( $product_type_options ) { 
    $product_type_options['batches'] = array(
        'id'            => '_batches',
        'wrapper_class' => 'show_if_simple show_if_variable',
        'label'         => __( 'Batches', 'woo-batches' ),
        'description'   => __( 'Product is sold from batches.', 'woo-batches' ),
        'default'       => 'no',
    );

    return $product_type_options;
}
add_filter( 'product_type_options', 'filter_product_type_options', 10, 1 );

// Add custom product setting tab.
function filter_woocommerce_product_data_tabs( $default_tabs ) {
    $default_tabs['woo_batches'] = array(
        'label'    => __( 'Batches', 'woo-batches' ),
        'target'   => 'woo_batches',
        'class'    => array( 'hide_if_simple', 'hide_if_variable', 'show_if_batches' ),
        'priority' => 25
    );
    
    return $default_tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'filter_woocommerce_product_data_tabs', 10, 1 );

// Prints scripts or data before the default footer scripts.
// This hook is for admin only and can’t be used to add anything on the front end.
function action_admin_footer() {
    ?>
    <script>
        jQuery(document).ready(function($) {
            $( 'input#_batches' ).change( function() {
                var is_batches = $( 'input#_batches:checked' ).length;
            
                // Show rules.
                if ( is_batches ) {
                    $( '.show_if_batches' ).show();
                }
            });            
        });
    </script>
    <?php
}
add_action( 'admin_footer', 'action_admin_footer' );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WooCommerce 自定义产品类型选项不隐藏自定义产品选项卡 的相关文章

随机推荐

  • 过程式编程与 OOP 的开发成本?

    我有相当深厚的 OO 背景 OOD 和 OOP 的好处对我来说是第二天性 但最近我发现自己在一家与过程编程习惯相关的开发商店 实现语言具有一些 OOP 功能 但它们没有以最佳方式使用 更新 每个人似乎对这个话题都有自己的看法 我也是如此 但
  • 本地开发的 Azure Functions 扩展包版本问题

    我有一个带有队列触发器的 Java 11 Azure 函数 该函数在部署到 Azure 时按预期工作 并正确从定义的服务总线主题中提取消息 但是 运行相同的功能locally除非我回滚版本 否则不起作用Azure Functions 绑定扩
  • CryptQueryObject 的 CNG 替代品

    我有兴趣尝试从数字签名中读取字段 我有调用 CryptQueryObject 的代码 然后调用 CryptMsgGetParam 来获取一些字段 最后调用 CertFindCertificateInStore 来加载证书 有关如何使用下一代
  • 使用 Spring 控制器处理错误 404

    I use ExceptionHandler处理我的网络应用程序抛出的异常 在我的例子中我的应用程序返回JSON回应HTTP status用于对客户端的错误响应 但是 我正在尝试弄清楚如何处理error 404返回与处理的类似的 JSON
  • 在我的 Unity 应用程序中检测来电

    我试图让我的游戏在接到电话时暂停 我想知道我使用的任何函数是否可以做到这一点 我在我的源代码中使用了它们 但它们都不起作用 void OnApplicationPause bool paused if paused true if isPa
  • 如何在角度材料的下拉菜单中显示表单?

    我想展示一个简单的form 如下图所示 作为dropdown当icon被点击 我查看了角材料的组件列表 但找不到任何合适的组件 有menu https material angular io components menu overview
  • 即使在轴上进行自动量程调整,我也可以保留积分刻度线吗?

    我 偷 了一些代码here http fxexperience com 2012 01 curve fitting and styling areachart 拥有一个AreaChart我在 FXML 中使用了 平滑线条 它的工作原理如下
  • Selenium IDE-自动化Select2搜索框

    我正在尝试自动化 selenium IDE 中的 select2 搜索框 我打开它并输入了我正在搜索的搜索关键字 但是 即使我有用于显示结果的代码 它也不起作用 问题是我猜字符输入得太快 因此结果不会显示在搜索框中 我确信我在某个地方出错了
  • python array(10,1) 和 array(10,) 之间的区别

    我正在尝试将 MNIST 数据集加载到数组中 当我使用 X train y train X test y test mnist load data 我得到一个数组 y test 10000 但我希望它的形状为 10000 1 数组 1000
  • 动态更改温斯顿的日志级别

    我尝试在运行时实现日志级别更改 我一直在关注https github com yannvr Winston dynamic loglevel blob master test https github com yannvr Winston
  • 在哪里保存选项值、重要文件的路径等[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我正在创建一个程序 需要设置一些选项值以及图像文件的一些路径 SQLite 数据库的路径 有关各种按钮上文本的一些信息 有关要使用哪个数据库的信
  • 无法访问“不安全”java方法的java表达式语言

    我正在开发一个项目 让用户向服务器提交小 脚本 然后我将执行这些脚本 有很多脚本语言可以嵌入到Java程序中 例如mvel ognl uel clojure rhino javascript等 但是 据我所知 它们都允许脚本编写者调用Jav
  • 在自动完成上添加 jQuery 延迟

    我正在尝试为应用程序创建 jQuery 自动完成 search input on keyup function search this val autocomplete div autocomplete get ajax search se
  • 简单的 Backbone 搜索页面 - 您会怎么做?

    我想使用 Backbone 实现一个简单的搜索页面 它不是单页应用程序 但仍然想使用 Backbone 构建我的 JavaScript 代码 搜索页面由搜索表单和搜索结果组成 搜索是通过 AJAX 完成的 并且必须保存在历史记录中 从历史记
  • Spring boot 中特定包的自定义日志文件

    我有一个带有专门操作的java包 专业化是因为它们很少被使用 并且我不想将它们与普通日志记录混合在一起 我知道添加logging file myapplication log会将日志记录重定向到此文件 但有没有办法指定仅从特定包记录到另一个
  • .htaccess 将所有页面重定向到新域上的主页

    我将使用哪个重定向规则来重定向下的所有页面olddomain example被重定向到newdomain example 该网站的结构完全不同 所以我想要每一页在旧域名下重定向到新域名索引页 我认为这可以 在 olddomain examp
  • 检查 python 中命令行参数的数量

    我是蟒蛇新手 还是把脚弄湿了 我正在尝试做这样的事情 import sys if len sys argv lt 3 or lt len sys argv gt 3 print This script will compare two fi
  • 从 pyspark.sql 中的列表创建数据框

    我完全陷入了有线的境地 现在我有一个清单li li example data map lambda x get labeled prediction w x collect print li type li 输出就像 0 0 59 0 0
  • BigQuery - 预定查询更新通知电子邮件

    有没有办法将计划查询通知电子邮件更新为自定义内容 默认情况下 它是创建者的电子邮件 但是 这通常是没有真正电子邮件收件人的服务帐户 例如 通过 terraform 配置 我们将拥有一个服务帐户 我们希望将电子邮件通知目标从 SA 更新为支持
  • WooCommerce 自定义产品类型选项不隐藏自定义产品选项卡

    我刚刚向我的 WC 管理产品页面添加了自定义产品类型选项 add filter product type options this filter product type options 99 1 public function filte