【发布时间】:2019-01-07 01:59:06
【问题描述】:
我们最近将一家大型产品商店从 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');
}
}
}
有人可以指出调整代码的正确方向,这样我就不必单独保存每个产品,而是运行一次即可全部更新?
再次感谢您的帮助。
【问题讨论】:
标签: php wordpress plugins woocommerce categories