我假设你已经添加礼品卡的产品类型选择。不过,为了完整性,我在此添加它,因为您在第二个函数中使用相同的名称。
add_filter('product_type_selector', 'so_42835590_add_product_type');
function so_42835590_add_product_type($types){
// Key should be exactly the same as in the class product_type parameter
$types[ 'gift-card' ] = __('Gift Card', 'your-plugin');
return $types;
}
您可以通过过滤woocommerce_product_data_tabs
添加自己的自定义选项卡,但你也可以添加类的现有标签。类是metabox javascript用于决定显示什么内容以及在产品类型更改时要隐藏的内容。在现有属性
add_filter('woocommerce_product_data_tabs', 'so_42835590_product_data_tabs');
function so_42835590_product_data_tabs($tabs) {
// Add an additional class to an existing tab, ex: Variations.
$tabs[ 'variations' ][ 'class' ][] = 'show_if_gift-card'; // gift-card must match key from above
return $tabs;
}
编辑你需要添加一些JavaScript来控制的可见性“已启用变化”标签,并在添加的属性。这应该做的伎俩:
jQuery(function ($) {
// Variable type options are valid for variable workshop.
$('.show_if_variable:not(.hide_if_gift-card)').addClass('show_if_gift-card');
// Trigger change
$('select#product-type').change();
// Show variable type options when new attribute is added.
$(document.body).on('woocommerce_added_attribute', function(e) {
$('#product_attributes .show_if_variable:not(.hide_if_gift-card)').addClass('show_if_gift-card');
var $attributes = $('#product_attributes').find('.woocommerce_attribute');
if ('gift-card' == $('select#product-type').val()) {
$attributes.find('.enable_variation').show();
}
});
});
感谢@helgatheviking它的工作原理。你可以添加一些更多的细节,如何显示复选框字段“用于变化”到属性选项卡? http://prntscr.com/el16t1仅当产品类型是变量产品时才显示。 –
请参阅我添加的脚本。 – helgatheviking
谢谢这么多@helgatheviking –