首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Woocommerce中添加自定义产品类型中的Variations选项卡?

如何在Woocommerce中添加自定义产品类型中的Variations选项卡?
EN

Stack Overflow用户
提问于 2017-03-16 13:33:41
回答 1查看 4.2K关注 0票数 4
  • 我已经安装了一个插件“礼品卡”,这基本上是一个产品类型的“礼品卡”。
    • 我需要增强它的功能。
    • 还有其他选项卡可用于此,但选项卡"Variations“不是。
    • 如何添加此选项卡,以便我的自定义产品类型也可以表现为一个可变产品。
    • 屏幕截图:http://prntscr.com/ekogwd
    • 我必须严格地使用我的自定义产品类型,这意味着我不能将它更改为可变产品类型。
    • 是否有任何钩子,动作,或过滤器手动添加此选项卡到我的定制产品类型“礼品卡”使用woocommerce?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-16 14:14:48

我想您已经将礼品卡添加到产品类型选择器中了。但是,为了完整起见,我在这里添加它,因为您可以在第二个函数中使用相同的名称。

代码语言:javascript
复制
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添加自己的自定义选项卡,但也可以将类添加到现有的选项卡中。类是用来决定在更改产品类型时显示什么和隐藏什么的。

代码语言:javascript
复制
add_filter( 'woocommerce_product_data_tabs', 'so_42835590_product_data_tabs' );
function so_42835590_product_data_tabs( $tabs ) {

    $product_type = 'gift-card'; // must match array key in previous snippet

    // Add an additional class to an existing tab, ex: Variations.
    $tabs[ 'variations' ][ 'class' ][] = 'show_if_' . $product_type;  

    return $tabs;
}

编辑您确实需要添加一些javascript来控制现有属性中"Enabled“选项卡的可见性,以及添加属性的时间。这应该能起作用:

代码语言: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();
        }
    });

});
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42835590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档