Sksudip
Sksudip

Reputation: 589

Add custom product type in woocommerce

I am using Woocommerce, I wanted to create a custom product type but I didn't get the way

In Woocommerce there are four product type like

 Simple product
 variable product
 grouped product and
 External/Affiliate product

I need another custom product type there. is there any way to create a custom product type?

Upvotes: 2

Views: 6562

Answers (2)

Faysal Haque
Faysal Haque

Reputation: 410

I'm also trying this answer helped.

Add the following to your admin scripts:

add_filter( 'product_type_selector' , array( $this, 'wpa_120215_add_product_type' ) );

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

This will show "Your Product Type" in the product data select box in the admin dashboard.

Next create a file with this to actually create the product:

class WC_Product_Your_Type extends WC_Product{
  /**
   * __construct function.
   *
   * @access public
   * @param mixed $product
   */
  public function __construct( $product ) {
    $this->product_type = 'your_type';
    parent::__construct( $product );
  }
}

And to display your custom template in the front end add this:

define( 'YOUR_TEMPLATE_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' );
add_action('woocommerce_your_type_add_to_cart', array($this, 'add_to_cart'),30);
public function add_to_cart() {
    wc_get_template( 'single-product/add-to-cart/your_type.php',$args = array(), $template_path = '', YOUR_TEMPLATE_PATH);

}

Upvotes: 6

Faysal Haque
Faysal Haque

Reputation: 410

for comment answer...

 your-plugin/
    includes/
       admin/

In the admin folder: class-your-type-admin.php

<?php
    class Your_Type_Admin {
        public function __construct() {
            add_filter( 'product_type_selector' , array( $this, 'product_type_selector_one'));
        }

        public function product_type_selector_one( $types ) {
            $types[ 'your_type' ] = __( 'Your Type product' );
            return $types;
       }}

new Your_Type_Admin();

then include this file on you plugin __construct file

Upvotes: 0

Related Questions