Custom Product Type Not Changing Type on Update

Viewed 59

I have created a custom product type to allow me to store supplier information as product meta.

So far everything displays and loads correctly, the attributes even save, however, no matter what the product always returns to Simple Product when I hit save.

I've so far completely failed to find a solution, do I need another function, or have I made an error somewhere else along the line?

I have two files:

  • inc/xxx_product_type.php - containing the functionality of the product type.
  • inc/register_xxx_product_type.php - Registers the class.

Both of these files are called with require_once in the main plugin file.

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_xxx_product_type.php

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