From 710e42aa4698750ddb51651dcc548ac22e0dee1f Mon Sep 17 00:00:00 2001 From: Clement Boirie Date: Fri, 27 Jun 2025 09:51:03 +0200 Subject: [PATCH] upload invoice when shipping an order --- src/Addons/Addons.php | 15 ++- .../WoocommercePdfInvoicesPackingSlips.php | 100 ++++++++++++++++++ src/Admin/Options.php | 24 +++++ src/Orders/Operations.php | 25 ++++- 4 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 src/Addons/Plugins/WoocommercePdfInvoicesPackingSlips/WoocommercePdfInvoicesPackingSlips.php diff --git a/src/Addons/Addons.php b/src/Addons/Addons.php index f1104a4f..cfaaca80 100644 --- a/src/Addons/Addons.php +++ b/src/Addons/Addons.php @@ -10,6 +10,7 @@ use ShoppingFeed\ShoppingFeedWC\Addons\Plugins\ASTPlugin\ASTPlugin; use ShoppingFeed\ShoppingFeedWC\Addons\Plugins\ChainedProductsPlugin\ChainedProducts; use ShoppingFeed\ShoppingFeedWC\Addons\Plugins\PhWoocommerceShipmentTrackingProPlugin\PhWoocommerceShipmentTrackingProPlugin; +use ShoppingFeed\ShoppingFeedWC\Addons\Plugins\WoocommercePdfInvoicesPackingSlips\WoocommercePdfInvoicesPackingSlips; use ShoppingFeed\ShoppingFeedWC\Addons\Shipping\Shipping; class Addons { @@ -36,10 +37,16 @@ class Addons { */ private $chained_products_plugin; + /** + * @var WoocommercePdfInvoicesPackingSlips + */ + private $woocommerce_pdf_invoices_packing_slips; + public function __construct() { - $this->shipping = new Shipping(); - $this->inventory = new Inventory(); - $this->marketplaces = new Marketplaces(); - $this->chained_products_plugin = new ChainedProducts(); + $this->shipping = new Shipping(); + $this->inventory = new Inventory(); + $this->marketplaces = new Marketplaces(); + $this->chained_products_plugin = new ChainedProducts(); + $this->woocommerce_pdf_invoices_packing_slips = new WoocommercePdfInvoicesPackingSlips(); } } diff --git a/src/Addons/Plugins/WoocommercePdfInvoicesPackingSlips/WoocommercePdfInvoicesPackingSlips.php b/src/Addons/Plugins/WoocommercePdfInvoicesPackingSlips/WoocommercePdfInvoicesPackingSlips.php new file mode 100644 index 00000000..93aca1bc --- /dev/null +++ b/src/Addons/Plugins/WoocommercePdfInvoicesPackingSlips/WoocommercePdfInvoicesPackingSlips.php @@ -0,0 +1,100 @@ +debug( + sprintf( + 'Check for order invoice to upload.' + ), + [ + 'source' => 'shopping-feed', + 'order' => $order->get_id(), + ] + ); + + // Don't override existing invoices. + if ( null !== $order_invoice_filepath ) { + ShoppingFeedHelper::get_logger()->info( + sprintf( + 'An invoice file has been provided.' + ), + [ + 'source' => 'shopping-feed', + 'order' => $order->get_id(), + 'invoice_filepath' => str_replace( WP_CONTENT_DIR, '', $order_invoice_filepath ), + ] + ); + + return $order_invoice_filepath; + } + + try { + $invoice = wcpdf_get_invoice( $order ); + if ( $invoice ) { + ShoppingFeedHelper::get_logger()->debug( + sprintf( + 'An invoice is available for the order.' + ), + [ + 'source' => 'shopping-feed', + 'order' => $order->get_id(), + ] + ); + + $order_invoice_filepath = wcpdf_get_document_file( $invoice ); + + ShoppingFeedHelper::get_logger()->info( + sprintf( + 'Successfully got the invoice file for the order.' + ), + [ + 'source' => 'shopping-feed', + 'order' => $order->get_id(), + 'invoice_filepath' => str_replace( WP_CONTENT_DIR, '', $order_invoice_filepath ), + ] + ); + } + } catch ( \Exception $exception ) { + ShoppingFeedHelper::get_logger()->error( + sprintf( + 'An error occurred while trying to get document file.' + ), + [ + 'source' => 'shopping-feed', + 'order' => $order->get_id(), + ] + ); + } + + return $order_invoice_filepath; + } +} diff --git a/src/Admin/Options.php b/src/Admin/Options.php index c3cbc948..50f55189 100644 --- a/src/Admin/Options.php +++ b/src/Admin/Options.php @@ -1557,6 +1557,30 @@ function () { 'sf_orders_settings_import_options' ); + add_settings_field( + 'upload_invoice_order', + __( 'Documents', 'shopping-feed' ), + function () { + ?> + +

+ +

+ wc_order ), ShoppingFeedHelper::wc_tracking_link( $this->wc_order ) ); + + // Upload invoice associated with the order if the option is enabled and document is available. + $upload_document = (bool) ( ShoppingFeedHelper::get_sf_orders_options()['upload_invoice_order'] ?? false ); + if ( $upload_document ) { + /** + * Filters the file path to the order invoice. + * + * @param string|null $order_invoice_filepath File path to the order invoice. Null if the order has no invoice. + * @param \WC_Order $order Current order + */ + $order_invoice_filepath = apply_filters( 'sf_order_invoice_filepath', null, $this->wc_order ); + if ( null !== $order_invoice_filepath && is_readable( $order_invoice_filepath ) ) { + error_log( $order_invoice_filepath ); + $order_invoice = new Invoice( $order_invoice_filepath ); + $this->order_operation->uploadDocument( + $this->sf_reference, + $this->sf_channel_name, + $order_invoice + ); + } + } + $this->order_api->execute( $this->order_operation ); } catch ( Exception $exception ) { ShoppingFeedHelper::get_logger()->error( sprintf( - /* translators: %s: Error message. */ + /* translators: %s: Error message. */ __( 'Failed to ship sf order %s', 'shopping-feed' ), $exception->getMessage() ),