使用MySQL在特定产品类别中添加产品名称

问题描述:

在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”产品是无关紧要的,因为我基于单个类别进行更改...产品名称会改变,但类别/类别不会。 –

更新:我不是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()在哪里呢?我是管理员,并且只希望它从后端运行(甚至一次)。再次感谢您的所有帮助......我将在今天/今晚进行测试。 –

你喜欢这款获得当前的产品类别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附加到产品名称上,一旦您删除此代码,它将会恢复原样。 –