首页 文章

将Woocommerce预订标签添加到我的自定义产品类型

提问于
浏览
0

我想要Woocommerce预购插件将自己添加到我创建的自定义产品类型中 . 如果我选择Woocommerce标准产品类型,我会得到一个预购标签 .

是否有一个钩子可以将我的“自定义产品类型”传递给插件,因此它知道它应该可见?

我已按照本指南:http://jeroensormani.com/adding-a-custom-woocommerce-product-type/实际添加自定义产品,它可以正常工作 .

我没有代码示例,因为我不知道如何做到这一点 .

编辑#1 - 用户希望从插件中获取参考代码 .

这取自“class-wc-pre-orders-admin.php”

/** product hooks */

 // add 'Pre-Orders' product writepanel tab
    add_action( 'woocommerce_product_write_panel_tabs', array( $this, 'add_product_tab' ), 11 );

 // Add 'Pre-Orders' tab content
    add_action( 'woocommerce_product_write_panels', array( $this, 'add_product_tab_options' ), 11 );


 /** Functions **/
 <?php
 /**
 * Add 'Pre-Orders' tab to product writepanel
 *
 * @since 1.0
 */
  public function add_product_tab() {
 ?>
  <li class="wc_pre_orders_tab wc_pre_orders_options">
     <a href="#wc_pre_orders_data"><?php _e( 'Pre-Orders', WC_Pre_Orders::TEXT_DOMAIN ); ?></a>
  </li>
 <?php
}

无法找到过滤标签的位置,只需将其添加到打印的数组中即可 .

目前我没有过滤器“product_data_tabs”

编辑#2 - 添加了我的代码 .

/* STEP 1 */
function register_simple_custom_product_type() {

    class WC_Product_Simple_Custom_Product extends WC_Product_Simple {

        public function __construct( $product ) {

            $this->product_type = 'simple_custom';

            parent::__construct( $product );
        }
    }
}
add_action( 'init', 'register_simple_custom_product_type' );  



/* STEP 2 */
function add_simple_custom_product( $types ){

    $types[ 'simple_custom' ] = __( 'Simple custom' );

    return $types;
}
add_filter( 'product_type_selector', 'add_simple_custom_product' );  



/* STEP 3*/
function custom_product_tabs( $tabs) {

    $tabs['custom'] = array(
        'label'     => __( 'Custom', 'woocommerce' ),
        'target'    => 'custom_options',
        'class'     => array( 'show_if_simple_custom', 'show_if_variable_custom' ),   
);
  /* filter to show pre_order tab with this product-type */
    tabs['pre_order']['class'][] = 'show_if_simple_custom show_if_variable_custom';

return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'custom_product_tabs' );

但事实并非如此 .

  • 显示名为Custom的新选项卡 .

  • 当我在下拉字段中选择简单自定义作为产品类型时,显示预订选项卡 .

编辑#3 .

标记了正确的答案,这里是更正后的代码的链接http://pastebin.com/GM1UmDwC

1 回答

  • 0

    它不是一个钩子,它是javascript,它可以控制在更改产品类型时可见和不可见的内容 . 我现在无法访问预订插件,但是,如果您找到预订选项卡的正确类,则这些行中的某些内容应该有效 .

    jQuery( function($){
    
        $( 'body' ).on( 'woocommerce-product-type-change', function( event, select_val, select ) {
    
            if ( select_val === 'your-product-type' ) {
                 // modify to an actual CSS class selector
                $( '.some-class-selector-for-pre-order-tab' ).show();
            }
    
        } );
    
        $( 'select#product-type' ).change();
    
    } );
    

    或者,将'show-if-PRODUCT-TYPE'(其中PRODUCT-TYPE需要替换为 <select> 元素中的产品类型的值)类添加到预订选项卡可能更有效 . 如果内存,服务,那么当产品类型发生变化时,WooCommerce会自动显示它 .

    jQuery( function($){
        $( '.some-class-selector-for-pre-order-tab' ).add_class( 'show-if-YOUR-PRODUCT-TYPE' );
        $( 'select#product-type' ).change();
    } );
    

    EDIT #1

    我忘了过滤标签类了!这可能更简单,并且不需要任何其他脚本,因为正确的类,WooCommerce将自动处理它自己的脚本中的隐藏/显示 . 您必须已对此进行过滤以添加到您自己的自定义标签中,但同时您可以调整其他标签 .

    这是一个示例,直到我可以看到更多代码并可以编辑以进行相应匹配 .

    **
     * Add a custom product tab.
     */
    function custom_product_tabs( $tabs) {
    
        // This is where you register your product type's tab.
        $tabs['rental'] = array(
            'label'     => __( 'Rental', 'your-plugin-textdomain' ),
            'target'    => 'rental_options',
            'class'     => array( 'show_if_simple_rental'  ),
        );
    
        // Add an additional class to an existing tab, ex: Inventory.
        $tabs[ 'inventory' ][ 'class' ][] = 'show_if_simple_rental';
    
        return $tabs;
    }
    add_filter( 'woocommerce_product_data_tabs', 'custom_product_tabs' );
    

    Edit #2

    因为预订单是通过 woocommerce_product_write_panel_tabs 动作挂钩而不是通过 woocommerce_product_data_tabs 过滤器添加其选项卡,所以我之前的编辑赢得了't work. Pre-Orders is then using it'自己的脚本以切换可见性,您不希望处理脚本的优先级 . 事实证明Pre-Orders具有获取它支持的产品类型的功能,您可以通过其 wc_pre_orders_supported_product_types 过滤器添加自己的类型:

    function so_42253729_add_pre_order_support( $types ){
        $types[] = 'your-product-type';
        return $types;
    }
    add_filter( 'wc_pre_orders_supported_product_types', 'so_42253729_add_pre_order_support' );
    

相关问题