From 72582cc0d5563017b60525803723f356bb168669 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Thu, 9 Jan 2025 18:38:44 +0530 Subject: [PATCH 1/2] `gpnf-child-products-in-parent-order-summary.php`: Added support for child products in parent order summary. --- ...child-products-in-parent-order-summary.php | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/gp-nested-forms/gpnf-child-products-in-parent-order-summary.php b/gp-nested-forms/gpnf-child-products-in-parent-order-summary.php index 994179a69..b7d365268 100644 --- a/gp-nested-forms/gpnf-child-products-in-parent-order-summary.php +++ b/gp-nested-forms/gpnf-child-products-in-parent-order-summary.php @@ -7,11 +7,13 @@ * * 1. Add a Calculated Product to your parent form. * 2. Add your Nested Form field with the :total modifier. - * 3. Copy and paste this snippet into your theme's functions.php file. + * 3. Optional: If you want to merge duplicates, assign 'true' to the $merge_duplicates variable. + * 4. Copy and paste this snippet into your theme's functions.php file. * * Now the Calculated Product field on your parent form will be replaced with the products from each child entry. */ -add_filter( 'gform_product_info', function( $product_info, $form, $entry ) { +$merge_duplicates = false; +add_filter( 'gform_product_info', function( $product_info, $form, $entry ) use ( $merge_duplicates ) { foreach ( $form['fields'] as $field ) { @@ -52,7 +54,29 @@ $_child_products[ "{$nested_form_field_id}.{$child_entry['id']}_{$child_field_id}" ] = $child_product; } - $child_products = $child_products + $_child_products; + + if ( $merge_duplicates ) { + // Loop through $_child_products and compare with $child_products. + foreach ( $_child_products as $key => $_child_product ) { + $match_found = false; + + foreach ( $child_products as &$child_product ) { + // Check if the name and price match + if ( $child_product['name'] == $_child_product['name'] && $child_product['price'] == $_child_product['price'] ) { + $child_product['quantity'] += $_child_product['quantity']; + + $match_found = true; + unset($_child_products[$key]); + break; + } + } + } + } + + // If there are remaining products in $_child_products (after merging) or if we are not merging, add them to $child_products. + if ( ! empty( $_child_products ) || ! $merge_duplicates ) { + $child_products = array_merge( $child_products, $_child_products ); + } } } From 0a97bd1f6b71e55af0d9367812b20e79a5aeafb5 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Thu, 9 Jan 2025 19:02:49 +0530 Subject: [PATCH 2/2] `gpnf-child-products-in-parent-order-summary.php`: Added support for child products in parent order summary. --- gp-nested-forms/gpnf-child-products-in-parent-order-summary.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gp-nested-forms/gpnf-child-products-in-parent-order-summary.php b/gp-nested-forms/gpnf-child-products-in-parent-order-summary.php index b7d365268..1300bf1bf 100644 --- a/gp-nested-forms/gpnf-child-products-in-parent-order-summary.php +++ b/gp-nested-forms/gpnf-child-products-in-parent-order-summary.php @@ -66,7 +66,7 @@ $child_product['quantity'] += $_child_product['quantity']; $match_found = true; - unset($_child_products[$key]); + unset( $_child_products[ $key ] ); break; } }