Skip to content

Commit

Permalink
Prevent missing items in content weight/dimension calculation when us…
Browse files Browse the repository at this point in the history
…ing item->id although not yet existent.
  • Loading branch information
dennisnissle committed Aug 30, 2024
1 parent cf04693 commit d34d8f2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand All @@ -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 ) ) {
Expand All @@ -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 ) ) {
Expand All @@ -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 ) ) {
Expand All @@ -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 ) ) {
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/ShipmentItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

/**
Expand All @@ -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();
}
}

/**
Expand All @@ -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();
}
}

/**
Expand All @@ -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' ) {
Expand All @@ -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 ) {
Expand Down
23 changes: 23 additions & 0 deletions tests/unit-tests/shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit d34d8f2

Please sign in to comment.