我们最近从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');
}
}
}
有没有人可以指出调整代码的正确方向,这样我就不必单独保存每个产品,而是运行一次来更新所有产品?
再次感谢您的帮助。
https://stackoverflow.com/questions/51600380
复制相似问题