【发布时间】:2020-04-26 12:17:19
【问题描述】:
我有一个 WooCommerce 设置,当我创建新产品时,我会像往常一样为产品分配一个类别。
Clothing
- Womens
-- Accessories
- Mens
-- Accessories <-- assigned to this term
我想要达到的目标:
当我选择一个子类别时,我还想将该帖子设置为它上面的每个父类别。在这种情况下,它看起来像:
Clothing <-- assigned to this term
- Womens
-- Accessories
- Mens <-- assigned to this term
-- Accessories <-- assigned to this term
注意:我的大部分产品都是其他用户从前端创建的,所以我不能 只需选择其他框,我知道这是一个选项。
我目前的尝试:
function set_product_parent_categories( $post_id ) {
$term_ids = wp_get_post_terms( $post_id, 'product_cat' );
foreach( $term_ids as $term_id ) {
if( $term_id->parent > 0 ) {
// Assign product to the parent category too.
wp_set_object_terms( $post_id, $term_id->parent, 'product_cat' );
}
}
}
add_action( 'woocommerce_update_product', __NAMESPACE__.'\\set_product_parent_categories', 10, 1 );
【问题讨论】:
标签: wordpress woocommerce