使用 MySQL 为特定产品类别的产品名称添加前缀

Using MySQL to prepend product names from a specific product category

在 WooCommerce 中,我想在产品名称中添加几个字符,以便它们与客户的 QB SKU 相匹配。

相关产品均属于同一产品类别。我希望有一个 mySql query/command 我可以调整来做像这个可悲的伪代码这样的事情:

in table wp_posts where category = "a product category" PREPEND "abc_" to product name.

有什么想法吗?

谢谢。

更新:我不是 SQL 专家,但它肯定会帮助您了解如何构建完整的 SQL 查询。

At the end below, you will find a custom function that you will run one time, that will do the job.

  1. 获取您的产品类别 ID……
  2. 在"wp_term_taxonomy"table中勾选对应的'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 文件或任何插件文件。

此代码已经过测试并且有效。

您按此操作以获取当前产品类别 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 文件