dongxi1943 2017-02-15 12:08
浏览 42
已采纳

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

I want Woocommerce pre-order plugin to add itself to my custom product type i have created. If i choose Woocommerce standard product types i get a pre-order tab.

Is there a hook to pass in my "custom product type" to the plugin so it knows that it should be visible?

I have follow this guide: http://jeroensormani.com/adding-a-custom-woocommerce-product-type/ to actually add the custom product and it works.

I dont have a code sample because i dont know how this could be done .

EDIT #1 - User wanted to have reference code from the plugin .

This is taken from the "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
}

Cant find where they filter the tabs, just adds it to the array that prints.

At the moment i do not have a filter on the hook "product_data_tabs"

EDIT #2 - Added my code .

/* 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' );  

But this does not .

  1. show a new tab with the name of Custom .

  2. Show the pre-order tab when i select Simple custom as product-type in the drop-down field .

EDIT #3 .

Correct answer is marked and here is link to corrected code http://pastebin.com/GM1UmDwC

  • 写回答

1条回答 默认 最新

  • duanchandun1860 2017-02-15 15:38
    关注

    It's not a hook, it's javascript, that controls what is and is not visible as you change product types. I don't have access to the pre order plugin right now, but, something along these lines should work if you find the correct class for the pre-order tab.

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

    Alternatively, it may be more efficient to add a 'show-if-PRODUCT-TYPE' (where PRODUCT-TYPE needs to be replaced with the value of your product type from the <select> element) class to the pre-order tab. If memory, serves, then WooCommerce will automatically show it when the product type is changed.

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

    EDIT #1

    I forgot about filtering the tab classes! This is probably simpler and doesn't require any additional scripts as with the right classes, WooCommerce will handle the hide/show from it's own script automatically. You must already be filtering this to add in your own custom tab, but at the same time you can adjust the other tabs.

    Here's an example until I can see more of your code and can edit to match accordingly.

    **
     * 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

    Because Pre Orders is adding their tab via the woocommerce_product_write_panel_tabs action hook instead of via the woocommerce_product_data_tabs filter, my earlier edit won't work. Pre-Orders is then using it's own script to toggle visibility and you don't want to get into dealing with priority of scripts. Turns out that Pre-Orders has a function for getting product types that it supports and you can add your own type via its wc_pre_orders_supported_product_types filter:

    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' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致