Skip to content

Commit

Permalink
Prevent persisting shipment items before shipment has been saved to m…
Browse files Browse the repository at this point in the history
…ake sure no orphan (shipment_id = 0) items exist in DB. Prevent reading items for shipments with empty id.
  • Loading branch information
dennisnissle committed Jul 16, 2024
1 parent 47e1f42 commit 1ddd2f1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions includes/wc-gzd-shipment-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ function wc_gzd_create_shipment( $order_shipment, $args = array() ) {

function wc_gzd_create_shipment_item( $shipment, $order_item, $args = array() ) {
try {

if ( ! $order_item || ! is_a( $order_item, 'WC_Order_Item' ) ) {
throw new Exception( _x( 'Invalid order item', 'shipments', 'woocommerce-germanized-shipments' ) );
}
Expand All @@ -471,8 +470,10 @@ function wc_gzd_create_shipment_item( $shipment, $order_item, $args = array() )
$item->set_order_item_id( $order_item->get_id() );
$item->set_shipment( $shipment );
$item->sync( $args );
$item->save();

if ( $shipment->get_id() > 0 ) {
$item->save();
}
} catch ( Exception $e ) {
return new WP_Error( 'error', $e->getMessage() );
}
Expand Down
4 changes: 4 additions & 0 deletions src/DataStores/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@ protected function update_or_delete_meta( $object, $meta_key, $meta_value ) {
public function read_items( $shipment ) {
global $wpdb;

if ( $shipment->get_id() <= 0 ) {
return array();
}

// Get from cache if available.
$items = 0 < $shipment->get_id() ? wp_cache_get( 'shipment-items-' . $shipment->get_id(), 'shipments' ) : false;

Expand Down

0 comments on commit 1ddd2f1

Please sign in to comment.