Abstract_WC_Order_Data_Store_CPT::update()publicWC 1.0

Method to update an order in the database.

Method of the class: Abstract_WC_Order_Data_Store_CPT{}

No Hooks.

Return

null. Nothing (null).

Usage

$Abstract_WC_Order_Data_Store_CPT = new Abstract_WC_Order_Data_Store_CPT();
$Abstract_WC_Order_Data_Store_CPT->update( $order );
$order(WC_Order) (required) (passed by reference — &)
Order object.

Abstract_WC_Order_Data_Store_CPT::update() code WC 9.4.2

public function update( &$order ) {
	$order->save_meta_data();
	$order->set_version( Constants::get_constant( 'WC_VERSION' ) );

	if ( null === $order->get_date_created( 'edit' ) ) {
		$order->set_date_created( time() );
	}

	$changes = $order->get_changes();

	// Only update the post when the post data changes.
	if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'post_excerpt' ), array_keys( $changes ) ) ) {
		$post_data = array(
			'post_date'         => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getOffsetTimestamp() ),
			'post_date_gmt'     => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ),
			'post_status'       => $this->get_post_status( $order ),
			'post_parent'       => $order->get_parent_id(),
			'post_excerpt'      => $this->get_post_excerpt( $order ),
			'post_modified'     => isset( $changes['date_modified'] ) ? gmdate( 'Y-m-d H:i:s', $order->get_date_modified( 'edit' )->getOffsetTimestamp() ) : current_time( 'mysql' ),
			'post_modified_gmt' => isset( $changes['date_modified'] ) ? gmdate( 'Y-m-d H:i:s', $order->get_date_modified( 'edit' )->getTimestamp() ) : current_time( 'mysql', 1 ),
		);

		/**
		 * When updating this object, to prevent infinite loops, use $wpdb
		 * to update data, since wp_update_post spawns more calls to the
		 * save_post action.
		 *
		 * This ensures hooks are fired by either WP itself (admin screen save),
		 * or an update purely from CRUD.
		 */
		if ( doing_action( 'save_post' ) ) {
			$GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $order->get_id() ) );
			clean_post_cache( $order->get_id() );
		} else {
			wp_update_post( array_merge( array( 'ID' => $order->get_id() ), $post_data ) );
		}
		$order->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
	}
	$this->update_post_meta( $order );
	$order->apply_changes();
	$this->clear_caches( $order );
}