2017-04-24 57 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