Created
August 28, 2024 08:53
-
-
Save webtoffee-git/bb025a5551b8639baa9b6c6e1de7ab8a to your computer and use it in GitHub Desktop.
Code for displaying the parent categories alone in packing slips - By WebToffee ( WooCommerce PDF Invoices, Packing Slips and Credit Notes Plugin )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //Do not copy this line of code | |
add_filter('wf_pklist_alter_grouping_term_names', 'wt_pklist_group_by_parent_cat_invoice', 10, 4); | |
function wt_pklist_group_by_parent_cat_invoice($term_name_arr, $product_id, $template_type, $order) { | |
if ( 'packinglist' === $template_type ) { | |
$terms = get_the_terms($product_id, 'product_cat'); | |
if ( $terms ) { | |
$main_category_name = ''; | |
$visited_terms = array(); | |
foreach ( $terms as $term ) { | |
$current_term = $term; | |
while ( $current_term->parent > 0 ) { | |
if ( in_array( $current_term->parent, $visited_terms ) ) { | |
break; | |
} | |
$visited_terms[] = $current_term->parent; | |
$current_term = get_term_by("id", $current_term->parent, "product_cat"); | |
} | |
// Only set $main_category_name if it is empty | |
if ( empty($main_category_name) && $current_term->parent == 0 ) { | |
$main_category_name = $current_term->name; | |
} | |
} | |
if ( ! empty( $main_category_name ) ) { | |
$term_name_arr = array($main_category_name); | |
} | |
} | |
} | |
return $term_name_arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment