diff --git a/src/Shipment.php b/src/Shipment.php index 022a7643..714792ec 100644 --- a/src/Shipment.php +++ b/src/Shipment.php @@ -644,8 +644,8 @@ public function get_item_weights() { if ( is_null( $this->weights ) ) { $this->weights = array(); - foreach ( $this->get_items() as $item ) { - $this->weights[ $item->get_id() ] = ( ( $item->get_weight() === '' ? 0 : $item->get_weight() ) * $item->get_quantity() ); + foreach ( $this->get_items() as $k => $item ) { + $this->weights[ $k ] = ( ( $item->get_weight() === '' ? 0 : $item->get_weight() ) * $item->get_quantity() ); } if ( empty( $this->weights ) ) { @@ -665,8 +665,8 @@ public function get_item_lengths() { if ( is_null( $this->lengths ) ) { $this->lengths = array(); - foreach ( $this->get_items() as $item ) { - $this->lengths[ $item->get_id() ] = $item->get_length() === '' ? 0 : $item->get_length(); + foreach ( $this->get_items() as $k => $item ) { + $this->lengths[ $k ] = $item->get_length() === '' ? 0 : $item->get_length(); } if ( empty( $this->lengths ) ) { @@ -681,11 +681,11 @@ public function get_item_volumes() { if ( is_null( $this->volumes ) ) { $this->volumes = array(); - foreach ( $this->get_items() as $item ) { + foreach ( $this->get_items() as $k => $item ) { $dimensions = $item->get_dimensions(); $volume = ( '' !== $dimensions['length'] ? (float) $dimensions['length'] : 0 ) * ( '' !== $dimensions['width'] ? (float) $dimensions['width'] : 0 ) * ( '' !== $dimensions['height'] ? (float) $dimensions['height'] : 0 ); - $this->volumes[ $item->get_id() ] = $volume * (float) $item->get_quantity(); + $this->volumes[ $k ] = $volume * (float) $item->get_quantity(); } if ( empty( $this->volumes ) ) { @@ -705,8 +705,8 @@ public function get_item_widths() { if ( is_null( $this->widths ) ) { $this->widths = array(); - foreach ( $this->get_items() as $item ) { - $this->widths[ $item->get_id() ] = $item->get_width() === '' ? 0 : $item->get_width(); + foreach ( $this->get_items() as $k => $item ) { + $this->widths[ $k ] = $item->get_width() === '' ? 0 : $item->get_width(); } if ( empty( $this->widths ) ) { @@ -726,8 +726,8 @@ public function get_item_heights() { if ( is_null( $this->heights ) ) { $this->heights = array(); - foreach ( $this->get_items() as $item ) { - $this->heights[ $item->get_id() ] = ( $item->get_height() === '' ? 0 : $item->get_height() ) * $item->get_quantity(); + foreach ( $this->get_items() as $k => $item ) { + $this->heights[ $k ] = ( $item->get_height() === '' ? 0 : $item->get_height() ) * $item->get_quantity(); } if ( empty( $this->heights ) ) { @@ -2291,7 +2291,7 @@ public function add_item( $item ) { /** * Reset item content data. */ - protected function reset_content_data() { + public function reset_content_data() { $this->weights = null; $this->lengths = null; $this->widths = null; diff --git a/src/ShipmentItem.php b/src/ShipmentItem.php index d46bfb27..5a66a2f8 100644 --- a/src/ShipmentItem.php +++ b/src/ShipmentItem.php @@ -668,6 +668,10 @@ public function set_sku( $sku ) { */ public function set_weight( $weight ) { $this->set_prop( 'weight', '' === $weight ? '' : wc_format_decimal( $weight ) ); + + if ( $shipment = $this->get_shipment() ) { + $shipment->reset_content_data(); + } } /** @@ -677,6 +681,10 @@ public function set_weight( $weight ) { */ public function set_width( $width ) { $this->set_prop( 'width', '' === $width ? '' : wc_format_decimal( $width ) ); + + if ( $shipment = $this->get_shipment() ) { + $shipment->reset_content_data(); + } } /** @@ -686,6 +694,10 @@ public function set_width( $width ) { */ public function set_length( $length ) { $this->set_prop( 'length', '' === $length ? '' : wc_format_decimal( $length ) ); + + if ( $shipment = $this->get_shipment() ) { + $shipment->reset_content_data(); + } } /** @@ -695,6 +707,10 @@ public function set_length( $length ) { */ public function set_height( $height ) { $this->set_prop( 'height', '' === $height ? '' : wc_format_decimal( $height ) ); + + if ( $shipment = $this->get_shipment() ) { + $shipment->reset_content_data(); + } } public function get_dimensions( $context = 'view' ) { @@ -707,6 +723,10 @@ public function get_dimensions( $context = 'view' ) { public function set_quantity( $quantity ) { $this->set_prop( 'quantity', absint( $quantity ) ); + + if ( $shipment = $this->get_shipment() ) { + $shipment->reset_content_data(); + } } public function set_name( $name ) { diff --git a/tests/unit-tests/shipment.php b/tests/unit-tests/shipment.php index ef5188a7..659afdce 100644 --- a/tests/unit-tests/shipment.php +++ b/tests/unit-tests/shipment.php @@ -28,6 +28,29 @@ function test_shipment_weight() { $this->assertEquals( 25, $shipment->get_total_weight() ); } + function test_shipment_content_weight() { + $shipment = new \Vendidero\Germanized\Shipments\SimpleShipment(); + + $item = new \Vendidero\Germanized\Shipments\ShipmentItem(); + $item->set_weight( 1.5 ); + + $item_2 = new \Vendidero\Germanized\Shipments\ShipmentItem(); + $item_2->set_quantity( 3 ); + $item_2->set_weight( 5 ); + + $shipment->add_item( $item ); + $shipment->add_item( $item_2 ); + + $this->assertEquals( 16.5, $shipment->get_content_weight() ); + + $item_2->set_quantity( 2 ); + $this->assertEquals( 11.5, $shipment->get_content_weight() ); + + $item_2->set_quantity( 3 ); + $item_2->set_weight( 3 ); + $this->assertEquals( 10.5, $shipment->get_content_weight() ); + } + function test_sync_shipment() { $shipment = ShipmentHelper::create_simple_shipment(); $shipment->set_packaging_id( 0 );