根据 WooCommerce 产品类型显示或隐藏管理员自定义设置字段
Show or hide admin custom setting fields, based on WooCommerce product type
我制作了自定义字段后端,我想为简单的产品类型显示它,而为其他类型隐藏它。我认为对我来说,实现它是不够的。我将不胜感激任何帮助。谢谢!
我添加的代码是:
add_action('admin_head', 'show_this_for_simple_products_and_hide_for_others');
function show_this_for_simple_products_and_hide_for_others() {
?>
<script>
jQuery( function($){
$( 'body' ).on( 'woocommerce-product-type-change', function( event, select_val, select ) {
if ( select_val === 'simple' ) {
// modify to an actual CSS class selector
$( '.p.form-field.final_product_sku_field' ).show();
}
} );
$( 'select#product-type' ).change();
} );
</script>
<?php
}
已更新
您不需要它,因为已经有一些 CSS class 选择器允许这样做。
要显示“简单”产品类型的字段,您需要在自定义设置字段之间添加 (在 您的函数中):
echo '<div class="show_if_simple hidden">';
// Your fields here (for "simple" products type only)
echo '</div >';
You can use the show_if_{$product_type}
or hide_if_{$product_type}
composite class selectors, to show or hide custom settings product fields based on the product type (where {$product_type}
need to be replaced by the targeted product type).
It works for all custom product types.
The class selector hidden
specify that the content is hidden for non specified product types to be shown.
You can also use those other class selectors like show_if_virtual
, hide_if_virtual
, show_if_downloadable
and hide_if_downloadable
.
The class selector options_group
adds a separator horizontal line.
这里是一个例子(只显示简单产品的字段):
add_action( 'woocommerce_product_options_sku', 'add_product_final_sku_custom_field' );
function add_product_final_sku_custom_field(){
global $post, $product_object;
echo '<div class="show_if_simple hidden">';
woocommerce_wp_text_input( array(
'label' => __( 'Final SKU', 'woocommerce' ),
'placeholder' => __( 'Enter final SKU here', 'woocommerce' ),
'id' => 'final_sku',
'desc_tip' => true,
'description' => __( 'This SKU is for final use only.', 'woocommerce' ),
) );
echo '</div>';
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
您可以看到 on the related core html files code source 在单个产品管理页面上显示 WooCommerce 产品设置字段。
我制作了自定义字段后端,我想为简单的产品类型显示它,而为其他类型隐藏它。我认为对我来说,实现它是不够的。我将不胜感激任何帮助。谢谢! 我添加的代码是:
add_action('admin_head', 'show_this_for_simple_products_and_hide_for_others');
function show_this_for_simple_products_and_hide_for_others() {
?>
<script>
jQuery( function($){
$( 'body' ).on( 'woocommerce-product-type-change', function( event, select_val, select ) {
if ( select_val === 'simple' ) {
// modify to an actual CSS class selector
$( '.p.form-field.final_product_sku_field' ).show();
}
} );
$( 'select#product-type' ).change();
} );
</script>
<?php
}
已更新
您不需要它,因为已经有一些 CSS class 选择器允许这样做。
要显示“简单”产品类型的字段,您需要在自定义设置字段之间添加 (在 您的函数中):
echo '<div class="show_if_simple hidden">';
// Your fields here (for "simple" products type only)
echo '</div >';
You can use the
show_if_{$product_type}
orhide_if_{$product_type}
composite class selectors, to show or hide custom settings product fields based on the product type (where{$product_type}
need to be replaced by the targeted product type).It works for all custom product types.
The class selector
hidden
specify that the content is hidden for non specified product types to be shown.You can also use those other class selectors like
show_if_virtual
,hide_if_virtual
,show_if_downloadable
andhide_if_downloadable
.The class selector
options_group
adds a separator horizontal line.
这里是一个例子(只显示简单产品的字段):
add_action( 'woocommerce_product_options_sku', 'add_product_final_sku_custom_field' );
function add_product_final_sku_custom_field(){
global $post, $product_object;
echo '<div class="show_if_simple hidden">';
woocommerce_wp_text_input( array(
'label' => __( 'Final SKU', 'woocommerce' ),
'placeholder' => __( 'Enter final SKU here', 'woocommerce' ),
'id' => 'final_sku',
'desc_tip' => true,
'description' => __( 'This SKU is for final use only.', 'woocommerce' ),
) );
echo '</div>';
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
您可以看到 on the related core html files code source 在单个产品管理页面上显示 WooCommerce 产品设置字段。