2017-03-16 67 views
2
  • 我已经安装了一个“礼品卡”插件,它基本上增加了一个 产品类型“礼品卡”。
    • 我需要增强其功能。
    • 还有其他选项卡可用于此,但选项卡“变体”不是 。
    • 如何添加此选项卡,以便我的自定义产品类型也可以 表现得像变量产品。
    • 屏幕截图:http://prntscr.com/ekogwd
    • 而且我必须使用我的自定义产品类型严格意味着我不能将其更改为变量产品类型。
    • 是否有任何挂钩,动作,或过滤器来手动添加此标签到我的自定义产品类型“礼品卡”使用woocommerce?

回答

1

我假设你已经添加礼品卡的产品类型选择。不过,为了完整性,我在此添加它,因为您在第二个函数中使用相同的名称。

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(); 
     } 
    }); 

}); 
+0

感谢@helgatheviking它的工作原理。你可以添加一些更多的细节,如何显示复选框字段“用于变化”到属性选项卡? http://prntscr.com/el16t1仅当产品类型是变量产品时才显示。 –

+0

请参阅我添加的脚本。 – helgatheviking

+0

谢谢这么多@helgatheviking –

相关问题