diff --git a/mailpoet/lib/Automation/Integrations/WooCommerce/Fields/OrderFieldsFactory.php b/mailpoet/lib/Automation/Integrations/WooCommerce/Fields/OrderFieldsFactory.php index 6c9037f038..5cd5188a25 100644 --- a/mailpoet/lib/Automation/Integrations/WooCommerce/Fields/OrderFieldsFactory.php +++ b/mailpoet/lib/Automation/Integrations/WooCommerce/Fields/OrderFieldsFactory.php @@ -79,11 +79,14 @@ class OrderFieldsFactory { ), new Field( 'woocommerce:order:billing-country', - Field::TYPE_STRING, + Field::TYPE_ENUM, __('Billing country', 'mailpoet'), function (OrderPayload $payload) { return $payload->getOrder()->get_billing_country(); - } + }, + [ + 'options' => $this->getBillingCountryOptions(), + ] ), new Field( 'woocommerce:order:shipping-company', @@ -127,11 +130,14 @@ class OrderFieldsFactory { ), new Field( 'woocommerce:order:shipping-country', - Field::TYPE_STRING, + Field::TYPE_ENUM, __('Shipping country', 'mailpoet'), function (OrderPayload $payload) { return $payload->getOrder()->get_shipping_country(); - } + }, + [ + 'options' => $this->getShippingCountryOptions(), + ] ), new Field( 'woocommerce:order:created-date', @@ -259,6 +265,22 @@ class OrderFieldsFactory { ); } + private function getBillingCountryOptions(): array { + $options = []; + foreach (WC()->countries->get_allowed_countries() as $code => $name) { + $options[] = ['id' => $code, 'name' => $name]; + } + return $options; + } + + private function getShippingCountryOptions(): array { + $options = []; + foreach (WC()->countries->get_shipping_countries() as $code => $name) { + $options[] = ['id' => $code, 'name' => $name]; + } + return $options; + } + private function getOrderPaymentOptions(): array { $gateways = WC()->payment_gateways()->get_available_payment_gateways(); $options = []; diff --git a/mailpoet/tests/integration/Automation/Integrations/WooCommerce/Fields/OrderFieldsFactoryTest.php b/mailpoet/tests/integration/Automation/Integrations/WooCommerce/Fields/OrderFieldsFactoryTest.php index 910a3d4349..1772a37cf6 100644 --- a/mailpoet/tests/integration/Automation/Integrations/WooCommerce/Fields/OrderFieldsFactoryTest.php +++ b/mailpoet/tests/integration/Automation/Integrations/WooCommerce/Fields/OrderFieldsFactoryTest.php @@ -15,6 +15,11 @@ use WP_Term; */ class OrderFieldsFactoryTest extends \MailPoetTest { public function testBillingInfo(): void { + // set specific countries + $wp = $this->diContainer->get(WPFunctions::class); + $wp->updateOption('woocommerce_allowed_countries', 'specific'); + $wp->updateOption('woocommerce_specific_allowed_countries', ['CZ', 'DE']); + $fields = $this->getFieldsMap(); // check definitions @@ -45,8 +50,13 @@ class OrderFieldsFactoryTest extends \MailPoetTest { $countryField = $fields['woocommerce:order:billing-country']; $this->assertSame('Billing country', $countryField->getName()); - $this->assertSame('string', $countryField->getType()); - $this->assertSame([], $countryField->getArgs()); + $this->assertSame('enum', $countryField->getType()); + $this->assertSame([ + 'options' => [ + ['id' => 'CZ', 'name' => 'Czech Republic'], + ['id' => 'DE', 'name' => 'Germany'], + ], + ], $countryField->getArgs()); // check values $order = new WC_Order(); @@ -55,7 +65,7 @@ class OrderFieldsFactoryTest extends \MailPoetTest { $order->set_billing_city('Test billing city'); $order->set_billing_postcode('12345'); $order->set_billing_state('Test billing state'); - $order->set_billing_country('Test billing country'); + $order->set_billing_country('CZ'); $payload = new OrderPayload($order); $this->assertSame('Test billing company', $companyField->getValue($payload)); @@ -63,10 +73,15 @@ class OrderFieldsFactoryTest extends \MailPoetTest { $this->assertSame('Test billing city', $cityField->getValue($payload)); $this->assertSame('12345', $postcodeField->getValue($payload)); $this->assertSame('Test billing state', $stateField->getValue($payload)); - $this->assertSame('Test billing country', $countryField->getValue($payload)); + $this->assertSame('CZ', $countryField->getValue($payload)); } public function testShippingInfo(): void { + // set specific countries + $wp = $this->diContainer->get(WPFunctions::class); + $wp->updateOption('woocommerce_ship_to_countries', 'specific'); + $wp->updateOption('woocommerce_specific_ship_to_countries', ['GR', 'SK']); + $fields = $this->getFieldsMap(); // check definitions @@ -97,8 +112,13 @@ class OrderFieldsFactoryTest extends \MailPoetTest { $countryField = $fields['woocommerce:order:shipping-country']; $this->assertSame('Shipping country', $countryField->getName()); - $this->assertSame('string', $countryField->getType()); - $this->assertSame([], $countryField->getArgs()); + $this->assertSame('enum', $countryField->getType()); + $this->assertSame([ + 'options' => [ + ['id' => 'GR', 'name' => 'Greece'], + ['id' => 'SK', 'name' => 'Slovakia'], + ], + ], $countryField->getArgs()); // check values $order = new WC_Order(); @@ -107,7 +127,7 @@ class OrderFieldsFactoryTest extends \MailPoetTest { $order->set_shipping_city('Test shipping city'); $order->set_shipping_postcode('12345'); $order->set_shipping_state('Test shipping state'); - $order->set_shipping_country('Test shipping country'); + $order->set_shipping_country('GR'); $payload = new OrderPayload($order); $this->assertSame('Test shipping company', $companyField->getValue($payload)); @@ -115,7 +135,7 @@ class OrderFieldsFactoryTest extends \MailPoetTest { $this->assertSame('Test shipping city', $cityField->getValue($payload)); $this->assertSame('12345', $postcodeField->getValue($payload)); $this->assertSame('Test shipping state', $stateField->getValue($payload)); - $this->assertSame('Test shipping country', $countryField->getValue($payload)); + $this->assertSame('GR', $countryField->getValue($payload)); } public function testCreatedDateField(): void {