Support » Plugin: WooCommerce PDF Invoices & Packing Slips » Displaying product attributes hook (exclude certain category)

  • Resolved Toomas

    (@toomask)


    Hello

    I’m using following hook to display product attribute in invoice:

    add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_show_product_attributes', 10, 3 );
    function wpo_wcpdf_show_product_attributes ( $template_type, $item, $order ) {
        if(empty($item['product'])) return;
        $document = wcpdf_get_document( $template_type, $order );
        printf('<div class="product-attribute">Attribute name: %s</div>', $document->get_product_attribute('Attribute name', $item['product']));
    }

    But is it possible to exclude certain category products? I need to display product attribute with all products in all categories except one specific category.

    Is it possible somehow to modify this hook to exclude certain category products?

    Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support kluver

    (@kluver)

    Hi @toomask,

    I believe the following hook should do the trick:

    add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_show_product_attributes', 10, 3 );
    function wpo_wcpdf_show_product_attributes ( $template_type, $item, $order ) {
    
        if( $product = $item['product'] ) {
    
        	//define your categories here
        	$no_attribute_cats = array('Music', 'Clothing');
    
        	$document = wcpdf_get_document( $template_type, $order );
        	$item_cats = array();
        	$product_id = $product->get_parent_id() ? $product_id = $product->get_parent_id() : $product_id = $product->get_id();
    		$terms = get_the_terms( $product_id, 'product_cat' );
    
    		if ( !empty($terms) ) {
    			foreach ($terms as $key => $term) {
    				// echo '<pre>';var_dump($term);echo '</pre>';die();
    				$item_cats[$term->term_id] = $term->name;
    			}
    		}
    
    		// get array of category matches
    		$cat_matches = array_intersect($no_attribute_cats, $item_cats);
    		if ( count($cat_matches) == 0 ) {
    			printf('<div class="product-attribute">Attribute name: %s</div>', $document->get_product_attribute('Atribute name', $product));
    		}
        }
    }

    Just define the categories that should exclude the printing of the attribute in the $no_attribute_cats array (comma separated).

    • This reply was modified 1 year, 8 months ago by kluver.
    • This reply was modified 1 year, 8 months ago by kluver.

    Hi

    Thanks for quick reply! Works fine, thanks!

    Best regards
    Toomas

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Displaying product attributes hook (exclude certain category)’ is closed to new replies.