Coloring Background Product Categories On PDF

[ad_1]

Hello everyone, I had previously asked in another topic how I could color the product names according to their categories in the pdf invoice, and I was able to do this thanks to the following snippet provided by the support team.

/**
 * WooCommerce PDF Invoices & Packing Slips:
 * Set a custom color to product names based on its category
 */
add_filter( 'wpo_wcpdf_order_items_data', function( $data_list, $order, $document_type ) {	
	foreach ( $data_list as $item_id => $item ) {
		if ( $product = $item['product'] ) {
			$id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
			$terms = get_the_terms( $id, 'product_cat' );
			foreach( $terms as $term ){
				$color = '';
				switch( $term->slug ) {
					case 'accessories':
						$color = 'red'; // color name or hex code (e.g. #673ab7)
						break;
					case 'clothing':
						$color = 'blue';
						break;
				}
				if ( ! empty( $color ) ) {
					$data_list[$item_id]['name'] = sprintf( '<span style="color:' . $color . '">%s</span>', $item['name']);
					break;
				}
			}
		}
	}
	return $data_list;
}, 10, 3 );

This snippet works great, there is no problem, but we also want to change the background color of some category groups (for that row only).

I tried to do it myself by playing with the above snippet, but I couldn’t. I wonder how can I change the background colors, can you help?

Thank you, have a nice day.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer