7

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

closed as off-topic by Rarst Feb 10 '14 at 9:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

18

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_product';
    }
}

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.

  • For people from ✨ the future ✨ finding this, here's a blog post that might provide some helpful additional information: jeroensormani.com/adding-a-custom-woocommerce-product-type – Travis Northcutt Dec 29 '16 at 17:49
  • Love that! But for people of the future, the $this->product_type will not be accurate for WC2.7+. – helgatheviking Dec 29 '16 at 19:11
  • @helgatheviking Any points as to where to find more up to date info in particular about the product_type? I keep bumping into those "old" tutorials using only that syntax. – kontur Sep 1 '17 at 6:53
  • 1
    @kontur you will need to add a get_type() method to your class. It should return the product type. ex: public function get_type() { return 'your_type'; } See my edit. – helgatheviking Sep 1 '17 at 19:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.