我向产品添加了自定义字段
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',
,但我得到了废话 - 只有输入被隐藏,但标签等仍然保留。
要隐藏其他产品自定义字段,您可以使用 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>';
}
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