根据产品类型显示或隐藏 WooCommerce 管理产品自定义字段

问题描述 投票:0回答:1

我向产品添加了自定义字段

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',
,但我得到了废话 - 只有输入被隐藏,但标签等仍然保留。

php wordpress woocommerce product admin
1个回答
0
投票

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

wrapper_class
属性与“show_if_simple”或“hide_if_variable”一起使用,例如:

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

或者您可以直接在开头使用“show_if_simple”或“hide_if_variable”

<div>
,例如:

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

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

所以输入您的代码:

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

    // Only for 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>';
}

添加

用于显示或隐藏自定义产品字段的不同产品类型类
WooCommerce 中默认:
  • 产品类型“简单”
    show_if_simple
    hide_if_simple
  • 产品类型“变量”
    show_if_variable
    hide_if_variable
  • 产品类型 “外部”
    show_if_external
    hide_if_external
  • 产品类型“分组”
    show_if_grouped
    hide_if_grouped
  • 虚拟产品:
    show_if_virtual
    hide_if_virtual
  • 可下载产品:
    show_if_downloadable
    hide_if_downloadable
其他一些国外产品:
  • 产品类型“订阅”
    show_if_subscription
    hide_if_subscription
  • 产品类型“预订”
    show_if_booking
    hide_if_booking
  • 产品类型 “捆绑”
    show_if_bundle
    hide_if_bundle
© www.soinside.com 2019 - 2024. All rights reserved.