Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit d35d2c8

Browse files
authored
Improve Package Handling in CartShippingRateSchema (#3680)
* Generate package names when geting them from the cart * Split package formatting methods * Fix variable name
1 parent 987b394 commit d35d2c8

File tree

2 files changed

+72
-40
lines changed

2 files changed

+72
-40
lines changed

src/StoreApi/Schemas/CartShippingRateSchema.php

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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 );

src/StoreApi/Utilities/CartController.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,16 +473,45 @@ public function get_shipping_packages( $calculate_rates = true ) {
473473

474474
$packages = $cart->get_shipping_packages();
475475

476-
// Add package ID to array.
477-
foreach ( $packages as $key => $package ) {
478-
if ( ! isset( $packages[ $key ]['package_id'] ) ) {
479-
$packages[ $key ]['package_id'] = $key;
480-
}
476+
// Add extra package data to array.
477+
if ( count( $packages ) ) {
478+
$packages = array_map(
479+
function( $key, $package, $index ) {
480+
$package['package_id'] = isset( $package['package_id'] ) ? $package['package_id'] : $key;
481+
$package['package_name'] = isset( $package['package_name'] ) ? $package['package_name'] : $this->get_package_name( $package, $index );
482+
return $package;
483+
},
484+
array_keys( $packages ),
485+
$packages,
486+
range( 1, count( $packages ) )
487+
);
481488
}
482489

483490
return $calculate_rates ? wc()->shipping()->calculate_shipping( $packages ) : $packages;
484491
}
485492

493+
/**
494+
* Creates a name for a package.
495+
*
496+
* @param array $package Shipping package from WooCommerce.
497+
* @param int $index Package number.
498+
* @return string
499+
*/
500+
protected function get_package_name( $package, $index ) {
501+
return apply_filters(
502+
'woocommerce_shipping_package_name',
503+
$index > 1 ?
504+
sprintf(
505+
/* translators: %d: shipping package number */
506+
_x( 'Shipping %d', 'shipping packages', 'woo-gutenberg-products-block' ),
507+
$index
508+
) :
509+
_x( 'Shipping', 'shipping packages', 'woo-gutenberg-products-block' ),
510+
$package['package_id'],
511+
$package
512+
);
513+
}
514+
486515
/**
487516
* Selects a shipping rate.
488517
*

0 commit comments

Comments
 (0)