Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Triggered when payment for membership / product is complete (for one-time or free billing)
- *
- * @access public
- * @return void
- */
- public function apply_tags_checkout( $event ) {
- // The mepr-signup hook passes a transaction already
- if ( is_a( $event, 'MeprTransaction' ) ) {
- $txn = $event;
- } else {
- $txn = $event->get_data();
- }
- if ( 'complete' != $txn->status ) {
- return;
- }
- // No need to run this twice if another action fires
- remove_action( 'mepr-signup', array( $this, 'apply_tags_checkout' ) );
- remove_action( 'mepr-event-transaction-completed', array( $this, 'apply_tags_checkout' ) );
- remove_action( 'mepr-txn-status-complete', array( $this, 'apply_tags_checkout' ) );
- // Log the action
- $action = 'unknown action';
- if ( doing_action( 'mepr-signup' ) ) {
- $action = 'mepr-signup';
- } elseif ( doing_action( 'mepr-event-transaction-completed' ) ) {
- $action = 'mepr-event-transaction-completed';
- } elseif ( doing_action( 'mepr-txn-status-complete' ) ) {
- $action = 'mepr-txn-status-complete';
- }
- // Logger
- wpf_log( 'info', $txn->user_id, 'New MemberPress transaction <a href="' . admin_url( 'admin.php?page=memberpress-trans&action=edit&id=' . $txn->id ) . '" target="_blank">#' . $txn->id . '</a> (' . $action . ')' );
- //
- // Get meta fields
- //
- $payment_method = $txn->payment_method();
- $product_id = $txn->product_id;
- $update_data = array(
- 'mepr_membership_level' => get_the_title( $product_id ),
- 'mepr_reg_date' => $txn->created_at,
- 'mepr_payment_method' => $payment_method->name,
- );
- // Add expiration only if applicable
- if ( strtotime( $txn->expires_at ) >= 0 ) {
- $update_data['mepr_expiration'] = $txn->expires_at;
- }
- // Coupons
- if ( ! empty( $txn->coupon_id ) ) {
- $update_data['mepr_coupon'] = get_the_title( $txn->coupon_id );
- }
- // Push all meta as well to get any updated custom field values during upgrades
- $user_meta = array_map( array( wp_fusion()->user, 'map_user_meta' ), get_user_meta( $txn->user_id ) );
- $update_data = array_merge( $user_meta, $update_data );
- wp_fusion()->user->push_user_meta( $txn->user_id, $update_data );
- //
- // Update tags based on the product purchased
- //
- $apply_tags = array();
- $settings = get_post_meta( $txn->product_id, 'wpf-settings-memberpress', true );
- if ( ! empty( $settings ) ) {
- if ( ! empty( $settings['apply_tags_registration'] ) ) {
- $apply_tags = array_merge( $apply_tags, $settings['apply_tags_registration'] );
- }
- if ( ! empty( $settings['tag_link'] ) ) {
- $apply_tags = array_merge( $apply_tags, $settings['tag_link'] );
- }
- if ( ! empty( $settings['apply_tags_payment_failed'] ) ) {
- // Remove any failed tags
- remove_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
- wp_fusion()->user->remove_tags( $settings['apply_tags_payment_failed'], $txn->user_id );
- add_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
- }
- // If this transaction was against a subscription that had a trial, and is no longer in a trial, consider it "converted"
- $subscription = $txn->subscription();
- if ( false !== $subscription && true == $subscription->trial ) {
- // Figure out if it's the first real payment
- $first_payment = false;
- if ( $subscription->trial_amount > 0.00 && $subscription->txn_count == 2 ) {
- $first_payment = true;
- } elseif ( $subscription->trial_amount == 0.00 && $subscription->txn_count == 1 ) {
- $first_payment = true;
- }
- if ( true == $first_payment && ! empty( $settings['apply_tags_converted'] ) ) {
- $apply_tags = array_merge( $apply_tags, $settings['apply_tags_converted'] );
- }
- }
- }
- // Coupons
- if ( ! empty( $txn->coupon_id ) ) {
- $coupon_settings = get_post_meta( $txn->coupon_id, 'wpf-settings', true );
- if ( ! empty( $coupon_settings ) && ! empty( $coupon_settings['apply_tags_coupon'] ) ) {
- $apply_tags = array_merge( $apply_tags, $coupon_settings['apply_tags_coupon'] );
- }
- }
- // Corporate accounts
- $corporate_account = get_user_meta( $txn->user_id, 'mpca_corporate_account_id', true );
- if ( ! empty( $corporate_account ) && ! empty( $settings['apply_tags_corporate_accounts'] ) ) {
- $apply_tags = array_merge( $apply_tags, $coupon_settings['apply_tags_corporate_accounts'] );
- }
- if ( ! empty( $apply_tags ) ) {
- // Prevent looping when tags are applied
- remove_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
- wp_fusion()->user->apply_tags( $apply_tags, $txn->user_id );
- add_action( 'wpf_tags_modified', array( $this, 'add_to_membership' ), 10, 2 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement