我在产品中添加了一个定制字段

add_action( 'woocommerce_product_options_sku', 'prodcode_fields');

function prodcode_fields() {
    global $post, $thepostid, $product_object;

    echo '<div class="options_prodcode_fields">';
   
    woocommerce_wp_text_input(
        array(
            'id' => '_prodcode_good_number',
            'value' => $product_object->get_meta('_prodcode_number', true),
        'type' => 'number',
            'label' => 'Product Code',
            'desc_tip' => true,
        'description' => __('Product Code', 'sv-prodcode'),
        )  );

echo '</div>';
}

一切都很好,但有必要不要把它展示在可变商品中.你能告诉我如何使用WooCommerce工具吗?

我试图添加'class' => 'show_if_simple',,但我得到的废话-只有输入是隐藏的,但标签等仍然存在.

enter image description here

推荐答案

要隐藏其他产品自定义域,您可以使用两种方法:

wrapper_class attribute与"show_if_Simple"或"Hide_IF_Variable"一起使用,如:

  • 'wrapper_class' => 'show_if_simple'
  • 'wrapper_class' => 'hide_if_variable'

也可以直接在开头的<div>中使用show_if_Simple或Hide_IF_Variable,如:

echo '<div class="options_prodcode_fields show_if_simple">';

echo '<div class="options_prodcode_fields hide_if_variable">';

So in f或 your code:

add_action( 'woocommerce_product_options_sku', 'display_admin_product_custom_setting_fields');
function display_admin_product_custom_setting_fields() {
    global $product_object;

    // Only f或 simple product type
    echo '<div class="options_prodcode_fields show_if_simple">';
   
    woocommerce_wp_text_input(
        array(
            'id'            => '_prodcode_good_number',
            'type'          => 'number',
            'label'         => __('Product Code', 'sv-prodcode'),
            'description'   => __('Product code description', 'sv-prodcode'),
            'desc_tip'      => true,
            'value'         => $product_object->get_meta('_prodcode_number'),
        )  );

    echo '</div>';
}

Addition

The different product type classes to show 或 hide a custom product field
By Default in WooCommerce:
  • Product type "simple": show_if_simplehide_if_simple
  • Product type "variable": show_if_variablehide_if_variable
  • Product type "external": show_if_externalhide_if_external
  • Product type "grouped": show_if_groupedhide_if_grouped
  • Virtual product: show_if_virtualhide_if_virtual
  • Downloadable product: show_if_downloadablehide_if_downloadable
Some other f或eign products:
  • Product type "subscription": show_if_subscriptionhide_if_subscription
  • Product type "booking": show_if_bookinghide_if_booking
  • Product type "bundle": show_if_bundlehide_if_bundle

Php相关问答推荐

基于验证动态更改输入字段类(名称密码)&

如果再次调用SESSION_START(),会话.gc-max是否会重新启动?或者它是从第一次创建会话开始计算的?

WooCommerce基于元数据的自定义范围目录排序

LaravelEloquent 地根据表中的值提取记录

PHP:PHP手册-Manual

Laravel eloquent-如果没有包含InitialValue的结果,则查询where Second Value

如果为布尔值,则 for each 循环重新启动

在PHP项目中安装OpenAI PHP SDK时出现问题

是否有条件判断是否发布了另一个Wordpress页面?

向WooCommerce管理订单总额添加自定义总额行

Laravel 在数据库上显示加密列

将一个表中的行连接为不同表MYSQL中的一列

PHP Laravel 迁移文件不创建关系

如何正确判断 PHP 是否已正确配置为使用 DOMDocument?

计算数组中连续值的数量

根据WooCommerce中的产品重量更改简单产品的输入数量步值

如何在 Process facade 中转义特殊字符?

转发和非转发呼叫 - 后期静态绑定

hasOne/belongsToMany 或多对多

如何将 2021-04-08T22:25:09.335541Z 作为时间戳保存到 Laravel 中的数据库中?