Search code examples
woocommerce

Woocommerce (3.0) Product type won't save


Recently updated Woocommerce to 3.0 and after that i having problem to save my custom product type that i have created.

This is what the code look like now.

function register_xxxxxx_product_type() {

    class WC_Product_package extends WC_Product {
    public function __construct( $product ) {
        $this->product_type = 'xxxxxx';
        parent::__construct( $product );
    }
  }
}

add_action( 'plugins_loaded', 'register_xxxxxxx_product_type' );

function add_xxxxxx_package_product( $types ){
   // Key should be exactly the same as in the class
   $types[ 'xxxxxx' ] = __( 'xxxxxx' );
   $types[ 'xxxxxx' ] = __( 'xxxxxx' );
   $types[ 'xxxxxx' ] = __( 'xxxxxx' );
return $types;

}

add_filter( 'product_type_selector', 'add_xxxx_package_product' );

are there anyone who have solved this?

Thanks!

UPDATE

Now my code look like this

function register_xxxxxx_product_type() {

class WC_Product_package extends WC_Product {

    public $product_type = 'NameOfType';
    public function __construct( $product ) {
        parent::__construct( $product );
    }
  }
}

 add_action( 'init', 'register_xxxxxx_product_type' );


  function add_xxxxxx_package_product( $types ){
 // Key should be exactly the same as in the class
 $types[ 'xxxxxx_package' ] = __( 'xxxxxx Paket' );
 $types[ 'xxxxxx_parts' ] = __( 'xxxxxx Tillbehör' );
 $types[ 'xxxxxx_service' ] = __( 'xxxxxx Tillvalstjänster' );
 return $types;

 }

 add_filter( 'product_type_selector', 'add_xxxxxx_package_product' );


 function woocommerce_product_class( $classname, $product_type ) {

 if ( $product_type == 'NameOfType' ) { // notice the checking here.
$classname = 'WC_Product_package';
 }

 return $classname;
 }

add_filter( 'woocommerce_product_class', 'woocommerce_product_class', 10, 2 );

But is not working. What am i doing wrong?

UPDATE #2

Okey, this is how it looks like now.

function register_daniel_product_type() {

class WC_Product_package extends WC_Product {

    public $product_type = 'daniel';
    public function __construct( $product ) {
        parent::__construct( $product );
    }
  }
}


add_action( 'init', 'register_daniel_product_type' );


function add_daniel_package_product( $types ){

// Key should be exactly the same as in the class

$types[ 'daniel_package' ] = __( 'Daniel Paket' );
$types[ 'daniel_parts' ] = __( 'Daniel Tillbehör' );
$types[ 'daniel_service' ] = __( 'Daniel Tillvalstjänster' );
return $types;
}

add_filter( 'product_type_selector', 'add_daniel_package_product' );

function woocommerce_product_class( $classname, $product_type ) {

if ( $product_type == 'daniel_package' ) { // notice the checking here.
    $classname = 'WC_Product_package';
}
return $classname;
}

add_filter( 'woocommerce_product_class', 'woocommerce_product_class', 10, 2 );

Sorry that i am slow but would you please give it a try one more time to explain it for me.

Thanks!!


Solution

  • this is how to do it.

    first make sure your class that extends to WC_Product is hooked on init.

    function register_xxxxxx_product_type() {
    
        class WC_Product_package extends WC_Product {
    
            public $product_type = 'xxxxxx';
            public function __construct( $product ) {
                parent::__construct( $product );
            }
        }
    }
    
    add_action( 'init', 'register_xxxxxx_product_type' );
    

    then add your product type.

    function add_xxxx_package_product( $types ){
       // Key should be exactly the same as in the class
       $types[ 'xxxxxx' ] = __( 'xxxxxx' );
    return $types;
    
    }
    
    add_filter( 'product_type_selector', 'add_xxxx_package_product' );
    

    then use your created class for your product type.
    If you don't have this, then you'll be stuck on WC_Product_Simple.

    function woocommerce_product_class( $classname, $product_type ) {
    
        if ( $product_type == 'xxxxxx' ) { // notice the checking here.
            $classname = 'WC_Product_package';
        }
    
        return $classname;
    }
    
    add_filter( 'woocommerce_product_class', 'woocommerce_product_class', 10, 2 );