php - WooCommerce 自定义产品类型选项未隐藏自定义产品选项卡

标签 php jquery wordpress woocommerce product

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

enter image description here

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 中找到。 (第 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' );

关于php - WooCommerce 自定义产品类型选项未隐藏自定义产品选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67341234/

相关文章:

javascript - Kendo菜单一次选择多个项目,ASP.NET MVC

php - 从 mysql 数据库中提取序列化数据并在 php 中反序列化和使用各个字段

php - 在数组中存储 MySQL 数据库连接

php - 通过在 php 中选择第一个下拉列表值来填充第二个下拉列表

javascript - Zurb Foundation 4 和同位素布局问题(某些浏览器)

javascript - 查找字符串 jQuery/Javascript 的部分内容

php - Wordpress:所以我一直在 mysql 查询中直接调用 wp_(tablenames)。纠正此问题的最佳方法是什么?

css - 如何为(帖子和页面)设置单独的布局并为主页设置不同的布局? CSS

php - 为什么 PHP 中的反序列化总是返回 false?

php - 使用CONVERT转换mysql中的特殊字符