2016-11-18 67 views
0

我正在開發woocommerce產品Convert.I將帖子轉換爲woocommerce產品,點擊進口button.Convert工作正常,但我創建了與帖子中相同的類別,我需要導入與帖子相同類別的產品。例如我在帖子中有類別。如何將帖子轉換爲有類別的產品?

風格 - 主貓 男裝 - 子貓 服裝 - 男士子貓 恤 皮帶 女性 - 子貓 服裝 - 女裝子貓 頂 錢包

,我已經創建woocommerce中的相同類別。現在,當我輸入帖子時,如果帖子在女裝類別中,那麼進口產品應該去女裝類別。產品應根據類別和子類別進行導入。這是我將代碼轉換爲產品的代碼。

foreach ($items as $post) { 
    $post_thumbnail_id = get_post_thumbnail_id($post->ID);     
    $getpostid = $wpdb->get_results(" 
     SELECT post_id 
     FROM " . $wpdb->prefix ."postmeta AS pm 
     INNER JOIN ".$wpdb->prefix."posts AS p 
      ON p.ID=pm.post_id 
     WHERE pm.`meta_key` = 'admitted_goods_id' 
     AND pm.`meta_value` ='".$post->ID."' 
     AND p.post_status='publish' 
    "); 

    foreach($getpostid as $k=>$postdata) { 
     $postId    = $postdata->post_id; 
     $admited_goodsId[$k] = get_post_meta($postId, "admitted_goods_id", true); 
    } 

    if($post->ID==$admited_goodsId[0]) { 
     echo "Updated"; 
    } else { 
     //echo "Insert";   
     $post_id = wp_insert_post(array(
      'post_title' => $post->post_title, 
      'post_content' => $post->post_content, 
      'post_status' => 'publish', 
      'post_type' => "product", 
     )); 

     wp_set_object_terms($post_id, 'external', 'product_type'); 
     update_post_meta($post_id, '_visibility', 'visible'); 
     update_post_meta($post_id, '_downloadable', 'no'); 
     update_post_meta($post_id, '_thumbnail_id',$post_thumbnail_id); 
     update_post_meta($post_id, 'admit_ds_id',$post->ID); 
     update_post_meta($post_id, '_stock_status', 'instock'); 
     update_post_meta($post_id, '_button_text','Buy Now'); 
     update_post_meta($post_id, '_brand', $pdata->vendor); 
     update_post_meta($post_id, '_model', $pdata->model); 

     $term_list = wp_get_post_terms($post->ID, 'adm_category', array("fields" => "all")); 
     foreach($term_list as $term) { 
      wp_set_post_terms($post_id, $term->name, 'product_cat', false); 
     } 

     $getOtherdata = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix ."admitad_product_data where post_id=".$post->ID); 

     foreach($getOtherdata as $pdata) { 
      //echo $pdata->price; 
      update_post_meta($post_id, '_regular_price', $pdata->old_price); 
      update_post_meta($post_id, '_sale_price', $pdata->price); 
      update_post_meta($post_id, '_product_url',$pdata->url); 
      update_post_meta($post_id, '_price', $pdata->price); 
      update_post_meta($post_id, '_currencyId', $pdata->currencyId); 
     } 
    } 
} 

回答

0

有在你的代碼的兩個錯誤,我固定請檢查以下內容:

<?php 

foreach ($items as $post) { 

     $post_thumbnail_id = get_post_thumbnail_id($post->ID); 

     $getpostid = $wpdb->get_results("SELECT post_id FROM " . $wpdb->prefix ."postmeta as pm inner join ".$wpdb->prefix."posts as p on p.ID=pm.post_id WHERE pm.`meta_key` = 'admitted_goods_id' AND pm.`meta_value` ='".$post->ID."' and p.post_status='publish' "); 
     foreach($getpostid as $k=>$postdata) 
     { 
      $postId = $postdata->post_id; 
      $admited_goodsId[$k] = get_post_meta($postId, "admitted_goods_id", true); 
     } 

     if($post->ID==$admited_goodsId[0]) 
     { 
      echo "Updated"; 


     }else 
     { 
      //echo "Insert"; 

      $post_id = wp_insert_post(array(
      'post_title' => $post->post_title, 
      'post_content' => $post->post_content, 
      'post_status' => 'publish', 
      'post_type' => "product", 
      )); 
      wp_set_object_terms($post_id, 'external', 'product_type'); 
      update_post_meta($post_id, '_visibility', 'visible'); 
      update_post_meta($post_id, '_downloadable', 'no'); 
      update_post_meta($post_id,'_thumbnail_id',$post_thumbnail_id); 
      update_post_meta($post_id,'admit_ds_id',$post->ID); 
      update_post_meta($post_id, '_stock_status', 'instock'); 
      update_post_meta($post_id,'_button_text','Buy Now'); 
      update_post_meta($post_id, '_brand', $pdata->vendor); 
      update_post_meta($post_id, '_model', $pdata->model); 





      //$term_list = wp_get_post_terms($post->ID, 'adm_category', array("fields" => "all")); 
      $term_list = wp_get_post_categories($post->ID); 
      // $term_list variable is an array containing IDs of all the post categories 

      // foreach($term_list as $term) 
      // { 

      //  wp_set_post_terms($post_id, $term->name, 'product_cat', false); 

      // } 
      // wp_set_post_term can't be used to update Product Category/Terms since this function can only be used to update Terms of native post type not custom post type. In order to update Terms of custom post type which is Products in our case we need to use wp_set_object_terms function. 




      wp_set_object_terms($post_id, $term_list, 'product_cat'); 




      $getOtherdata = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix ."admitad_product_data where post_id=".$post->ID); 
      foreach($getOtherdata as $pdata) 
      { 
       //echo $pdata->price; 
       update_post_meta($post_id, '_regular_price', $pdata->old_price); 
       update_post_meta($post_id, '_sale_price', $pdata->price); 
       update_post_meta($post_id,'_product_url',$pdata->url); 
       update_post_meta($post_id, '_price', $pdata->price); 
       update_post_meta($post_id, '_currencyId', $pdata->currencyId); 
      } 

    } 
} 

?> 
+0

wp_get_post_categories()返回空array.I正在使用自定義後類型 –

+0

$ term_list = get_the_term_list($後> ID,'admitad_category');我用這個來獲得術語表。它是插入產品分類但是如果男女貓有配件貓並且在配件中有袋貓然後它將男士類包包裝入女士類包包產品中。 –

+0

好的嘗試使用wp_set_object_terms函數而不是wp_set_post_terms併爲其提供一個類別ID數組。 – Vineet

相關問題