• Resolved Daniel P.

    (@danidub)


    Hello, we are selling gift cards but we need them to not being invoiced. Is there code or option we can include on our store for this to work?

    Could be something specific to a product or a category. Better would be to add something to a product if possible.

    Thank you so much for this great plugin!

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

    (@alexmigf)

    Hi @danidub

    You could use ACF to add a custom field to those products you want to exclude, and then use a code snippet like below in your functions.php file:

    add_filter( 'wpo_wcpdf_order_items_data', function( $items, $order, $document_type ) {
    	if( ! empty( $items ) && ! empty( $order ) ) {
    		foreach( $items as $item_id => $item ) {
    			$product_id = $item['variation_id'] != 0 ? $item['variation_id'] : $item['product_id'];
    			$meta       = get_post_meta( $product_id, '_my_meta_key', true ); // replace with your meta
    			if( ! empty( $meta ) ) {
    				unset( $items[$item_id] );
    			}
    		}
    	}
    	return $items;
    }, 10, 3 );

    Don’t forget to replace the custom field key.

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

    Thread Starter Daniel P.

    (@danidub)

    @alexmigf thank you for your help. I have modified the snippet you create because actually we need to exclude only one product. The new snippet will compare the product ID and unset it from the invoice:

    add_filter( 'wpo_wcpdf_order_items_data', function( $items, $order, $document_type ) {
    	
        $product_to_exclude = 10324;
    
        if( ! empty( $items ) && ! empty( $order ) ) {
              foreach( $items as $item_id => $item ) {
                   $product_id = $item['variation_id'] != 0 ? $item['variation_id'] : $item['product_id'];
                   if( $product_id == $product_to_exclude ) {
                        unset( $items[$item_id] );
                    }
                }
        }
    	
         return $items;
    }, 10, 3 );

    The problem is that when generating the invoice the subtotal and total are still calculating the value of the product. In this case we have made a test using the product to exclude (was 50€). So the invoice generated is empty of items but the calculation is still happening.

    We need this type of functionality because we are selling Gift Cards, and we don’t need them to be invoiced. We want to exclude this product from invoices.

    Please see: invoice screenshot

    • This reply was modified 3 years, 8 months ago by Daniel P..
    Plugin Contributor Darren Peyou

    (@dpeyou)

    Hi @danidub,

    It sounds like you are trying to perform calculations (modifying the totals) but our plugin grabs the information it displays from WooCommerce (WC).
    This means that you would somehow have to modify the order in WC, then your invoices would follow suit.

    Thread Starter Daniel P.

    (@danidub)

    @dpeyou I understand your point.

    Let me ask another question. I have setup to send the invoice to the client once the order is completed. Is there any way to bypass this? I’m trying to achieve the same as before, when a user buys some special product, or there is a product on an order, don’t send the invoice by email to the customer.

    Is there any hook that runs before the sending email action I can intercept and fire if the conditions are met?

    Thanks.

    Plugin Contributor Darren Peyou

    (@dpeyou)

    Hey @danidub,

    Sorry for the delay, it was a long weekend & I hope you also got to relax. ??

    You should be able to prevent the Invoice attachment with this code:

    add_filter('wpo_wcpdf_custom_attachment_condition', function( $condition, $order, $status, $template_type ) {
    	if ( $template_type == 'invoice' ) {
    		
    		$conditional_id = "74";
    
    		foreach ( $order->get_items() as $item_id => $item ) {
        
    			// check product ID
    			$product_id = $item->get_product_id();
    
    			// do not send attachment if conditional product is found (gift card ID)
    			if ($product_id == $conditional_id) {
    				
    				$condition = false;
    				return $condition;
    			}
    		}
    	}
    	return $condition;
    }, 10, 4 );

    You will need to modify this code by adding the Gift Card ID number where you see $conditional_id = '74'. You can find the product IDs in the Product menu — just highlight a product & the ID should appear.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Exclude product or category from invoices’ is closed to new replies.