• Resolved redmorgane

    (@redmorgane)


    Hi,
    I am using your plugin to create a gift card template for one specific product on the shop of a client.
    I only need the PDF to be send if this particular product is added to cart and checked out. I only allow one item at a time in the cart to prevent any mix up.

    I tried all the snippets I could find in this forum, tried to tweak them to what I need, but none of them work.

    Here is the last thing I tried (the product ID is 185747, and it is a variable product) , which still send the PDF to every product.
    All my other attempt doesn’t send the PDF at all.

    add_filter('wpo_wcpdf_document_is_allowed', 'disallow_document_on_certain_products', 10, 2);
    function disallow_document_on_certain_products( $allowed, $document )
    {
    	$products_ids = array(185747);
    	if ( !empty($order = $document->order) && $document->get_type() == 'invoice' ) :
    		foreach( $order->get_items() as $item_id => $item ) :
    			$product_id = $item['variation_id'] != 0 ? $item['variation_id'] : $item['product_id'];
                if( in_array($product_id, $products_ids) ) :
                    $allowed = false;
                endif;
        endforeach;
    	endif;
    	return $allowed;
    }

    What am I doing wrong ?
    I would be very grateful if you could help me with this issue !
    Thank you

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

    (@yordansoares)

    Hi @redmorgane,

    Just to be sure, do you want to disable just the attachment for that specific product ID, or the invoice creation altogether (that is what your code try to do)? If this last, please update your code with this one:

    /**
     * Enable the invoice for certain products
     */
    add_filter('wpo_wcpdf_document_is_allowed', function( $allowed, $document ) {
    	if ( $document->get_type() == 'invoice' ) {		
    		if ( $document->exists() ) {
    			return $allowed;
    		}
    		$allowed = false; // disabled by default
    		$products_ids = array( 18 ); // Enter here all the allowed product IDs
    		if ( ! empty( $order = $document->order) && $document->get_type() == 'invoice' ) {
    			foreach( $order->get_items() as $item_id => $item ) {
    				$product_id = $item['variation_id'] != 0 ? $item['variation_id'] : $item['product_id'];
    				if( in_array($product_id, $products_ids) ) {
    					$allowed = true; // enable if the product ID match
    					break;
    				}			
    			}
    		}
    	}	
    	return $allowed;
    }, 10, 2);

    Let me know if it worked! ??

    Thread Starter redmorgane

    (@redmorgane)

    Hi @yordansoares

    Thank you for your reply !
    I just gave your code a try, but unfortunately it doesn’t seems to work.
    No PDF is being sent at all, for any product.
    I change the $products_ids value to put my product ID, but had no luck whatsoever.

    I need the PDF to be sent ONLY for one particular product (which ID is 185747).

    I’m really confused because your code seems logical to me. Could it be bothered by the fact I rename “Invoice” with the following filter ?
    add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );

    My new prefix is “carte-cadeau”.

    Thread Starter redmorgane

    (@redmorgane)

    Sorry for double message, just to let you know that changing “invoice” with “carte-cadeau” in your code result in the PDF being send with all product (so basically cancelling the code).

    So I don’t think it has anything to do with it. I’m going to try again with invoice and check if I can tweak it myself, but would be super grateful for your help again ??

    Plugin Contributor Yordan Soares

    (@yordansoares)

    I have tested the code, and it’s working on my side. You just need to add your product IDs in the $products_ids array. Could you please install and activate temporarily the Code Snippets plugin to test it? Then, you can add it to your child theme’s functions.php file, in the case that you don’t want to use the Code Snippets plugin.

    Thread Starter redmorgane

    (@redmorgane)

    I don’t really see how installing Code Snippets can help ? Would you mind explaining why this would be useful ?

    Here is all the code related to the PDFs I have in my functions.php file.
    I also made a custom template by following the guidelines in the plugin documentation.

    // Change PDF names
    
    add_filter( 'wpo_wcpdf_invoice_title', 'wpo_wcpdf_invoice_title', 10, 2 );
    function wpo_wcpdf_invoice_title ( $title, $document ) {
        $title = 'Carte cadeau';
        return $title;
    }
    
    add_filter( 'wpo_wcpdf_filename', 'wpo_wcpdf_custom_filename', 10, 4 );
    function wpo_wcpdf_custom_filename( $filename, $template_type, $order_ids, $context ) {
        $invoice_string = _n( 'invoice', 'invoices', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
        $new_prefix = _n( 'carte-cadeau', 'cartes-cadeaux', count($order_ids), 'woocommerce-pdf-invoices-packing-slips' );
        $new_filename = str_replace($invoice_string, $new_prefix, $filename);
     
        return $new_filename;
    }
    
    // Allow PDF only for "cartes cadeaux"
    
    add_filter('wpo_wcpdf_document_is_allowed', function( $allowed, $document ) {
    	if ( $document->get_type() == 'invoice' ) {		
    		if ( $document->exists() ) {
    			return $allowed;
    		}
    		$allowed = false; // disabled by default
    		$products_ids = array(185747); // Enter here all the allowed product IDs
    		if ( ! empty( $order = $document->order) && $document->get_type() == 'invoice' ) {
    			foreach( $order->get_items() as $item_id => $item ) {
    				$product_id = $item['variation_id'] != 0 ? $item['variation_id'] : $item['product_id'];
    				if( in_array($product_id, $products_ids) ) {
    					$allowed = true; // enable if the product ID match
    					break;
    				}			
    			}
    		}
    	}	
    	return $allowed;
    }, 10, 2);

    The last part is the exact code you send me, but I changed the ID to the product I want to target.
    I just tested it again, and it is defintely not working for me.
    It result in no PDF being sent at all, for any product.

    The product I’m trying to target is a variable product, if this change anything ?

    I use my own custom made WordPress theme made on a _s basis, with very minimal plugin, so I don’t think there could be any incompatibility. Everything is up to date too.

    Anything I’m doing wrong ?

    Plugin Contributor Yordan Soares

    (@yordansoares)

    I think this improved version of the code snippet do the trick:

    /**
     * Enable the invoice for certain products
     */
    add_filter('wpo_wcpdf_document_is_allowed', function( $allowed, $document ) {
    	if ( $document->get_type() == 'invoice' ) {		
    		if ( $document->exists() ) {
    			return $allowed;
    		}
    		$allowed = false; // disabled by default
    		$products_ids = array( 185747 ); // Enter here all the allowed product IDs
    		if ( $order = $document->order ) {
    			foreach( $order->get_items() as $item_id => $item ) {
    				if( in_array( absint( $item['variation_id'] ), $products_ids ) || in_array( absint( $item['product_id'] ), $products_ids ) ) {
    					$allowed = true; // enable if the product ID match
    					break;
    				}			
    			}
    		}
    	}	
    	return $allowed;
    }, 10, 2);
    Plugin Contributor Ewout

    (@pomegranate)

    Note that the above code should be tested with new orders that don’t have an invoice yet!

    Thread Starter redmorgane

    (@redmorgane)

    Yes, that work !
    Thank you so much ??

    Plugin Contributor Yordan Soares

    (@yordansoares)

    I’m glad to hear that it worked now!

    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 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Send invoice only for a specific product’ is closed to new replies.