LLMS_Order::init( array|LLMS_Student|WP_User|integer $user_data, LLMS_Access_Plan $plan, LLMS_Payment_Gateway $gateway, LLMS_Coupon $coupon = false )
Initializes a new order with user, plan, gateway, and coupon metadata.
Description Description
Assumes all data passed in has already been validated.
Parameters Parameters
- $user_data
-
(array|LLMS_Student|WP_User|integer) (Required) User info for the person placing the order. See LLMS_Order::set_user_data() for more info.
- $plan
-
(LLMS_Access_Plan) (Required) The purchase access plan.
- $gateway
-
(LLMS_Payment_Gateway) (Required) Gateway being used.
- $coupon
-
(LLMS_Coupon) (Optional) Coupon object or
falseif no coupon used.Default value: false
Return Return
Source Source
File: includes/models/model.llms.order.php
public function init( $user_data, $plan, $gateway, $coupon = false ) {
$this->set_user_data( $user_data );
// Access plan data.
$this->set( 'plan_id', $plan->get( 'id' ) );
$this->set( 'plan_title', $plan->get( 'title' ) );
$this->set( 'plan_sku', $plan->get( 'sku' ) );
// Product data.
$product = $plan->get_product();
$this->set( 'product_id', $product->get( 'id' ) );
$this->set( 'product_title', $product->get( 'title' ) );
$this->set( 'product_sku', $product->get( 'sku' ) );
$this->set( 'product_type', $plan->get_product_type() );
$this->set( 'payment_gateway', $gateway->get_id() );
$this->set( 'gateway_api_mode', $gateway->get_api_mode() );
// Trial data.
if ( $plan->has_trial() ) {
$this->set( 'trial_offer', 'yes' );
$this->set( 'trial_length', $plan->get( 'trial_length' ) );
$this->set( 'trial_period', $plan->get( 'trial_period' ) );
$trial_price = $plan->get_price( 'trial_price', array(), 'float' );
$this->set( 'trial_original_total', $trial_price );
$trial_total = $coupon ? $plan->get_price_with_coupon( 'trial_price', $coupon, array(), 'float' ) : $trial_price;
$this->set( 'trial_total', $trial_total );
$this->set( 'date_trial_end', $this->calculate_trial_end_date() );
} else {
$this->set( 'trial_offer', 'no' );
}
$price = $plan->get_price( 'price', array(), 'float' );
$this->set( 'currency', get_lifterlms_currency() );
// Price data.
if ( $plan->is_on_sale() ) {
$price_key = 'sale_price';
$this->set( 'on_sale', 'yes' );
$sale_price = $plan->get( 'sale_price', array(), 'float' );
$this->set( 'sale_price', $sale_price );
$this->set( 'sale_value', $price - $sale_price );
} else {
$price_key = 'price';
$this->set( 'on_sale', 'no' );
}
// Store original total before any discounts.
$this->set( 'original_total', $price );
// Get the actual total due after discounts if any are applicable.
$total = $coupon ? $plan->get_price_with_coupon( $price_key, $coupon, array(), 'float' ) : $$price_key;
$this->set( 'total', $total );
// Coupon data.
if ( $coupon ) {
$this->set( 'coupon_id', $coupon->get( 'id' ) );
$this->set( 'coupon_amount', $coupon->get( 'coupon_amount' ) );
$this->set( 'coupon_code', $coupon->get( 'title' ) );
$this->set( 'coupon_type', $coupon->get( 'discount_type' ) );
$this->set( 'coupon_used', 'yes' );
$this->set( 'coupon_value', $$price_key - $total );
if ( $plan->has_trial() && $coupon->has_trial_discount() ) {
$this->set( 'coupon_amount_trial', $coupon->get( 'trial_amount' ) );
$this->set( 'coupon_value_trial', $trial_price - $trial_total );
}
} else {
$this->set( 'coupon_used', 'no' );
}
// Get all billing schedule related information.
$this->set( 'billing_frequency', $plan->get( 'frequency' ) );
if ( $plan->is_recurring() ) {
$this->set( 'billing_length', $plan->get( 'length' ) );
$this->set( 'billing_period', $plan->get( 'period' ) );
$this->set( 'order_type', 'recurring' );
$this->set( 'date_next_payment', $this->calculate_next_payment_date() );
} else {
$this->set( 'order_type', 'single' );
}
$this->set( 'access_expiration', $plan->get( 'access_expiration' ) );
// Get access related data so when payment is complete we can calculate the actual expiration date.
if ( $plan->can_expire() ) {
$this->set( 'access_expires', $plan->get( 'access_expires' ) );
$this->set( 'access_length', $plan->get( 'access_length' ) );
$this->set( 'access_period', $plan->get( 'access_period' ) );
}
/**
* Action triggered after the order is initialized.
*
* @since Unknown.
* @since 7.0.0 Added `$user_data` parameter.
* The `$student` parameter returns an "empty" student object
* if the method's input data is an array instead of an existing
* user object.
*
* @param LLMS_Order $order The order object.
* @param LLMS_Student $student The student object. If an array of data is passed
* to `LLMS_Order::init()` then an empty student object
* will be passed.
* @param array|LLMS_Student|WP_User|integer $user_data User data.
*/
do_action(
'lifterlms_new_pending_order',
$this,
is_array( $user_data ) ? new LLMS_Student( null, false ) : llms_get_student( $user_data ),
$user_data
);
return $this;
}
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
| Version | Description |
|---|---|
| 7.0.0 | Use LLMS_Order::set_user_data() to update user data. |
| 5.3.0 | Don't set unused legacy property date_billing_end. |
| 3.8.0 | |
| 3.10.0 | Introduced. |