如何解决仅计算顶级WooCommerce产品类别以进行自定义运输计算
我正在使用上一个问题中的 Increase Shipping cost per categories count found in WooCommerce cart 答案代码。
如何从产品类别ID的数组中排除子类别,以仅保留热门类别?
根据此自定义计算的解决方案,还包括子类别,用于计算运输成本。 我需要将它们排除在外,因为它们仅用于排序项目。
我尝试向阵列添加过滤器,但制动了站点。
解决方法
要仅计算顶级产品类别(不包括子类别),您将在 Increase Shipping cost per categories count found in WooCommerce cart 答案代码中替换以下代码块:
// Loop through cart items for the current shipping package
foreach( $package['contents'] as $cart_item ){
$term_ids = array_merge(
$term_ids,(array) $cart_item['data']->get_category_ids()
);
}
有了这个:
// Loop through cart items for the current shipping package
foreach( $package['contents'] as $cart_item ){
$terms = wp_get_post_terms( $cart_item['product_id'],'product_cat' );
// Loop through product categories terms for current cart item
foreach ($terms as $term ) {
// Only top level product category terms
if ( $term->parent == 0 ) {
// Set the term id in the array
$term_ids[] = $term->term_id;
}
}
}
经过测试可以正常工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。