Skip to content

Commit ace3ac2

Browse files
authored
Merge pull request #3463 from woocommerce/PCP-4808-update-shipping-entity-to-include-contact-fields-email-phone
Update Shipping entity to include contact fields (email, phone) (4808)
2 parents 1dc797b + 59d9f0f commit ace3ac2

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

modules/ppcp-api-client/src/Entity/Shipping.php

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ class Shipping {
2828
*/
2929
private $address;
3030

31+
/**
32+
* Custom contact email address, usually added via the Contact Module.
33+
*/
34+
private ?string $email_address = null;
35+
36+
/**
37+
* Custom contact phone number, usually added via the Contact Module.
38+
*/
39+
private ?Phone $phone_number = null;
40+
3141
/**
3242
* Shipping methods.
3343
*
@@ -38,14 +48,18 @@ class Shipping {
3848
/**
3949
* Shipping constructor.
4050
*
41-
* @param string $name The name.
42-
* @param Address $address The address.
43-
* @param ShippingOption[] $options Shipping methods.
51+
* @param string $name The name.
52+
* @param Address $address The address.
53+
* @param string|null $email_address Contact email.
54+
* @param Phone|null $phone_number Contact phone.
55+
* @param ShippingOption[] $options Shipping methods.
4456
*/
45-
public function __construct( string $name, Address $address, array $options = array() ) {
46-
$this->name = $name;
47-
$this->address = $address;
48-
$this->options = $options;
57+
public function __construct( string $name, Address $address, string $email_address = null, Phone $phone_number = null, array $options = array() ) {
58+
$this->name = $name;
59+
$this->address = $address;
60+
$this->email_address = $email_address;
61+
$this->phone_number = $phone_number;
62+
$this->options = $options;
4963
}
5064

5165
/**
@@ -66,6 +80,24 @@ public function address(): Address {
6680
return $this->address;
6781
}
6882

83+
/**
84+
* Returns the contact email address, or null.
85+
*
86+
* @return null|string
87+
*/
88+
public function email_address() : ?string {
89+
return $this->email_address;
90+
}
91+
92+
/**
93+
* Returns the contact phone number, or null.
94+
*
95+
* @return null|Phone
96+
*/
97+
public function phone_number() : ?Phone {
98+
return $this->phone_number;
99+
}
100+
69101
/**
70102
* Returns the shipping methods.
71103
*
@@ -87,6 +119,17 @@ public function to_array(): array {
87119
),
88120
'address' => $this->address()->to_array(),
89121
);
122+
123+
$contact_email = $this->email_address();
124+
$contact_phone = $this->phone_number();
125+
126+
if ( $contact_email ) {
127+
$result['email_address'] = $contact_email;
128+
}
129+
if ( $contact_phone ) {
130+
$result['phone_number'] = $contact_phone->to_array();
131+
}
132+
90133
if ( $this->options ) {
91134
$result['options'] = array_map(
92135
function ( ShippingOption $opt ): array {

modules/ppcp-api-client/src/Factory/ShippingFactory.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
1111

1212
use WooCommerce\PayPalCommerce\ApiClient\Entity\Shipping;
13-
use WooCommerce\PayPalCommerce\ApiClient\Entity\ShippingOption;
1413
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
14+
use WooCommerce\PayPalCommerce\ApiClient\Entity\Phone;
1515

1616
/**
1717
* Class ShippingFactory
@@ -60,9 +60,12 @@ public function from_wc_customer( \WC_Customer $customer, bool $with_shipping_op
6060
$customer->get_shipping_last_name()
6161
);
6262
$address = $this->address_factory->from_wc_customer( $customer );
63+
6364
return new Shipping(
6465
$full_name,
6566
$address,
67+
null,
68+
null,
6669
$with_shipping_options ? $this->shipping_option_factory->from_wc_cart() : array()
6770
);
6871
}
@@ -77,6 +80,7 @@ public function from_wc_customer( \WC_Customer $customer, bool $with_shipping_op
7780
public function from_wc_order( \WC_Order $order ): Shipping {
7881
$full_name = $order->get_formatted_shipping_full_name();
7982
$address = $this->address_factory->from_wc_order( $order );
83+
8084
return new Shipping(
8185
$full_name,
8286
$address
@@ -102,14 +106,27 @@ public function from_paypal_response( \stdClass $data ): Shipping {
102106
__( 'No address was given for shipping.', 'woocommerce-paypal-payments' )
103107
);
104108
}
105-
$address = $this->address_factory->from_paypal_response( $data->address );
109+
$contact_phone = null;
110+
$contact_email = null;
111+
$address = $this->address_factory->from_paypal_response( $data->address );
112+
106113
$options = array_map(
107114
array( $this->shipping_option_factory, 'from_paypal_response' ),
108115
$data->options ?? array()
109116
);
117+
118+
if ( isset( $data->phone_number->national_number ) ) {
119+
$contact_phone = new Phone( $data->phone_number->national_number );
120+
}
121+
if ( isset( $data->email_address ) ) {
122+
$contact_email = $data->email_address;
123+
}
124+
110125
return new Shipping(
111126
$data->name->full_name,
112127
$address,
128+
$contact_email,
129+
$contact_phone,
113130
$options
114131
);
115132
}

0 commit comments

Comments
 (0)