From 6c087974b8b6e103b7501aea2d501c17444d5271 Mon Sep 17 00:00:00 2001 From: mt8 Date: Wed, 15 Nov 2023 15:18:52 +0900 Subject: [PATCH 1/7] Add woocommerce_order_has_downloadable_item filter to WC_Order->has_downloadable_item() --- plugins/woocommerce/includes/class-wc-order.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce/includes/class-wc-order.php b/plugins/woocommerce/includes/class-wc-order.php index 6dc59aeddb2..8adf66c8162 100644 --- a/plugins/woocommerce/includes/class-wc-order.php +++ b/plugins/woocommerce/includes/class-wc-order.php @@ -1654,16 +1654,18 @@ class WC_Order extends WC_Abstract_Order { * @return bool */ public function has_downloadable_item() { + $has_downloadable_item = false; foreach ( $this->get_items() as $item ) { if ( $item->is_type( 'line_item' ) ) { $product = $item->get_product(); if ( $product && $product->has_file() ) { - return true; + $has_downloadable_item = true; + break; } } } - return false; + return apply_filters( 'woocommerce_order_has_downloadable_item', $has_downloadable_item, $this ); } /** From 94f95f6ea31c3a25ee745017957167a517ea5439 Mon Sep 17 00:00:00 2001 From: mt8 Date: Tue, 21 Nov 2023 09:05:05 +0900 Subject: [PATCH 2/7] Revert "Add woocommerce_order_has_downloadable_item filter to WC_Order->has_downloadable_item()" This reverts commit 3baa6a891e985c88ea828987ed00281c55daf2ee. --- plugins/woocommerce/includes/class-wc-order.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugins/woocommerce/includes/class-wc-order.php b/plugins/woocommerce/includes/class-wc-order.php index 8adf66c8162..6dc59aeddb2 100644 --- a/plugins/woocommerce/includes/class-wc-order.php +++ b/plugins/woocommerce/includes/class-wc-order.php @@ -1654,18 +1654,16 @@ class WC_Order extends WC_Abstract_Order { * @return bool */ public function has_downloadable_item() { - $has_downloadable_item = false; foreach ( $this->get_items() as $item ) { if ( $item->is_type( 'line_item' ) ) { $product = $item->get_product(); if ( $product && $product->has_file() ) { - $has_downloadable_item = true; - break; + return true; } } } - return apply_filters( 'woocommerce_order_has_downloadable_item', $has_downloadable_item, $this ); + return false; } /** From d51ed1aa4c291491a2bdd9e2fa31e1880f1bdabb Mon Sep 17 00:00:00 2001 From: mt8 Date: Tue, 21 Nov 2023 09:36:19 +0900 Subject: [PATCH 3/7] Change: Added a filterable variable to the variables when loading the order/order-details.php template. --- plugins/woocommerce/includes/wc-template-functions.php | 9 ++++++++- plugins/woocommerce/templates/order/order-details.php | 3 +-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/plugins/woocommerce/includes/wc-template-functions.php b/plugins/woocommerce/includes/wc-template-functions.php index a4cbf70014e..05689f30fe9 100644 --- a/plugins/woocommerce/includes/wc-template-functions.php +++ b/plugins/woocommerce/includes/wc-template-functions.php @@ -2723,10 +2723,17 @@ if ( ! function_exists( 'woocommerce_order_details_table' ) ) { return; } + $order = wc_get_order( $order_id ); + + if ( ! $order ) { + return; + } + wc_get_template( 'order/order-details.php', array( - 'order_id' => $order_id, + 'order_id' => $order_id, + 'show_downloads' => apply_filters( 'woocommerce_order_downloads_table_show_downloads', ( $order->has_downloadable_item() && $order->is_download_permitted() ), $order ), ) ); } diff --git a/plugins/woocommerce/templates/order/order-details.php b/plugins/woocommerce/templates/order/order-details.php index 6713ceacda8..cfc8bf01810 100644 --- a/plugins/woocommerce/templates/order/order-details.php +++ b/plugins/woocommerce/templates/order/order-details.php @@ -27,9 +27,8 @@ $order_items = $order->get_items( apply_filters( 'woocommerce_purchase $show_purchase_note = $order->has_status( apply_filters( 'woocommerce_purchase_note_order_statuses', array( 'completed', 'processing' ) ) ); $show_customer_details = is_user_logged_in() && $order->get_user_id() === get_current_user_id(); $downloads = $order->get_downloadable_items(); -$show_downloads = $order->has_downloadable_item() && $order->is_download_permitted(); -if ( $show_downloads ) { +if ( $show_downloads ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited wc_get_template( 'order/order-downloads.php', array( From acf14e07a64708fcbb926f4b3f8d90935288dc2f Mon Sep 17 00:00:00 2001 From: barryhughes <3594411+barryhughes@users.noreply.github.com> Date: Tue, 21 Nov 2023 14:57:50 -0800 Subject: [PATCH 4/7] Add docblock for filter hook `woocommerce_order_downloads_table_show_downloads`. --- .../woocommerce/includes/wc-template-functions.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugins/woocommerce/includes/wc-template-functions.php b/plugins/woocommerce/includes/wc-template-functions.php index 05689f30fe9..9dbbeada562 100644 --- a/plugins/woocommerce/includes/wc-template-functions.php +++ b/plugins/woocommerce/includes/wc-template-functions.php @@ -2733,6 +2733,19 @@ if ( ! function_exists( 'woocommerce_order_details_table' ) ) { 'order/order-details.php', array( 'order_id' => $order_id, + /** + * Determines if the order downloads table should be shown (in the context of the order details + * template). + * + * By default, this is true if the order has at least one dowloadable items and download is permitted + * (which is partly determined by the order status). For special cases, though, this can be overridden + * and the downloads table can be forced to render (or forced not to render). + * + * @since 8.4.0 + * + * @param bool $show_downloads If the downloads table should be shown. + * @param WC_Order $order The related order. + */ 'show_downloads' => apply_filters( 'woocommerce_order_downloads_table_show_downloads', ( $order->has_downloadable_item() && $order->is_download_permitted() ), $order ), ) ); From 54675f228be6f418893b9a30422766ce4640bf44 Mon Sep 17 00:00:00 2001 From: barryhughes <3594411+barryhughes@users.noreply.github.com> Date: Tue, 21 Nov 2023 15:01:41 -0800 Subject: [PATCH 5/7] Update template doc tags; remove unnecessary phpcs:ignore rule. --- plugins/woocommerce/templates/order/order-details.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce/templates/order/order-details.php b/plugins/woocommerce/templates/order/order-details.php index cfc8bf01810..7ef55182c66 100644 --- a/plugins/woocommerce/templates/order/order-details.php +++ b/plugins/woocommerce/templates/order/order-details.php @@ -12,7 +12,9 @@ * * @see https://woo.com/document/template-structure/ * @package WooCommerce\Templates - * @version 7.8.0 + * @version 8.4.0 + * + * @var bool $show_downloads Controls whether the downloads table should be rendered. */ defined( 'ABSPATH' ) || exit; @@ -28,7 +30,7 @@ $show_purchase_note = $order->has_status( apply_filters( 'woocommerce_purchas $show_customer_details = is_user_logged_in() && $order->get_user_id() === get_current_user_id(); $downloads = $order->get_downloadable_items(); -if ( $show_downloads ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited +if ( $show_downloads ) { wc_get_template( 'order/order-downloads.php', array( From 6cf48117cc2914d0cbbd0fd73a8a7c3e8f6a0199 Mon Sep 17 00:00:00 2001 From: barryhughes <3594411+barryhughes@users.noreply.github.com> Date: Tue, 21 Nov 2023 15:04:07 -0800 Subject: [PATCH 6/7] Changelog. --- plugins/woocommerce/changelog/add-show-downloads-hook | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/add-show-downloads-hook diff --git a/plugins/woocommerce/changelog/add-show-downloads-hook b/plugins/woocommerce/changelog/add-show-downloads-hook new file mode 100644 index 00000000000..8d4b9e92c4f --- /dev/null +++ b/plugins/woocommerce/changelog/add-show-downloads-hook @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Adds new hook `woocommerce_order_downloads_table_show_downloads` to control rendering of the downloads table (classic templates only). From 8fa32829813f34ae1ad5847a34b73f4871f5ff28 Mon Sep 17 00:00:00 2001 From: barryhughes <3594411+barryhughes@users.noreply.github.com> Date: Mon, 4 Dec 2023 15:30:28 -0800 Subject: [PATCH 7/7] Bump version tags. --- plugins/woocommerce/includes/wc-template-functions.php | 2 +- plugins/woocommerce/templates/order/order-details.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce/includes/wc-template-functions.php b/plugins/woocommerce/includes/wc-template-functions.php index 9dbbeada562..d42d90c96f5 100644 --- a/plugins/woocommerce/includes/wc-template-functions.php +++ b/plugins/woocommerce/includes/wc-template-functions.php @@ -2741,7 +2741,7 @@ if ( ! function_exists( 'woocommerce_order_details_table' ) ) { * (which is partly determined by the order status). For special cases, though, this can be overridden * and the downloads table can be forced to render (or forced not to render). * - * @since 8.4.0 + * @since 8.5.0 * * @param bool $show_downloads If the downloads table should be shown. * @param WC_Order $order The related order. diff --git a/plugins/woocommerce/templates/order/order-details.php b/plugins/woocommerce/templates/order/order-details.php index 7ef55182c66..131d86b91c2 100644 --- a/plugins/woocommerce/templates/order/order-details.php +++ b/plugins/woocommerce/templates/order/order-details.php @@ -12,7 +12,7 @@ * * @see https://woo.com/document/template-structure/ * @package WooCommerce\Templates - * @version 8.4.0 + * @version 8.5.0 * * @var bool $show_downloads Controls whether the downloads table should be rendered. */