最近将 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' );
Run Code Online (Sandbox Code Playgroud)
有没有人解决过这个问题?
谢谢!
更新
现在我的代码看起来像这样
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 );
Run Code Online (Sandbox Code Playgroud)
但不工作。我究竟做错了什么?
更新 #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 );
Run Code Online (Sandbox Code Playgroud)
对不起,我很慢,但请你再试一次,为我解释一下。
谢谢!!
这是怎么做的。
延伸到首先确保你的类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' );
Run Code Online (Sandbox Code Playgroud)
然后添加您的产品类型。
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' );
Run Code Online (Sandbox Code Playgroud)
然后将您创建的类用于您的产品类型。
如果你没有这个,那么你会被困在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 );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1560 次 |
| 最近记录: |