Woocommerce(3.0)产品类型不会保存

时间:2017-04-24 08:29:32

标签: woocommerce

最近更新的Woocommerce到3.0,之后我有问题保存我创建的自定义产品类型。

这就是代码现在的样子。

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' );

有没有人解决过这个问题?

谢谢!

更新

现在我的代码看起来像这样

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 );

但是没有用。我做错了什么?

更新#2

哦,这就是现在的样子。

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 );

很抱歉我很慢但请你再试一次,为我解释一下。

谢谢!

1 个答案:

答案 0 :(得分:1)

这是怎么做的。

首先确保延伸到WC_Product的类隐藏在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' );

然后添加您的产品类型。

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' );

然后将您创建的类用于您的产品类型 如果你没有这个,那么你将被困在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 );