Woocommerce 自定义产品类型在更新时不更改类型

Woocommerce 自定义产品类型在更新时不更改类型,woocommerce,Woocommerce,我创建了一个自定义产品类型,允许我将供应商信息存储为产品元 到目前为止,所有内容都正确显示和加载,属性甚至保存,但是,无论产品是什么,当我点击“保存”时,总是返回到“简单产品” 到目前为止,我完全没有找到解决方案,我是否需要另一个函数,或者我是否在其他地方犯了错误 我有两个文件: inc/xxx_product_type.php-包含产品类型的功能 inc/register\u xxx\u product\u type.php-注册类 这两个文件在主插件文件中用require_调用一次 xx

我创建了一个自定义产品类型,允许我将供应商信息存储为产品元

到目前为止,所有内容都正确显示和加载,属性甚至保存,但是,无论产品是什么,当我点击“保存”时,总是返回到“简单产品”

到目前为止,我完全没有找到解决方案,我是否需要另一个函数,或者我是否在其他地方犯了错误

我有两个文件:

  • inc/xxx_product_type.php-包含产品类型的功能
  • inc/register\u xxx\u product\u type.php-注册类
这两个文件在主插件文件中用require_调用一次

xxx_product_type.php

/* Add custom product type for Xxx Computers*/
function add_xxx_products( $types ){
    $types[ 'xxx_product' ] = __( 'Xxx Computers Product', 'xxx_product' );
    return $types;  
}
add_filter( 'product_type_selector', 'add_xxx_products' );

/* Add options tab for Xxx Computers products */
function xxx_product_tab( $tabs) {
    $tabs['xxx_product'] = array(
        'label'  => __( 'Xxx Product Data', 'xxx_product' ),
        'target' => 'xxx_product_options',
        'class'  => 'show_if_xxx_product');
    return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'xxx_product_tab' );

/* Contents of the xxx_product options product tab. */
function xxx_product_tab_content() {

    global $post;

    ?><div id='xxx_product_options' class='panel woocommerce_options_panel'><?php

    ?><div class='options_group'><?php

    woocommerce_wp_text_input( array(
        'id' => '_product_code',
        'label' => __( 'Xxx Product Code', 'woocommerce' ),
        'placeholder' => '',
        'desc_tip' => 'true',
        'description' => __( "Enter Xxx's product code, it can be found underneath the product's title.", 'woocommerce' ),
        'type' => 'text')
    );

    woocommerce_wp_checkbox( array(
        'id'        => '_enable_margin',
        'label'     => __( 'Ignore Margin', 'woocommerce' ),
        'desc_tip' => 'true',
        'description' => __( "When ticked, the pricing will be fixed and will not change.", 'woocommerce' ),
            ) );

    woocommerce_wp_select( array( 
        'id'      => '_margin_type', 
        'label'   => __( 'Margin Type', 'woocommerce' ),
        'desc_tip' => 'true',
        'description' => __( "Variable: A percentage of the cost price added on top of the cost price.<br>Fixed: A dollar value added on top of the cost price.", 'woocommerce' ),
        'options' => array(
            'variable'   => __( 'Variable', 'woocommerce' ),
            'fixed'   => __( 'Fixed', 'woocommerce' ))
    ));

    woocommerce_wp_text_input( array(
        'id' => '_product_margin',
        'label' => __( 'Product Margin', 'woocommerce' ),
        'placeholder' => '15',
        'desc_tip' => 'true',
        'description' => __( "Depending on the type selected, either enter a fixed value or a percentage value.", 'woocommerce' ),
        'type' => 'number', 
        'custom_attributes' => array(
            'step'  => 'any',
            'min'   => '0')
    ));

    ?></div>

    </div><?php


}
add_action( 'woocommerce_product_data_panels', 'xxx_product_tab_content' );


/* Save the custom fields. */
function save_xxx_product_option_field( $post_id ) {
    $product_code = $_POST['_product_code'];
    $enable_margin = $_POST['_enable_margin'];
    $margin_type = $_POST['_margin_type'];
    $product_margin = $_POST['_product_margin'];
        
    if( !empty( $product_code ) ) {
        update_post_meta( $post_id, '_product_code', esc_attr( $product_code ) );
    }   
    if( !empty( $enable_margin ) ) {
        update_post_meta( $post_id, '_enable_margin', esc_attr( $enable_margin ) );
    }   
    if( !empty( $margin_type ) ) {
        update_post_meta( $post_id, '_margin_type', esc_attr( $margin_type ) );
    }   
    if( !empty( $product_margin ) ) {
        update_post_meta( $post_id, '_product_margin', esc_attr( $product_margin ) );
    }
}
add_action( 'woocommerce_process_product_meta', 'save_xxx_product_option_field'  );

/* Show general tab for xxx_product. */
function xxx_product_custom_js() {
    if ( 'product' != get_post_type() ) :
        return;
    endif;
    ?><script type='text/javascript'>
        jQuery( document ).ready( function() {
            jQuery( '.options_group.pricing' ).addClass( 'show_if_xxx_product' ).show();
        });
        </script><?php
    }
    add_action( 'admin_footer', 'xxx_product_custom_js' );

/* Hide uneccesary data panels. */
function hide_attributes_data_panel( $tabs) {

    if ( 'product' != get_post_type() ) :
        return;
    endif;

    $tabs['attribute']['class'][] = 'hide_if_xxx_product';
    $tabs['advanced']['class'][] = 'hide_if_xxx_product';
    return $tabs;
}
add_filter( 'woocommerce_product_data_tabs', 'hide_attributes_data_panel' );
/* Register the custom product type after init */
function register_xxx_product_type() {
    class WC_Product_Xxx extends WC_Product_Simple {
        public function __construct( $product ) {
            $this->product_type = 'xxx_product';
            parent::__construct( $product );
        }
    }
}
add_action( 'init', 'register_xxx_product_type' );