@@ -224,7 +224,41 @@ protected function get_rate_properties() {
224224 * @return array
225225 */
226226 public function get_item_response ( $ package ) {
227- // Add product names and quantities.
227+ return [
228+ 'package_id ' => $ package ['package_id ' ],
229+ 'name ' => $ package ['package_name ' ],
230+ 'destination ' => $ this ->prepare_package_destination_response ( $ package ),
231+ 'items ' => $ this ->prepare_package_items_response ( $ package ),
232+ 'shipping_rates ' => $ this ->prepare_package_shipping_rates_response ( $ package ),
233+ ];
234+ }
235+
236+ /**
237+ * Gets and formats the destination address of a package.
238+ *
239+ * @param array $package Shipping package complete with rates from WooCommerce.
240+ * @return object
241+ */
242+ protected function prepare_package_destination_response ( $ package ) {
243+ return (object ) $ this ->prepare_html_response (
244+ [
245+ 'address_1 ' => $ package ['destination ' ]['address_1 ' ],
246+ 'address_2 ' => $ package ['destination ' ]['address_2 ' ],
247+ 'city ' => $ package ['destination ' ]['city ' ],
248+ 'state ' => $ package ['destination ' ]['state ' ],
249+ 'postcode ' => $ package ['destination ' ]['postcode ' ],
250+ 'country ' => $ package ['destination ' ]['country ' ],
251+ ]
252+ );
253+ }
254+
255+ /**
256+ * Gets items from a package and creates an array of strings containing product names and quantities.
257+ *
258+ * @param array $package Shipping package complete with rates from WooCommerce.
259+ * @return array
260+ */
261+ protected function prepare_package_items_response ( $ package ) {
228262 $ items = array ();
229263 foreach ( $ package ['contents ' ] as $ item_id => $ values ) {
230264 $ items [] = [
@@ -233,38 +267,7 @@ public function get_item_response( $package ) {
233267 'quantity ' => $ values ['quantity ' ],
234268 ];
235269 }
236-
237- // Generate package name.
238- $ package_number = absint ( $ package ['package_id ' ] ) + 1 ;
239- $ package_display_name = apply_filters (
240- 'woocommerce_shipping_package_name ' ,
241- $ package_number > 1 ?
242- sprintf (
243- /* translators: %d: shipping package number */
244- _x ( 'Shipping %d ' , 'shipping packages ' , 'woo-gutenberg-products-block ' ),
245- $ package_number
246- ) :
247- _x ( 'Shipping ' , 'shipping packages ' , 'woo-gutenberg-products-block ' ),
248- $ package ['package_id ' ],
249- $ package
250- );
251-
252- return [
253- 'package_id ' => $ package ['package_id ' ],
254- 'name ' => $ package_display_name ,
255- 'destination ' => (object ) $ this ->prepare_html_response (
256- [
257- 'address_1 ' => $ package ['destination ' ]['address_1 ' ],
258- 'address_2 ' => $ package ['destination ' ]['address_2 ' ],
259- 'city ' => $ package ['destination ' ]['city ' ],
260- 'state ' => $ package ['destination ' ]['state ' ],
261- 'postcode ' => $ package ['destination ' ]['postcode ' ],
262- 'country ' => $ package ['destination ' ]['country ' ],
263- ]
264- ),
265- 'items ' => $ items ,
266- 'shipping_rates ' => $ this ->prepare_rates_response ( $ package ),
267- ];
270+ return $ items ;
268271 }
269272
270273 /**
@@ -273,10 +276,10 @@ public function get_item_response( $package ) {
273276 * @param array $package Shipping package complete with rates from WooCommerce.
274277 * @return array
275278 */
276- protected function prepare_rates_response ( $ package ) {
279+ protected function prepare_package_shipping_rates_response ( $ package ) {
277280 $ rates = $ package ['rates ' ];
278281 $ selected_rates = wc ()->session ->get ( 'chosen_shipping_methods ' , array () );
279- $ selected_rate = isset ( $ chosen_shipping_methods [ $ package ['package_id ' ] ] ) ? $ chosen_shipping_methods [ $ package ['package_id ' ] ] : '' ;
282+ $ selected_rate = isset ( $ selected_rates [ $ package ['package_id ' ] ] ) ? $ selected_rates [ $ package ['package_id ' ] ] : '' ;
280283
281284 if ( empty ( $ selected_rate ) && ! empty ( $ package ['rates ' ] ) ) {
282285 $ selected_rate = wc_get_chosen_shipping_method_for_package ( $ package ['package_id ' ], $ package );
0 commit comments