From c09410af94eaba08893dad8aa77176d8f26eb8d5 Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Wed, 9 Aug 2023 14:52:29 +0200 Subject: [PATCH] Include parent category IDs in customer order fields [MAILPOET-5377] --- .../Fields/CustomerOrderFieldsFactory.php | 13 ++++++-- .../Fields/CustomerOrderFieldsFactoryTest.php | 30 +++++++++++-------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/mailpoet/lib/Automation/Integrations/WooCommerce/Fields/CustomerOrderFieldsFactory.php b/mailpoet/lib/Automation/Integrations/WooCommerce/Fields/CustomerOrderFieldsFactory.php index eed13e1a93..26cad8c406 100644 --- a/mailpoet/lib/Automation/Integrations/WooCommerce/Fields/CustomerOrderFieldsFactory.php +++ b/mailpoet/lib/Automation/Integrations/WooCommerce/Fields/CustomerOrderFieldsFactory.php @@ -20,14 +20,19 @@ class CustomerOrderFieldsFactory { /** @var TermOptionsBuilder */ private $termOptionsBuilder; + /** @var TermParentsLoader */ + private $termParentsLoader; + public function __construct( WordPress $wordPress, WooCommerce $wooCommerce, - TermOptionsBuilder $termOptionsBuilder + TermOptionsBuilder $termOptionsBuilder, + TermParentsLoader $termParentsLoader ) { $this->wordPress = $wordPress; $this->wooCommerce = $wooCommerce; $this->termOptionsBuilder = $termOptionsBuilder; + $this->termParentsLoader = $termParentsLoader; } /** @return Field[] */ @@ -92,7 +97,11 @@ class CustomerOrderFieldsFactory { __('Purchased categories', 'mailpoet'), function (CustomerPayload $payload) { $customer = $payload->getCustomer(); - return $customer ? $this->getOrderProductTermIds($customer, 'product_cat') : []; + if (!$customer) { + return []; + } + $ids = $this->getOrderProductTermIds($customer, 'product_cat'); + return array_merge($ids, $this->termParentsLoader->getParentIds($ids)); }, [ 'options' => $this->termOptionsBuilder->getTermOptions('product_cat'), diff --git a/mailpoet/tests/integration/Automation/Integrations/WooCommerce/Fields/CustomerOrderFieldsFactoryTest.php b/mailpoet/tests/integration/Automation/Integrations/WooCommerce/Fields/CustomerOrderFieldsFactoryTest.php index f76703c3bb..df87b37d1b 100644 --- a/mailpoet/tests/integration/Automation/Integrations/WooCommerce/Fields/CustomerOrderFieldsFactoryTest.php +++ b/mailpoet/tests/integration/Automation/Integrations/WooCommerce/Fields/CustomerOrderFieldsFactoryTest.php @@ -96,8 +96,9 @@ class CustomerOrderFieldsFactoryTest extends \MailPoetTest { $cat1Id = $this->tester->createWordPressTerm('Cat 1', 'product_cat', ['slug' => 'cat-1']); $cat2Id = $this->tester->createWordPressTerm('Cat 2', 'product_cat', ['slug' => 'cat-2']); $cat3Id = $this->tester->createWordPressTerm('Cat 3', 'product_cat', ['slug' => 'cat-3']); - $subCat1 = $this->tester->createWordPressTerm('Subcat 1', 'product_cat', ['slug' => 'subcat-1', 'parent' => $cat1Id]); - $subCat2 = $this->tester->createWordPressTerm('Subcat 2', 'product_cat', ['slug' => 'subcat-2', 'parent' => $cat1Id]); + $subCat1Id = $this->tester->createWordPressTerm('Subcat 1', 'product_cat', ['slug' => 'subcat-1', 'parent' => $cat1Id]); + $subCat2Id = $this->tester->createWordPressTerm('Subcat 2', 'product_cat', ['slug' => 'subcat-2', 'parent' => $cat1Id]); + $subSubCat1Id = $this->tester->createWordPressTerm('Subsubcat 1', 'product_cat', ['slug' => 'subsubcat-1', 'parent' => $subCat1Id]); // check definitions $fields = $this->getFieldsMap(); @@ -107,8 +108,9 @@ class CustomerOrderFieldsFactoryTest extends \MailPoetTest { $this->assertSame([ 'options' => [ ['id' => $cat1Id, 'name' => 'Cat 1'], - ['id' => $subCat1, 'name' => 'Cat 1 | Subcat 1'], - ['id' => $subCat2, 'name' => 'Cat 1 | Subcat 2'], + ['id' => $subCat1Id, 'name' => 'Cat 1 | Subcat 1'], + ['id' => $subSubCat1Id, 'name' => 'Cat 1 | Subcat 1 | Subsubcat 1'], + ['id' => $subCat2Id, 'name' => 'Cat 1 | Subcat 2'], ['id' => $cat2Id, 'name' => 'Cat 2'], ['id' => $cat3Id, 'name' => 'Cat 3'], ['id' => $uncategorizedId, 'name' => 'Uncategorized'], @@ -117,10 +119,10 @@ class CustomerOrderFieldsFactoryTest extends \MailPoetTest { // create products $p1 = $this->tester->createWooCommerceProduct(['name' => 'Product 1']); // uncategorized - $p2 = $this->tester->createWooCommerceProduct(['name' => 'Product 2', 'category_ids' => [$cat1Id]]); - $p3 = $this->tester->createWooCommerceProduct(['name' => 'Product 3', 'category_ids' => [$cat1Id, $cat2Id]]); - $p4 = $this->tester->createWooCommerceProduct(['name' => 'Product 4', 'category_ids' => [$subCat1]]); - $p5 = $this->tester->createWooCommerceProduct(['name' => 'Product 5', 'category_ids' => [$cat3Id, $subCat2]]); + $p2 = $this->tester->createWooCommerceProduct(['name' => 'Product 2', 'category_ids' => [$cat2Id]]); + $p3 = $this->tester->createWooCommerceProduct(['name' => 'Product 3', 'category_ids' => [$subSubCat1Id]]); + $p4 = $this->tester->createWooCommerceProduct(['name' => 'Product 4', 'category_ids' => [$cat2Id, $subSubCat1Id]]); + $p5 = $this->tester->createWooCommerceProduct(['name' => 'Product 5', 'category_ids' => [$cat3Id, $subCat2Id]]); // check values (guest) $o1 = $this->createOrder(0, 123); @@ -151,13 +153,14 @@ class CustomerOrderFieldsFactoryTest extends \MailPoetTest { $value = $purchasedCategories->getValue($customerPayload); $this->assertIsArray($value); - $this->assertCount(4, $value); + $this->assertCount(5, $value); $this->assertContains($uncategorizedId, $value); - $this->assertContains($cat1Id, $value); + $this->assertContains($cat1Id, $value); // auto-included parent $this->assertContains($cat2Id, $value); - $this->assertContains($subCat1, $value); + $this->assertContains($subCat1Id, $value); // auto-included parent + $this->assertContains($subSubCat1Id, $value); $this->assertNotContains($cat3Id, $value); - $this->assertNotContains($subCat2, $value); + $this->assertNotContains($subCat2Id, $value); } public function testPurchasedTags(): void { @@ -251,7 +254,8 @@ class CustomerOrderFieldsFactoryTest extends \MailPoetTest { $purchasedCategories = $fields['woocommerce:customer:purchased-categories']; $value = $purchasedCategories->getValue($customerPayload); $this->assertIsArray($value); - $this->assertCount(1, $value); + $this->assertCount(2, $value); + $this->assertContains($categoryId, $value); // auto-included parent $this->assertContains($subCategoryId, $value); }