llms_update_300_update_orders()

Migrate all orders from the 2.x to 3.x data structure


Return Return

(void)


Top ↑

Source Source

File: includes/functions/updates/llms-functions-updates-300.php

function llms_update_300_update_orders() {

	$args = array(
		'post_type'      => array( 'llms_order' ),
		'posts_per_page' => -1,
		'status'         => 'publish',
	);

	$orders = new WP_Query( $args );

	if ( $orders->have_posts() ) {
		foreach ( $orders->posts as $post ) {

			$order = new LLMS_Order( $post );

			// Add an order key.
			$order->set( 'order_key', $order->generate_order_key() );

			$order->set( 'access_expiration', 'lifetime' );

			// Add coupon used info.
			$coupon_used = $order->get( 'coupon_id' ) ? 'yes' : 'no';
			$order->set( 'coupon_used', $coupon_used );

			// Add data about the user to the order if we can find it.
			if ( isset( $order->user_id ) ) {

				$id = $order->get( 'user_id' );

				if ( $id && get_user_by( 'ID', $id ) ) {

					$student = new LLMS_Student( $id );

					$metas = array(
						'billing_address_1'  => 'billing_address_1',
						'billing_address_2'  => 'billing_address_2',
						'billing_city'       => 'billing_city',
						'billing_country'    => 'billing_country',
						'billing_email'      => 'user_email',
						'billing_first_name' => 'first_name',
						'billing_last_name'  => 'last_name',
						'billing_state'      => 'billing_state',
						'billing_zip'        => 'billing_zip',
					);

					foreach ( $metas as $ordermeta => $usermeta ) {

						$v = $student->$usermeta;
						if ( $v ) {

							$order->set( $ordermeta, $v );

						}
					}
				}
			}

			// Setup trial info if there was a first payment recorded.
			if ( $order->get( 'trial_total' ) ) {

				$order->set( 'trial_offer', 'yes' );
				$order->set( 'trial_length', $order->get( 'billing_length' ) );
				$order->set( 'trial_period', $order->get( 'billing_period' ) );
				$order->set( 'trial_original_total', $order->get( 'trial_total' ) );

			} else {

				$order->set( 'trial_offer', 'no' );

			}

			$total = $order->is_recurring() ? get_post_meta( $post->ID, '_llms_order_recurring_price', true ) : get_post_meta( $post->ID, '_llms_order_total', true );
			$order->set( 'original_total', $total );
			$order->set( 'total', $total );

			$order->add_note( sprintf( __( 'This order was migrated to the LifterLMS 3.0 data structure. %1$sLearn more%2$s.', 'lifterlms' ), '<a href="https://lifterlms.com/docs/lifterlms-orders#migration" target="_blank">', '</a>' ) );

			// Remove deprecated.
			delete_post_meta( $post->ID, '_llms_order_recurring_price' );
			delete_post_meta( $post->ID, '_llms_order_total' );
			delete_post_meta( $post->ID, '_llms_order_coupon_limit' );
			delete_post_meta( $post->ID, '_llms_order_product_price' );
			delete_post_meta( $post->ID, '_llms_order_billing_start_date' );
			delete_post_meta( $post->ID, '_llms_order_coupon_value' );
			delete_post_meta( $post->ID, '_llms_order_original_total' );

		}
	}
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.