Advertisement
blacknoir007

Original Code

Dec 9th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | Source Code | 0 0
  1. // 1. Initialize the 3-phase meta on referral creation
  2. add_action( 'affwp_referral_created', 'my_initialize_three_phase_referral', 10, 2 );
  3. function my_initialize_three_phase_referral( $referral_id, $data ) {
  4.     affwp_update_referral_meta( $referral_id, 'phase_1_paid', 0 );
  5.     affwp_update_referral_meta( $referral_id, 'phase_2_paid', 0 );
  6.     affwp_update_referral_meta( $referral_id, 'phase_3_paid', 0 );
  7.     affwp_update_referral_meta( $referral_id, 'total_commission', 50000 );
  8.     affwp_add_referral_note( $referral_id, 'Referral initialized with a 3-phase payout structure (Total: 50,000 Rs).');
  9. }
  10.  
  11. // 2. Add checkboxes to referral edit screen
  12. add_action( 'affwp_edit_referral_bottom', 'my_add_phase_paid_checkboxes', 10 );
  13. function my_add_phase_paid_checkboxes( $referral ) {
  14.     $total_commission = affwp_get_referral_meta( $referral->referral_id, 'total_commission', true );
  15.     if ( empty( $total_commission ) ) {
  16.         return;
  17.     }
  18.  
  19.     $phase_1_paid = affwp_get_referral_meta( $referral->referral_id, 'phase_1_paid', true );
  20.     $phase_2_paid = affwp_get_referral_meta( $referral->referral_id, 'phase_2_paid', true );
  21.     $phase_3_paid = affwp_get_referral_meta( $referral->referral_id, 'phase_3_paid', true );
  22.     ?>
  23.     <h3>3-Phase Payout Status</h3>
  24.     <p>
  25.         <label><input type="checkbox" name="phase_1_paid" value="1" <?php checked( $phase_1_paid, 1 ); ?>> Phase 1 Paid (15,000 Rs)</label><br>
  26.         <label><input type="checkbox" name="phase_2_paid" value="1" <?php checked( $phase_2_paid, 1 ); ?>> Phase 2 Paid (15,000 Rs)</label><br>
  27.         <label><input type="checkbox" name="phase_3_paid" value="1" <?php checked( $phase_3_paid, 1 ); ?>> Phase 3 Paid (20,000 Rs)</label><br>
  28.     </p>
  29.     <?php
  30. }
  31.  
  32. // 3. Save updated phase status on referral update
  33. add_action( 'affwp_update_referral', 'my_save_phase_paid_fields', 10, 2 );
  34. function my_save_phase_paid_fields( $referral_id, $data ) {
  35.     $total_commission = affwp_get_referral_meta( $referral_id, 'total_commission', true );
  36.     if ( empty( $total_commission ) ) {
  37.         return;
  38.     }
  39.  
  40.     $phases = array(
  41.         1 => array( 'key' => 'phase_1_paid', 'amount' => 15000 ),
  42.         2 => array( 'key' => 'phase_2_paid', 'amount' => 15000 ),
  43.         3 => array( 'key' => 'phase_3_paid', 'amount' => 20000 )
  44.     );
  45.  
  46.     foreach ( $phases as $num => $phase_data ) {
  47.         $paid_val = isset( $_POST[ $phase_data['key'] ] ) ? 1 : 0;
  48.         $current_val = affwp_get_referral_meta( $referral_id, $phase_data['key'], true );
  49.  
  50.         if ( $paid_val != $current_val ) {
  51.             affwp_update_referral_meta( $referral_id, $phase_data['key'], $paid_val );
  52.             if ( $paid_val == 1 ) {
  53.                 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')) );
  54.             } else {
  55.                 affwp_add_referral_note( $referral_id, sprintf('Phase %d payment reverted to unpaid on %s.', $num, date('Y-m-d')) );
  56.             }
  57.         }
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement