2017-04-16 76 views
1

在WooCommerce中,我想在產品名稱中添加幾個字符,以便它們與客戶端的QB SKU相匹配。使用MySQL在特定產品類別中添加產品名稱

有問題的產品都屬於同一產品類別。我希望有一個mySql查詢/命令,我可以做這樣的事情,像這個可憐的僞代碼:

in table wp_posts where category =「a product category」PREPEND「abc_」to product name。

有什麼想法?

謝謝。

+0

它似乎也沒有在wp_posts表中指出這些類別......這使得混淆lol。 –

+0

如果您有一個屬於'Cat1'的產品'ProA',那麼它應該重命名爲'Cat1-ProA'?以及如果產品屬於多個類別會怎樣。說'ProB'屬於'Cat1'和'Cat2',那麼這個案例的產品名稱是什麼? –

+0

我想要的是,例如,將「abc_」添加到所有帶有catergory的產品的product_name中,例如屬於多個類別的「xyz」產品是無關緊要的,因爲我基於單個類別進行更改...產品名稱會改變,但類別/類別不會。 –

回答

1

更新:我不是SQL專家,但它肯定會幫助您瞭解如何構建完整的SQL查詢。

在下面的結尾處,您會發現一個自定義函數,您將運行一次,它將完成這項工作。

  1. 讓您的產品類別ID ......在「wp_term_taxonomy」表
  2. 檢查相應的「term_taxonomy_id」(一般相同產品類別ID)。

這裏是讓產品ID的數組,這個特定類別的方式:

global $wpdb; 

// Define HERE the corresponding 'term_taxonomy_id' of your product category. 
$term_taxonomy_id = 11; 

$table_name = $wpdb->prefix . "term_relationships"; 
$product_ids = $wpdb->get_col(" 
    SELECT object_id 
    FROM $table_name 
    WHERE `term_taxonomy_id` = $term_taxonomy_id 
    ORDER BY $table_name.object_id ASC 
"); 

測試和工程


與該代碼可以使一個函數,去實現你想做的事:

function prepending_products_names(){ 

    global $wpdb; 

    // Define HERE the corresponding 'term_taxonomy_id' of your product category. 
    $term_taxonomy_id = 11; 

    $table_name = $wpdb->prefix . "term_relationships"; 
    $product_ids = $wpdb->get_col(" 
     SELECT object_id 
     FROM $table_name 
     WHERE `term_taxonomy_id` = $term_taxonomy_id 
     ORDER BY $table_name.object_id ASC 
    "); 

    foreach($product_ids as $product_id){ 
     // get the post object 
     $post_obj = get_post($product_id); 
     // get the product sku 
     $product_sku = get_post_meta($product_id, '_sku', true); 
     // Customizing the product name 
     // (You can prepend the a part of the sku to it) 
     $custom_product_name = 'abc_'. $post_obj->post_title; 

     // Updating the product name 
     wp_update_post(array(
      'id' => $product_id, 
      'post_title' => $custom_product_name, 
     )); 
    } 
} 

// Run this one time reloading a backend page and comment it 
// (or remove the code aftewards). 

if(is_admin()) 
    prepending_products_names(); 

代碼在你的活動子主題(或主題)的function.php文件中,或者也在任何插件文件中。

此代碼已經過測試並可正常工作。

+0

LoicTheAztec - 這會更新實際的數據庫嗎?我把它放在function.php中是好的? –

+1

@BrockVond是的,我的函數將更新數據庫:wp_update_post()WordPress函數。但在**之前做一個數據庫備份或張貼表的備份** ...這樣您就可以安全地測試它。使用後(一次)將其刪除。這個函數會在你的網站的一個頁面被瀏覽時運行(所以要小心)...你可以添加一個條件並使用is_admin()從後端運行它(更安全)... – LoicTheAztec

+0

好的,所以這段代碼如果留在函數中。如果我在第一次運行後不刪除它,php會不斷地爲項目重複添加一個前綴? is_admin()在哪裏呢?我是管理員,並且只希望它從後端運行(甚至一次)。再次感謝您的所有幫助......我將在今天/今晚進行測試。 –

1

你喜歡這款獲得當前的產品類別ID

global $post; 
$terms = get_the_terms($post->ID, 'product_cat'); 
foreach ($terms as $term) { 
    $category_id = $term->term_id; 
    break; 
} 

然後用它來設置基於產品類別的價值。

if ($category_id == "1"){ 
     $sku = "your_value_if_current_cat_id_is_2"; 
}elseif ($category_id == "2"){ 
     $sku = "your_value_if_current_cat_id_is_2"; 
}else{ 
     $sku = "your_value_for_all_other_categories"; 
} 

現在,讓我們一起把這個和鉤的功能與可變$sku

remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_title', 5); 
add_action('woocommerce_single_product_summary', 'woocommerce_my_single_title',5); 

function woocommerce_my_single_title() { 

global $post; 
$terms = get_the_terms($post->ID, 'product_cat'); 
foreach ($terms as $term) { 
    $category_id = $term->term_id; 
    break; 
} 

if ($category_id == "1"){ 
     $sku = "your_value_if_current_cat_id_is_2"; 
}elseif ($category_id == "2"){ 
     $sku = "your_value_if_current_cat_id_is_2"; 
}else{ 
     $sku = "your_value_for_all_other_categories"; 
} 
     ?> 
      <h1 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h1>-<h2><?php echo $sku; ?></h2> 
     <?php 
    } 

您選擇的值附加的一個新的標題替換當前的標題把函數在functions.php文件

+0

真棒的答案,但我期待預先,而不是取代SKU /產品名稱。感謝壽。 –

+1

@BrockVond這不是替代品,它會將SKU附加到產品名稱上,一旦您刪除此代碼,它將會恢復原樣。 –

相關問題