• Hi. I’ve created custom packing slip template and this code is WORKING to fetch the Parent product category but I now need to exclude Items (don’t fetch) if the items belong to any of these categories – eg. ‘cat1’, ‘cat2’, ‘cat3’ – How can I do that? I’ve tried has_terms and in_carry… not working. Also how I need to stop products in these categories from being included in my count?

    <?php
    			$count = 1;
    			
    			$items = $order->get_items();
    			if ( sizeof( $items ) > 0 )
    				:
    			foreach ( $items as $item_id => $item )
    				: ?>
    			<?php
    			$items_count = count( $order->get_items() );
    			$product = wc_get_product($item->get_product_id());
    			$item_sku = $product->get_sku();
    			$product_terms = wc_get_product_terms( $product->get_id(), 'product_cat' );
    			?>
    <?php
    	foreach ($product_terms as $product_term) {
    	    
    		if ($product_term->parent == 0) {
    			echo $product_term->name;
    			
    		}
    	}
    	?>
Viewing 1 replies (of 1 total)
  • Plugin Contributor alexmigf

    (@alexmigf)

    Hi @pbg4775

    First you should use the default function to retrieve the order items, because doing it fixes your 2 problems: $items = $this->get_order_items();

    Note that the items returned by the function mentioned above is different from the one you’re using, just do a var_dump to see the array structure.

    Then you could use the hook below to remove the items that have a specific category attached:

    add_filter( 'wpo_wcpdf_order_items_data', function( $items, $order, $document_type ) {
    	$exclude_from_this_categories = array( 19, 52, 23 ); // fill with category IDs separated by comma
    
    	foreach( $items as $item_id => $item ) {
    		$product_category_ids = $item['product']->get_category_ids();
    		foreach( $product_category_ids as $cat_id ) {
    			if( in_array( $cat_id, $exclude_from_this_categories ) ) {
    				unset( $items[$item_id] );
    			}
    		}
    	}
    	
    	return $items;
    }, 10, 3 );

    If you never worked with actions/filters please read this documentation page: How to use filters

Viewing 1 replies (of 1 total)
  • The topic ‘If Order item belongs to either of these categories, hide from Packing Slip’ is closed to new replies.