在 Woocommerce 上批量重新链接子类别到父类别
Bulk relink child categories to parent categories on Woocommerce
我们最近将一家大型产品商店从 Big commerce 导入到 woocommerce,但 运行 遇到了有关类别的问题。
产品仅链接到子类别,这意味着如果您访问父类别,则不会显示任何产品。
Product linked to child category but not parent
有没有办法让所有子类别大规模地重新链接到父类别?该商店有 6000 多种产品和数百个类别,因此即使使用批量编辑工具也需要很长时间才能完成此任务。
提前感谢您,如果已经有人问过这个问题,我们深表歉意,很难找到适合这种情况的正确措辞。
** 我遇到了这段代码**
add_action('save_post', 'assign_parent_terms', 10, 2);
function assign_parent_terms($post_id, $post){
if($post->post_type != 'product')
return $post_id;
// get all assigned terms
$terms = wp_get_post_terms($post_id, 'product_cat' );
foreach($terms as $term){
while($term->parent != 0 && !has_term( $term->parent, 'product_cat', $post )){
// move upward until we get to 0 level terms
wp_set_post_terms($post_id, array($term->parent), 'product_cat', true);
$term = get_term($term->parent, 'product_cat');
}
}
}
有人可以指出调整代码的正确方向,这样我就不必单独保存每个产品,而是 运行 一次性更新它们吗?
再次感谢您的帮助。
不要执行 add_action()
部分。使用
$products = get_posts(['post_type' => 'product', 'posts_per_page' => 100, 'offset' => 0]);
var_dump(count($products));
foreach ($products as $product) {
assign_parent_terms($oroduct->ID, $product);
}
刷新页面。每次刷新后手动将偏移量增加 100,直到转储显示 0。
我们最近将一家大型产品商店从 Big commerce 导入到 woocommerce,但 运行 遇到了有关类别的问题。
产品仅链接到子类别,这意味着如果您访问父类别,则不会显示任何产品。
Product linked to child category but not parent
有没有办法让所有子类别大规模地重新链接到父类别?该商店有 6000 多种产品和数百个类别,因此即使使用批量编辑工具也需要很长时间才能完成此任务。
提前感谢您,如果已经有人问过这个问题,我们深表歉意,很难找到适合这种情况的正确措辞。
** 我遇到了这段代码**
add_action('save_post', 'assign_parent_terms', 10, 2);
function assign_parent_terms($post_id, $post){
if($post->post_type != 'product')
return $post_id;
// get all assigned terms
$terms = wp_get_post_terms($post_id, 'product_cat' );
foreach($terms as $term){
while($term->parent != 0 && !has_term( $term->parent, 'product_cat', $post )){
// move upward until we get to 0 level terms
wp_set_post_terms($post_id, array($term->parent), 'product_cat', true);
$term = get_term($term->parent, 'product_cat');
}
}
}
有人可以指出调整代码的正确方向,这样我就不必单独保存每个产品,而是 运行 一次性更新它们吗?
再次感谢您的帮助。
不要执行 add_action()
部分。使用
$products = get_posts(['post_type' => 'product', 'posts_per_page' => 100, 'offset' => 0]);
var_dump(count($products));
foreach ($products as $product) {
assign_parent_terms($oroduct->ID, $product);
}
刷新页面。每次刷新后手动将偏移量增加 100,直到转储显示 0。