Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. Initialize the 3-phase meta on referral creation
- add_action( 'affwp_referral_created', 'my_initialize_three_phase_referral', 10, 2 );
- function my_initialize_three_phase_referral( $referral_id, $data ) {
- affwp_update_referral_meta( $referral_id, 'phase_1_paid', 0 );
- affwp_update_referral_meta( $referral_id, 'phase_2_paid', 0 );
- affwp_update_referral_meta( $referral_id, 'phase_3_paid', 0 );
- affwp_update_referral_meta( $referral_id, 'total_commission', 50000 );
- affwp_add_referral_note( $referral_id, 'Referral initialized with a 3-phase payout structure (Total: 50,000 Rs).');
- }
- // 2. Add checkboxes to referral edit screen
- add_action( 'affwp_edit_referral_bottom', 'my_add_phase_paid_checkboxes', 10 );
- function my_add_phase_paid_checkboxes( $referral ) {
- $total_commission = affwp_get_referral_meta( $referral->referral_id, 'total_commission', true );
- if ( empty( $total_commission ) ) {
- return;
- }
- $phase_1_paid = affwp_get_referral_meta( $referral->referral_id, 'phase_1_paid', true );
- $phase_2_paid = affwp_get_referral_meta( $referral->referral_id, 'phase_2_paid', true );
- $phase_3_paid = affwp_get_referral_meta( $referral->referral_id, 'phase_3_paid', true );
- ?>
- <h3>3-Phase Payout Status</h3>
- <p>
- <label><input type="checkbox" name="phase_1_paid" value="1" <?php checked( $phase_1_paid, 1 ); ?>> Phase 1 Paid (15,000 Rs)</label><br>
- <label><input type="checkbox" name="phase_2_paid" value="1" <?php checked( $phase_2_paid, 1 ); ?>> Phase 2 Paid (15,000 Rs)</label><br>
- <label><input type="checkbox" name="phase_3_paid" value="1" <?php checked( $phase_3_paid, 1 ); ?>> Phase 3 Paid (20,000 Rs)</label><br>
- </p>
- <?php
- }
- // 3. Save updated phase status on referral update
- add_action( 'affwp_update_referral', 'my_save_phase_paid_fields', 10, 2 );
- function my_save_phase_paid_fields( $referral_id, $data ) {
- $total_commission = affwp_get_referral_meta( $referral_id, 'total_commission', true );
- if ( empty( $total_commission ) ) {
- return;
- }
- $phases = array(
- 1 => array( 'key' => 'phase_1_paid', 'amount' => 15000 ),
- 2 => array( 'key' => 'phase_2_paid', 'amount' => 15000 ),
- 3 => array( 'key' => 'phase_3_paid', 'amount' => 20000 )
- );
- foreach ( $phases as $num => $phase_data ) {
- $paid_val = isset( $_POST[ $phase_data['key'] ] ) ? 1 : 0;
- $current_val = affwp_get_referral_meta( $referral_id, $phase_data['key'], true );
- if ( $paid_val != $current_val ) {
- affwp_update_referral_meta( $referral_id, $phase_data['key'], $paid_val );
- if ( $paid_val == 1 ) {
- affwp_add_referral_note( $referral_id, sprintf('Phase %d payment of %d Rs marked as paid on %s.', $num, $phase_data['amount'], date('Y-m-d')) );
- } else {
- affwp_add_referral_note( $referral_id, sprintf('Phase %d payment reverted to unpaid on %s.', $num, date('Y-m-d')) );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement