• Resolved oelita

    (@oelita)


    Hello,

    I create some orders programmatically by importing data. Some of my data says if we want an invoice or not for that order. I store this data in a custom order meta (wpcf-payeur).

    Then, I call your hook wpo_wcpdf_document_is_allowed for preventing the invoice creation, in this way :

    add_filter( 'wpo_wcpdf_document_is_allowed', 'genum_disable_facture_payeur', 30, 2 );
    function genum_disable_facture_payeur( $allowed, $document ) {	
    	if( $document->exists() && ! empty($order = $document->order) ) {
    		$payeur = $order->get_meta('wpcf-payeur');		
    		if ( $payeur != '') {
    			return false;
    		} else {
    			return $allowed;
    		}
    	}
    	return $allowed;
    }

    For theses orders, there is no invoice in the order, and no invoice number in the orders list, which is fine. We thought that everyting was OK.

    BUT now we see that : the invoice PDF does indeed exist in the directory, for example in :
    /mymultisite/wp-content/uploads/sites/3/wpo_wcpdf/attachments
    and the number of the invoice will be skipped for the next invoice.
    The invoice is also attached to some emails…

    Is it normal ? Is it tied to the fact that we are on multisite ?
    How can we prevent any creation of these invoices ?

    Thanks !
    Sylvie

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hello Sylvie,
    It looks like there’s a small error in your code:

    
    if( $document->exists() && ! empty($order = $document->order) ) {
    

    Basically this says: if the document doesn’t exist yet, don’t apply any of the logic.
    I think you meant the opposite:

    
    if( ! $document->exists() && ! empty( $order = $document->order ) ) {
    

    Which will result allowing the invoice when it was already created (for whatever reason), and otherwise apply your logic.

    Let me know if that indeed resolves the issue!

    Thread Starter oelita

    (@oelita)

    Hello,

    So thanks ! That is why the invoice PDF was created, sent, but then it was hidden from the list of orders, or the order detail (as the invoice document existed at that time) !

    But sadly, even when I changed my code, it remained the exact same problem.

    I added some debug logging, and found out that, as I was doing

    $order->add_meta_data( 'wpcf-payeur', $payeur, TRUE );	
    $order->update_status( 'completed', 'Commande importée', TRUE); 
    $order->save();

    the invoice creation was called in the save function BEFORE the meta_data was saved.

    So, I changed my code to :

    $order->add_meta_data( 'wpcf-payeur', $payeur, TRUE );	
    $order->save();
    $order->update_status( 'completed', 'Commande importée', TRUE); 
    $order->save();

    and then, everything is fine !

    Thanks
    Sylvie

    Plugin Contributor Ewout

    (@pomegranate)

    Glad to hear that Sylvie! Instead of the first $order->save(); your could also use $order->save_meta_data(); specifically. This will persist the meta to the database so that our plugin can read it when it checks if it should create the invoice.

    Thread Starter oelita

    (@oelita)

    Good to know, thanks !

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Use of wpo_wcpdf_document_is_allowed on multisite’ is closed to new replies.