2017-07-06 86 views
0

WooCommerce產品易於創建,但該產品已添加到product_type:簡單。我想將產品添加到如何使用php代碼將woocommerce產品添加到自定義Product_type?

static function createTicket($postId, $productDetails) { 
$id = wp_insert_post(array(
    'post_type' => 'product', 
    'post_title' => $productDetails['title'], 
    'post_content' => $productDetails['description'], 
    'post_status' => get_post_status($postId), 
     'max_value' => 1, 
)); 
update_post_meta($id, '_price', $productDetails['price']); 
update_post_meta($id, '_regular_price', $productDetails['price']); 
update_post_meta($id, '_wpws_testID', $postId); 
update_post_meta($id, '_sold_individually', "yes"); 
wp_set_object_terms($id, 'test', 'product_type'); 
return $id; 
} 

我已經加入

wp_set_object_terms 「定製產品類型測試」($ ID, '測試', '產品類型');

但沒有任何工程

enter image description here

回答

1

使用product_type_selector

// add a product type 
add_filter('product_type_selector', 'add_custom_product_type'); 
function add_custom_product_type($types){ 
    $types[ 'test_custom_product_type' ] = __('test Product'); 
    return $types; 
} 

編輯

請檢查下面的鏈接它HEL p你更多關於這個

編輯

以上代碼的舊版本WooCommerce

0123的

使用此代碼可以保存具有自定義產品類型的產品。它與WooCommerce 3. * 檢查工作爲這我已與這些步驟完成更多的信息https://gist.github.com/stirtingale/753ac6ddb076bd551c3261007c9c63a5

function register_simple_rental_product_type() { 
    class WC_Product_Simple_Rental extends WC_Product { 

     public function __construct($product) { 
      $this->product_type = 'simple_rental'; 
      parent::__construct($product); 
     } 
    } 
} 
add_action('init', 'register_simple_rental_product_type'); 
function add_simple_rental_product($types){ 
    // Key should be exactly the same as in the class 
    $types[ 'simple_rental' ] = __('Simple Rental'); 
    return $types; 
} 
add_filter('product_type_selector', 'add_simple_rental_product'); 
+0

。自定義產品類型顯示在product_type下拉菜單下。現在我想將一個新產品分配給後端的自定義產品類型 – Himani

+0

我該如何實現這一目標? – Himani

+0

@Himani不遵循你所說的話? –

相關問題