How to add a new product type on woocommerce product types?

I want to add a new custom “product type” to woocommerce plugin:

enter image description here

Tried to duplicate one of currently exist product type files (woocommerce template structure) as a new file (file name and inside commented name) but not worked!

enter image description here

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The add to cart template is only 1 of the many things you’ll need to do. Each product type has it’s own class in the /includes/ folder. Each one extends the WC_Product class.

To add items to the list you’ve screencapped (which is on the admin side and not the front-end, unlike the add-to-cart.php template, you will need to filter product_type_selector.

add_filter( 'product_type_selector', 'wpa_120215_add_product_type' );
function wpa_120215_add_product_type( $types ){
    $types[ 'your_type' ] = __( 'Your Product Type' );
    return $types;
}

then you’ll need to declare your product class. The standard naming system is WC_Product_Type_Class so in this example it would be:

class WC_Product_Your_Type extends WC_Product{
    /**
     * __construct function.
     *
     * @access public
     * @param mixed $product
     */
    public function __construct( $product ) {
        $this->product_type = 'your_type'; // Deprecated as of WC3.0 see get_type() method
        parent::__construct( $product );
    }

     /**
     * Get internal type.
     * Needed for WooCommerce 3.0 Compatibility
     * @return string
     */
    public function get_type() {
        return 'your_type';
    }
}

You are asking a very complicated question and I can’t provide a more complete answer. Hopefully this sets you on the right path. I highly encourage you to read the code in WooCommerce. It is very well commented and you can see how they are handling the different product types.

Edit Added WC3.0 compatibility to product type class.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x