get_item($item_id); if (!$item) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); } $item_documents = $item['wiaas_documents']; $order_document = null; foreach ($item_documents as $item_document) { if ($item_document['key'] === $document_key && $item_document['type'] === $type) { $order_document = $item_document; break; } } if (!isset($order_document)) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); } $file_path = wiaas_get_document_version_path($order_document['version']); if (!file_exists($file_path)) { wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); } WC_Download_Handler::download_file_force( $file_path, pathinfo( $file_path, PATHINFO_FILENAME ) . '.' . pathinfo( $file_path, PATHINFO_EXTENSION ) ); } /** * Download cart document * * @param $item_key * @param $type * @param $document_key */ public static function download_cart_document($item_key, $type, $document_key) { $item = WC()->cart->get_cart_item($item_key); if (!isset($item)) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); } $item_documents = $item['_wiaas_documents']; if (!isset($item_documents) || empty($item_documents) || !array_key_exists($type, $item_documents)) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); } $cart_document = $item_documents[$type]; if ($cart_document['key'] !== $document_key) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); } $file_path = wiaas_get_document_version_path($cart_document['version']); if (!file_exists($file_path)) { wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); } WC_Download_Handler::download_file_force( $file_path, pathinfo( $file_path, PATHINFO_FILENAME ) . '.' . pathinfo( $file_path, PATHINFO_EXTENSION ) ); } } Wiaas_Document_Download::init();