Handle order documents

This commit is contained in:
Almira Krdzic
2018-11-15 11:29:15 +01:00
parent 0ba27b2f1d
commit 6c656a0b26
10 changed files with 216 additions and 108 deletions

View File

@@ -83,6 +83,49 @@ class Wiaas_Document_Download {
);
}
/**
* Download order other document (not binded to any bundle)
* @param int $order_id
* @param string $document_key
*/
public static function download_order_other_document($order_id, $document_key) {
$order = wc_get_order($order_id);
if (!$order) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
}
$order_other_documents = wiaas_get_order_other_documents($order_id);
$order_document = null;
foreach ($order_other_documents as $order_other_document) {
if ($order_other_document['key'] === $document_key) {
$order_document = $order_other_document;
break;
}
}
if (!isset($order_document)) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
}
$file_path = wiaas_get_document_version_path($order_document['version']);
error_log($file_path);
if (!file_exists($file_path)) {
wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) );
}
WC_Download_Handler::download_file_force(
$file_path,
pathinfo( $file_path, PATHINFO_FILENAME ) . '.' . pathinfo( $file_path, PATHINFO_EXTENSION )
);
}
/**
* Download document related to order item
*
@@ -91,7 +134,7 @@ class Wiaas_Document_Download {
* @param $type
* @param $document_key
*/
public static function download_order_item_document($order_id, $item_id, $type, $document_key) {
public static function download_order_item_document($order_id, $item_id, $document_key) {
$order = wc_get_order($order_id);
if (!$order) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
@@ -106,7 +149,7 @@ class Wiaas_Document_Download {
$order_document = null;
foreach ($item_documents as $item_document) {
if ($item_document['key'] === $document_key && $item_document['type'] === $type) {
if ($item_document['key'] === $document_key) {
$order_document = $item_document;
break;
}