Delete child orders using existing functions instead of direct db access.

Additionally, add get/set_verify_parent_id methods to the order object.
This commit is contained in:
Nestor Soriano 2023-05-11 12:48:21 +02:00
parent 2ff1c70f9d
commit a0a2390d9e
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
2 changed files with 40 additions and 3 deletions

View File

@ -103,6 +103,14 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
*/
protected $object_type = 'order';
/**
* Indicates if set_parent_id will throw an error if no order exists with the supplied id.
*
* @since 7.8.0
* @var bool
*/
private $verify_parent_id = true;
/**
* Get the order if ID is passed, otherwise the order is new and empty.
* This class should NOT be instantiated, but the wc_get_order function or new WC_Order_Factory
@ -608,15 +616,38 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
*
* @since 3.0.0
* @param int $value Value to set.
* @throws WC_Data_Exception Exception thrown if parent ID does not exist or is invalid.
* @throws WC_Data_Exception Exception thrown if parent ID does not exist or is invalid (only if $verify_parent_id is true).
*/
public function set_parent_id( $value ) {
if ( $value && ( $value === $this->get_id() || ! wc_get_order( $value ) ) ) {
if ( $this->verify_parent_id && $value && ( $value === $this->get_id() || ! wc_get_order( $value ) ) ) {
$this->error( 'order_invalid_parent_id', __( 'Invalid parent ID', 'woocommerce' ) );
}
$this->set_prop( 'parent_id', absint( $value ) );
}
/**
* Sets the value of the $verify_parent_id flag.
*
* If the flag is set, set_parent_id will throw an error if no order exists with the supplied id.
*
* @param bool $value The value to set.
* @return void
*/
public function set_verify_parent_id( bool $value ) {
$this->verify_parent_id = $value;
}
/**
* Gets the value of the $verify_parent_id flag.
*
* If the flag is set, set_parent_id will throw an error if no order exists with the supplied id.
*
* @return bool
*/
public function get_verify_parent_id(): bool {
return $this->verify_parent_id;
}
/**
* Set order status.
*

View File

@ -1860,8 +1860,14 @@ FROM $order_meta_table
$order->get_id()
)
);
foreach ( $child_order_ids as $child_order_id ) {
$this->delete_order_data_from_custom_order_tables( $child_order_id );
// We can't use wc_get_order here because we would get a "Invalid parent ID" error
// (and we don't need to load the full order from the database anyway).
$child_order = new \WC_Order();
$child_order->set_verify_parent_id( false );
$child_order->set_id( $child_order_id );
$child_order->delete( true );
}
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}