• Hi,

    I use the – no longer maintained – “Invoices for WooCommerce Premium” plugin (by Bas Elbers) and I’d like to switch to the premium version of WooCommerce PDF Invoices & Packing Slips.

    I downloaded the free version of your plugin on a test installation and try to show the invoices already generated by the previous plugin without having to keep it activated.
    I need an access to the existing invoices on the admin side (orders list page and edit order page) as well as the “My account” page in which the users have access to their invoices.

    My issue is very similar to this one.

    I was able to retrieve the meta_key used for the invoice number (_bewpi_invoice_number) as well as the meta_key for the date of the invoice (_bewpi_invoice_date).

    I’ve used this code snippet you provided on the topic above (edited with my meta keys) with no success :

    /**
     * WooCommerce PDF Invoices & Packing Slips:
     * Migrate invoice numbers from YITH WooCommerce PDF Invoice and Shipping List Premium
     */
    add_filter( 'wpo_wcpdf_invoice_get_date', function( $date, $document ) {
    	if ( $document->get_type() == 'invoice' && $order = $document->order ) {
    		// Get invoice date
    		if ( $legacy_date = $order->get_meta('_bewpi_invoice_date') ) {
    			$date = new \WC_DateTime( "@{$legacy_date}", new \DateTimeZone( 'UTC' ) );
    			$document->set_date( $date );
    		}
    	}
    	return $date;
    }, 10, 2 );
    add_filter( 'wpo_wcpdf_external_invoice_number_enabled', '__return_true' );
    add_filter( 'wpo_wcpdf_external_invoice_number', function( $number, $document ) {
    	if ( ! empty( $document->order ) ) {
    		// Get invoice formatted number
    		$number = $document->order->get_meta( '_bewpi_invoice_number' ); // if $number is empty, the plugin will generate a new number
    	}
    	return $number;
    }, 10, 2 );

    If I try to generate an invoice by clicking the invoice icon on the orders list page, no invoice is generated and I have this error :

    Exception: DateTime::__construct(): Failed to parse time string (@2022-03-25 15:32:37) at position 8 (-): Double timezone specification

    What am I doing wrong ?

    I use WordPress 5.9.4, Woocommerce 6.3.1 and the Php version is 8.0.22

    Thanks !

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

    (@dpeyou)

    Hello @matt-pain,

    Could you test for the returned value here?
    $legacy_date = $order->get_meta('_bewpi_invoice_date')
    It appears to me your error message is indicating that is the issue’s source. ??

    Test like this, using var_dump:
    var_dump( $legacy_date = $order->get_meta( '_bewpi_invoice_date' ) );

    For testing purposes, you could change "@{$legacy_date}" to something like "now" to see if you still get an error (my guess is no).

    This suggestion is based on the PHP documentation. WC_DateTime extends the DateTime class:
    public DateTime::__construct(string $datetime = "now", ?DateTimeZone $timezone = null)

    Plugin Contributor Darren Peyou

    (@dpeyou)

    Hey @matt-pain,

    I just revisited your error message & a colleague of mine pointed out to me that you just need to remove the “@” from this line:
    $date = new \WC_DateTime( "@{$legacy_date}", new \DateTimeZone( 'UTC' ) ); ??

    Thread Starter Matt12

    (@matt-pain)

    Thanks for your reply Darren,

    var_dump( $legacy_date = $order->get_meta( '_bewpi_invoice_date' ) );
    returns something like this, for each invoice generated by the old plugin :
    string(19) "2022-03-25 15:42:27"

    If I var_dump the meta_key _wcpdf_invoice_date_formatted :
    var_dump( $legacy_date = $order->get_meta( '_wcpdf_invoice_date_formatted' ) );
    for the invoices generated by your plugin, I have the same output as _bewpi_invoice_date : something like string(19) "2022-09-06 15:44:50"

    And changing @{$legacy_date} to now definitely works.
    I really don’t get why _bewpi_invoice_date doesn’t work in this case ??

    I also have an issue with the second filter to get the old invoice number :

    add_filter( 'wpo_wcpdf_external_invoice_number', function( $number, $document ) {
    	if ( ! empty( $document->order ) ) {
    		// Get invoice formatted number
    		$number = $document->order->get_meta( '_bewpi_invoice_number' ); // if $number is empty, the plugin will generate a new number
    	}
    	return $number;
    }, 10, 2 );

    The invoice number generated is correct but adds the prefix [invoice_year]- I added in the settings of your plugin.
    How do I remove it ?

    Thanks ??

    Thread Starter Matt12

    (@matt-pain)

    I didn’t see your second answer before my answer but yes removing the “@” from "@{$legacy_date}" definitely worked ! I now have the correct date for the invoices. Thanks ??

    I still have the issue with the second filter adding the prefix. Let me know if there’s a way to fix it.

    Thread Starter Matt12

    (@matt-pain)

    Do you have any update regarding this second filter ?

    add_filter( 'wpo_wcpdf_external_invoice_number', function( $number, $document ) {
    	if ( ! empty( $document->order ) ) {
    		// Get invoice formatted number
    		$number = $document->order->get_meta( '_bewpi_invoice_number' ); // if $number is empty, the plugin will generate a new number
    	}
    	return $number;
    }, 10, 2 );

    I put a prefix for the invoices in your plugin’s settings ([invoice_year]-)but the invoices created with the old plugin already have their own prefix. Do you have a way to remove your prefix from the invoice number in this case ?
    Thanks !

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Switching from other invoice plugin (Invoices for WooCommerce / Bas Elbers)’ is closed to new replies.