如何在Woocommerce的自定义产品类型中添加“变化"标签? [英] How to add Variations tab in custom product type in Woocommerce?

查看:108
本文介绍了如何在Woocommerce的自定义产品类型中添加“变化"标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我已经安装了礼品卡"插件,基本上是在添加一个 产品类型礼品卡".
    • 我需要增强其功能.
    • 还有其他选项卡可用于此操作,但是选项卡"Variations"是 不是.
    • 如何添加此标签,这样我的自定义产品类型也可以 表现得像可变产品.
    • 屏幕截图: http://prntscr.com/ekogwd
    • 我必须严格使用自定义产品类型,这意味着我无法将其更改为可变产品类型.
    • 是否有任何挂钩,操作或过滤器,可以使用woocommerce将该选项卡手动添加到我的自定义产品类型礼品卡"中?
    • I've installed a plugin for "Gift Cards", which is basically adding a product type "Gift card".
      • I need to enhance its functionality.
      • There are other tabs available for this but the tab "Variations" is not.
      • How can i add this tab so my custom product type can also behave like a variable product.
      • Screen Shot: http://prntscr.com/ekogwd
      • And i've to use my custom product type strictly means i cannot change it to variable product type.
      • Is there any hook, action, or filter to add this tab manually to my custom product type "Gift card" using woocommerce?

      推荐答案

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

      I assume you've already added the Gift Card to the product type selector. However, I'm adding it here for completeness and because you ave to use the same name in the second function.

      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用于确定更改产品类型时显示的内容和隐藏的内容的内容.

      You can add your own custom tabs by filtering woocommerce_product_data_tabs but you can also add classes to the existing tabs. Classes are what the metabox javascript uses to decide what to show and what to hide when the product type is changed.

      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,以控制已启用变体"选项卡在现有属性中以及添加属性时的可见性.这应该可以解决问题:

      Edit you do need to add some javascript to control the visibility of "Enabled for variations" tab in existing attributes, and when attributes are added. This should do the trick:

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

      这篇关于如何在Woocommerce的自定义产品类型中添加“变化"标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆