Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use delete query format suppored by wpdb::get_table_from_query() in Abstract_WC_Order_Data_Store_CPT::delete_items() #46692

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Update delete item meta query to format supported by wpdb::get_table_from_query()
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,15 @@ function ( $order_items_collection, $order_item ) {
*/
public function delete_items( $order, $type = null ) {
global $wpdb;

if ( ! empty( $type ) ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id AND items.order_id = %d AND items.order_item_type = %s", $order->get_id(), $type ) );
$wpdb->query( $wpdb->prepare( "DELETE itemmeta FROM {$wpdb->prefix}woocommerce_order_itemmeta as itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items as items WHERE itemmeta.order_item_id = items.order_item_id AND items.order_id = %d AND items.order_item_type = %s", $order->get_id(), $type ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s", $order->get_id(), $type ) );
} else {
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id and items.order_id = %d", $order->get_id() ) );
$wpdb->query( $wpdb->prepare( "DELETE itemmeta FROM {$wpdb->prefix}woocommerce_order_itemmeta as itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items as items WHERE itemmeta.order_item_id = items.order_item_id and items.order_id = %d", $order->get_id() ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $order->get_id() ) );
}

$this->clear_caches( $order );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,87 @@ function ( $order_id ) use ( &$order_id_from_after_delete ) {
$this->assertSame( $order, $order_from_before_delete );
}
}

/**
* @testDox Deleting order items should only delete items of the specified type.
*/
public function test_delete_items() {
$order = WC_Helper_Order::create_order();
$product = WC_Helper_Product::create_simple_product();
$product_item = new WC_Order_Item_Product();
$product_item->set_product( $product );
$product_item->set_quantity( 1 );
$product_item->save();

$fee_item_1 = new WC_Order_Item_Fee();
$fee_item_1->set_amount( 20 );
$fee_item_1->save();

$fee_item_2 = new WC_Order_Item_Fee();
$fee_item_2->set_amount( 30 );
$fee_item_2->save();

$shipping_item = new WC_Order_Item_Shipping();
$shipping_item->set_name( 'dummy shipping' );
$shipping_item->set_total( 20 );
$shipping_item->save();

$order->add_item( $product_item );
$order->add_item( $fee_item_1 );
$order->add_item( $fee_item_2 );
$order->add_item( $shipping_item );

$order->save();

$r_order = wc_get_order( $order->get_id() );
$this->assertTrue( $r_order->get_item( $fee_item_1->get_id() )->get_id() === $fee_item_1->get_id() );
$this->assertTrue( $r_order->get_item( $fee_item_2->get_id() )->get_id() === $fee_item_2->get_id() );
$this->assertTrue( $r_order->get_item( $product_item->get_id() )->get_id() === $product_item->get_id() );
$this->assertTrue( $r_order->get_item( $shipping_item->get_id() )->get_id() === $shipping_item->get_id() );

// Deleting single item type should only delete that item type.
$r_order->get_data_store()->delete_items( $r_order, $fee_item_1->get_type() );
$this->assertFalse( $r_order->get_item( $fee_item_1->get_id() ) );
$this->assertFalse( $r_order->get_item( $fee_item_2->get_id() ) );
$this->assertTrue( $r_order->get_item( $product_item->get_id() )->get_id() === $product_item->get_id() );
$this->assertTrue( $r_order->get_item( $shipping_item->get_id() )->get_id() === $shipping_item->get_id() );

// Deleting all items should all items.
$r_order->get_data_store()->delete_items( $r_order );
$this->assertFalse( $r_order->get_item( $fee_item_1->get_id() ) );
$this->assertFalse( $r_order->get_item( $fee_item_2->get_id() ) );
$this->assertFalse( $r_order->get_item( $product_item->get_id() ) );
$this->assertFalse( $r_order->get_item( $shipping_item->get_id() ) );
}

/**
* @testDox Deleting order item should delete items from only that order.
*/
public function test_delete_items_multi_order() {
$order_1 = WC_Helper_Order::create_order();
$product = WC_Helper_Product::create_simple_product();
$product_item_1 = new WC_Order_Item_Product();
$product_item_1->set_product( $product );
$product_item_1->set_quantity( 1 );
$product_item_1->save();

$order_2 = WC_Helper_Order::create_order();
$product_item_2 = new WC_Order_Item_Product();
$product_item_2->set_product( $product );
$product_item_2->set_quantity( 1 );
$product_item_2->save();

$order_1->add_item( $product_item_1 );
$order_1->save();
$order_2->add_item( $product_item_2 );
$order_2->save();

$this->assertTrue( $order_1->get_item( $product_item_1->get_id() )->get_id() === $product_item_1->get_id() );
$this->assertTrue( $order_2->get_item( $product_item_2->get_id() )->get_id() === $product_item_2->get_id() );

$order_1->get_data_store()->delete_items( $order_1 );

$this->assertFalse( $order_1->get_item( $product_item_1->get_id() ) );
$this->assertTrue( $order_2->get_item( $product_item_2->get_id() )->get_id() === $product_item_2->get_id() );
}
}