Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Addons/Addons.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace ShoppingFeed\ShoppingFeedWC\Addons\Plugins\WoocommercePdfInvoicesPackingSlips;

// Exit on direct access
use ShoppingFeed\ShoppingFeedWC\ShoppingFeedHelper;

defined( 'ABSPATH' ) || exit;

/**
* Class WoocommercePdfInvoicesPackingSlips is used to get the invoice file for the order.
*
* @link https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/
* @package ShoppingFeed\ShoppingFeedWC\Addons\Plugins\WoocommercePdfInvoicesPackingSlips
*/
class WoocommercePdfInvoicesPackingSlips {
public function __construct() {
if ( ! class_exists( '\WPO_WCPDF' ) ) {
return;
}

add_filter( 'sf_order_invoice_filepath', array( $this, 'get_order_invoice' ), 5, 2 );
}

/**
* Get the invoice file for the order.
*
* @param string|null $order_invoice_filepath
* @param \WC_Order $order
*
* @return string|null
*/
public function get_order_invoice( $order_invoice_filepath, $order ) {
ShoppingFeedHelper::get_logger()->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;
}
}
24 changes: 24 additions & 0 deletions src/Admin/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,30 @@ function () {
'sf_orders_settings_import_options'
);

add_settings_field(
'upload_invoice_order',
__( 'Documents', 'shopping-feed' ),
function () {
?>
<label for="upload_invoice_order">
<input
type="checkbox"
id="upload_invoice_order"
name="<?php echo esc_attr( sprintf( '%s[upload_invoice_order]', self::SF_ORDERS_OPTIONS ) ); ?>"
value="1"
<?php checked( '1', isset( $this->sf_orders_options['upload_invoice_order'] ) ? $this->sf_orders_options['upload_invoice_order'] : '0' ); ?>
>
<?php esc_html_e( "Upload order's invoice", 'shopping-feed' ); ?>
</label>
<p class="description" id="tagline-description">
<?php esc_html_e( 'Send invoice to ShoppingFeed for compatible marketplaces when order is shipped.', 'shopping-feed' ); ?>
</p>
<?php
},
self::SF_ORDERS_SETTINGS_PAGE,
'sf_orders_settings_import_options'
);

//mapping
$sf_actions = Operations::get_available_operations();

Expand Down
25 changes: 24 additions & 1 deletion src/Orders/Operations.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
OrderOperation,
OrderOperationResult,
OrderResource};
use ShoppingFeed\ShoppingFeedWC\Dependencies\ShoppingFeed\Sdk\Api\Order\Document\Invoice;
use ShoppingFeed\ShoppingFeedWC\Dependencies\ShoppingFeed\Sdk\Api\Store\StoreResource;
use ShoppingFeed\ShoppingFeedWC\Sdk\Sdk;
use ShoppingFeed\ShoppingFeedWC\ShoppingFeedHelper;
Expand Down Expand Up @@ -142,11 +143,33 @@ public function ship() {
ShoppingFeedHelper::wc_tracking_number( $this->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()
),
Expand Down