2017-04-24 58 views
1

最近更新Woocommerce到3.0之后,我有问题来保存我创建的自定义产品类型。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'); 

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

谢谢!

UPDATE

现在我的代码看起来象

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

对不起,我很慢,但请你再试一次再试一次给我解释一下。

谢谢!

+0

没有,'$ PRODUCT_TYPE ==“NameOfType''应与product_type_selector'$ type'变量..而不是你创建的类......请我的回答再次...你这样做错误.. – Reigel

+0

奥基,我已经改变了它,但仍然没有工作... – Daniel

+0

那么你不这样做,因为我在我的答案如下。 – Reigel

回答

0

这是怎么做到的。

首先确保你的班级延伸到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); 
+0

嗨,谢谢你, – Daniel

+0

你是否按照原样尝试了我的代码? – Reigel