使用sql查询更改产品类别名称

r6l8ljro  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(378)

我想更新在wordpress,许多产品类别名称,检索他们从csv文件。
csv文件头只是:(id;产品类别(名称)
我有900个类别和子类别,层次结构。
是否可以在数据库中按id搜索类别并更新类别名称?
这能保持等级结构正确吗?
我能在名字里加上一些字符吗ö' 'ä' 或'å' 在utf-8中?我可以使用wp php函数或直接sql命令。

oxalkeyp

oxalkeyp1#

这个答案回答了您的问题标题,但不是所有其他问题(请参阅如何提问)。
可以使用以下sql查询(在之前进行数据库备份):

UPDATE wp_terms as a
    JOIN wp_term_taxonomy b ON a.term_id = b.term_id
    SET a.name = 'new_name', 
        a.slug = 'new_slug'
    WHERE b.taxonomy = 'product_cat'
    AND a.name = 'old_name'

需要更换的地方: new_name 按您的新产品类别名称 new_slug 按您的新产品类别slug(小写和“ - “替换空格) old_name 按旧产品类别名称(要替换的名称)
也可以对同一sql查询使用以下函数:

function rename_product_category( $old_name, $new_name ){
    global $wpdb;

    // Check that the new name doesn't exist
    if( term_exists( $new_name, 'product_cat' ) )
        return __("Your new product category term name already exist");

    // Check that the old name exist
    if( ! term_exists( $old_name, 'product_cat' ) )
        return __("Your old product category term name doesn't exist");

    $new_slug = sanitize_title( $new_name );

    $result = $wpdb->query("
        UPDATE {$wpdb->prefix}terms as a
        JOIN {$wpdb->prefix}term_taxonomy b ON a.term_id = b.term_id
        SET a.name = '$new_name',
            a.slug = '$new_slug'
        WHERE b.taxonomy = 'product_cat'
        AND a.name = '$old_name'
    ");

     if($result)
        return sprintf(
            __("The product category %s has been renamed to %s."),
            '"<strong>' . $old_name . '</strong>"',
            '"<strong>' . $new_name . '</strong>"'
        );
    else
        return __("Something is wrong!.");
}

代码放在活动子主题(或活动主题)的function.php文件中。
用法(假设您将“clothing”产品类别重命名为“wear”):

echo rename_product_category( 'Clothing', 'Wear' );

它将显示产品类别是否已重命名。测试和工作。

相关问题