gpt4 book ai didi

php - 使用 SQL 查询更改 Woocommerce 产品类别名称

转载 作者:行者123 更新时间:2023-11-29 16:38:13 24 4
gpt4 key购买 nike


我想在 Wordpress 中更新许多产品类别名称,从 csv 文件中检索它们。
csv 文件头只是:(ID;Product_category_Name)
我有 900 个类别和子类别,采用层次结构。
是否可以通过 ID 在数据库中搜索类别并更新类别名称?
这会保持层次结构正确吗?
我可以在名称中包含 UTF-8 中的“ö”、“ä”或“å”等字符吗?我可以使用 wp php 函数或直接 sql 命令。

最佳答案

This answer, answers your question title, but not all other questions (see how to ask).

您可以使用以下 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 由您的新产品类别别名(小写字母和“-”替换空格)
  • 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 文件中。

USAGE (Let's say tha you rename "Clothing" product category to "Wear"):

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

It will display if the product category has been renamed or not. Tested and works.

关于php - 使用 SQL 查询更改 Woocommerce 产品类别名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53477241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com