• Resolved WeDev.Africa

    (@mad2kx)


    Hello,

    I tried searching the forum but could not find the topics yet. I want to enable the creation of PDF invoices only for a specific product category. All other categories do not require a PDF invoice.

    Please advise?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @mad2kx,

    Try by adding this code snippet to your site in order to achieve what you want to:

    /**
     * Enable invoice just for specific product categories
     */
    add_filter( 'wpo_wcpdf_document_is_allowed', function( $condition, $document ) {	
    	if ( $document->type == 'invoice' ) {
    		if ( $document->exists() ) {
    			return $condition;
    		}
    		$condition = false;
    		if ( $order = $document->order ) {
    			//Set categories here (comma separated)
    			$not_allowed_cats = array( 'accessories', 'music' );
    
    			$order_cats = array();
    			//Get order categories
    			foreach ( $order->get_items() as $item_id => $item ) {
    				// get categories for item, requires product
    				if ( $product = $item->get_product() ) {
    					$id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
    					$terms = get_the_terms( $id, 'product_cat' );
    
    					if ( empty( $terms ) ) {
    						continue;
    					} else {
    						foreach ( $terms as $key => $term ) {
    							$order_cats[$term->term_id] = $term->slug;
    						}
    					}
    				}
    			}
    
    			// get array of category matches
    			$cat_matches = array_intersect( $not_allowed_cats, $order_cats );
    			if ( count( $cat_matches ) > 0 ) {
    				return true; // if there is 1 or more matches: allow invoice
    			}
    		}
    	}
    	return $condition;
    }, 10, 2 );

    Let me know if it worked! ??

    Thread Starter WeDev.Africa

    (@mad2kx)

    @yordansoares thanks for your fast response. So I just need to replace your $not_allowed_cats = array( 'accessories', 'music' ); with my category that I want to enable invoices for?

    Plugin Contributor Yordan Soares

    (@yordansoares)

    That’s right! I used accessories and music as examples, but you need to put your actual category slugs there.

    Thread Starter WeDev.Africa

    (@mad2kx)

    @yordansoares awesome thank you so much it’s working.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    I’m glad to hear that it worked!

    If you don’t mind and have the time, do you think you could leave us a review?

    Thanks in advance and all the best with your store!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘PDF Invoice on specific product category only’ is closed to new replies.