customproducttype

In this post, I will be using amazing examples of how to create a My Custom Product types.

Register a new class

To get started you first need to create a class that is extending the existing WC_Product class. It is vital to have the product class named the same as in the classes’ parameter product_type, (‘same’ as in the same name, each first letter of the class name should be capitalized and should have underscore separators). It is vital because WooCommerce uses the product type to get the class name and if it’s called differently it will not be recognized.

In my pattern, I called my class WC_Product_Simple_Myproduct.
[cc lang=”php”]
/**
* Register the custom product type after init
*/
function register_simple_myproduct_type() {
/**
* This should be in its own separate file.
*/
class WC_Product_Simple_Myproduct extends WC_Product_Simple {

public function __construct( $product ) {

$this->product_type = ‘myproduct’;

parent::__construct( $product );
}
}
}
add_action( ‘init’, ‘register_simple_myproduct_type’ );
[/cc]
On itself, this doesn’t do much as you will not see it in the drop down when you select a product type. To add it do the product type drop down, use the following code.
[cc lang=”php”]
function add_myproduct_product( $types ){

// Key should be exactly the same as in the class product_type parameter
$types[ ‘myproduct’ ] = __( ‘My Product’ );

return $types;

}
add_filter( ‘product_type_selector’, ‘add_myproduct_product’ );
[/cc]
That’s it! the most basic form of adding a new product type! You will now be able to select your new product type from the drop-down on the edit product pages.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.