api = $api; // Apply a discount to logged-in members. add_filter( 'app_get_price_prepare', array( $this, 'get_price' ), 10, 3 ); // Add CSS/JS to the top of the settings form add_action( 'app-settings-after_advanced_settings', array( $this, 'settings_scripts' ) ); // Display integration settings on the payment settings page. add_action( 'app_settings_form_payment', array( $this, 'settings_form' ), 10, 2 ); // Save the custom payment settings. add_filter( 'app-options-before_save', array( $this, 'settings_save' ) ); } /** * Filter that can apply a discount to the appointment price for logged in * members. * * We will check all memberships of the current user to find the highest * discount available for him. * * @since 1.0.0 * @param float $price Default price * @param bool $want_advance If we need the total price or deposit price. * @param Appointments $appointments Caller object. * @return float Modified price */ public function get_price( $price, $want_advance, $appointments ) { $member = $this->api->get_current_member(); $max_discount = 0; $need_deposit = true; foreach ( $member->subscriptions as $subscription ) { $membership = $subscription->get_membership(); // Get the custom data $discount = $membership->get_custom_data( 'app_discount_value' ); $type = $membership->get_custom_data( 'app_discount_type' ); $no_advance = $membership->get_custom_data( 'app_no_advance_payment' ); $discount = floatval( $discount ); // Find the effective discount amount (i.e. convert percentage) switch ( $type ) { case 'per': $discount = $price * $discount / 100; break; case 'abs': // no calculation needed. break; default: // Unrecognized discount types are ignored $discount = 0; break; } if ( $max_discount < $discount ) { $max_discount = $discount; } if ( $no_advance ) { $need_deposit = false; } } if ( $want_advance && ! $need_deposit ) { /* * If we calculate the Advance deposit price but the member is * allowed to book without advance payment then set price to 0. */ $price = 0; } elseif ( $max_discount > 0 ) { /* * If the user is eligable for a Membership discount then calculate * new price. Make sure the price does not go below 0 */ $price -= $max_discount; if ( $price < 0 ) { $price = 0; } } return $price; } /** * Output the payment settings form used by this integration. * * @since 1.0.0 * @param array $options List of all Appintments options. * @param bool $use_payments Current state of the "require payments" flag. */ public function settings_form( $options, $use_payments ) { $row_style = ''; if ( ! $use_payments ) { $row_style = 'display:none;'; } $memberships = $this->api->list_memberships( $list_all = true ); ?>
| /> |