Fixed payout for Affiliates with Marketpress

This is more of a code submission more than a support question.

For everyone else reading this:
This is not official code. I'm posting this to submit it to @Barry but it is still his call to integrate it into the plugin as an official release.

I've modified the Marketpress plugin for Affiliates to enable paying the affiliates a fixed amount for each Marketpress sale by adding a checkbox below the Affiliate Settings in Marketpress payment to indicate that the amount is a fixed amount not percentage.

I'm attaching my changed affiliate/affiliateincludes/addons/marketpress.php file and diff of the changes.

  • Rystraum

    Diff:

    diff --git a/affiliateincludes/addons/marketpress.php b/affiliateincludes/addons/marketpress.php
    index dd74ee3..fc3729b 100644
    --- a/affiliateincludes/addons/marketpress.php
    +++ b/affiliateincludes/addons/marketpress.php
    @@ -36,16 +36,11 @@ function AM_Paid_order( $order ) {
    
     	if(!empty($aff_id)) {
     		$percentage = aff_get_option('affiliate_mp_percentage', 0);
    -		$fixed = aff_get_option('affiliate_mp_fixed', 0);
    -		if ($fixed == 0) {
    -		  // We have a referrer - get the total
    -		  $total_amount = get_post_meta($order->ID, 'mp_order_total', true);
    -		  // calculate the amount to give the referrer - hardcoded for testing to 30%
    -		  $amount = ($total_amount / 100) * $percentage;
    -	  } else {
    -	    $amount = $percentage;
    -	  }
    -	  // run the standard affiliate action to do the recording and assigning
    +		// We have a referrer - get the total
    +		$total_amount = get_post_meta($order->ID, 'mp_order_total', true);
    +		// calculate the amount to give the referrer - hardcoded for testing to 30%
    +		$amount = ($total_amount / 100) * $percentage;
    +		// run the standard affiliate action to do the recording and assigning
     		do_action('affiliate_purchase', $aff_id, $amount, 'marketpress', $order->ID, 'Affiliate payment for MarketPress order.');
     		// record the amount paid / assigned in the meta for the order
     		add_post_meta($order->ID, 'affiliate_marketpress_order_paid', $amount, true);
    @@ -63,15 +58,8 @@ function AM_Show_Affiliate_Settings( $settings ) {
     		} else {
     			aff_delete_option( 'affiliate_mp_percentage' );
     		}
    +    }
    
    -		if( !empty($_POST['affiliate_mp_fixed']) && $_POST['affiliate_mp_fixed'] == 'on') {
    -		  aff_update_option('affiliate_mp_fixed', 1);
    -		} else {
    -		  aff_update_option('affiliate_mp_fixed', 0);
    -		}
    -  }
    -
    -    $fixed = aff_get_option('affiliate_mp_fixed', 0);
     	?>
     		<div id="mp_gateways" class="postbox">
                 <h3 class='hndle'><span><?php _e('Affiliate Settings', 'mp') ?></span></h3>
    @@ -79,17 +67,14 @@ function AM_Show_Affiliate_Settings( $settings ) {
     			  <span class="description"><?php _e('You can set the global commision amount paid to affiliates for referred purchases below. Set it to 0 for no payments.','affiliate'); ?></span>
                   <table class="form-table">
                     <tr>
    -          				<th scope="row"><?php $fixed == 0 ? _e('Set percentage to be paid to affiliates', 'affiliate') : _e('Set amount to be paid to affiliates', 'affiliate') ?></th>
    -          				<td>
    -							      <?php $percentage = aff_get_option('affiliate_mp_percentage', 0); ?>
    -							      <input type='text' name='affiliate_mp_percentage' value='<?php echo number_format($percentage, 2); ?>' style='width:5em;'/>
    -							      &nbsp;<?php if($fixed == 0) _e('%', 'affiliate'); ?>
    -          				</td>
    -                </tr>
    -                <tr>
    -                  <?php $checked = $fixed == 1 ? 'checked="checked"': '' ?>
    -                  <th><?php _e('Treat this as fixed amount, not percentage', 'affiliate') ?></th>
    -                  <td><input type="checkbox" name='affiliate_mp_fixed' <?php echo $checked ?> /></td>
    +        				<th scope="row"><?php _e('Set percentage to be paid to affiliates', 'affiliate') ?></th>
    +        				<td>
    +							<?php $percentage = aff_get_option('affiliate_mp_percentage', 0); ?>
    +							<input type='text' name='affiliate_mp_percentage' value='<?php echo number_format($percentage, 2); ?>' style='width:5em;'/>&nbsp;<?php _e('%', 'affiliate'); ?>
    +                			<?php
    +
    +                			?>
    +        				</td>
                     </tr>
                   </table>
                 </div>
    @@ -98,4 +83,4 @@ function AM_Show_Affiliate_Settings( $settings ) {
     }
     add_action('mp_gateway_settings', 'AM_Show_Affiliate_Settings');
    
    -?>
    +?>
    \ No newline at end of file

    Marketpress file:

    <?php
    /*
    Plugin Name: MarketPress add-on
    Description: Affiliate system plugin for the WordPress MarketPress plugin
    Author: Barry (Incsub)
    Author URI: https://premium.wpmudev.org
    */
    
    function AM_Record_affiliate() {
    
    	global $current_user;
    
    	// Call the affiliate action
    	do_action( 'affiliate_signup' );
    
    	if(defined( 'AFFILIATEID' )) {
    		// We found an affiliate that referred this order creation - so add a meta to the order recording it
    
    		if(!empty($_SESSION['mp_shipping_info'])) {
    			$_SESSION['mp_shipping_info']['affiliate_referrer'] = AFFILIATEID;
    		}
    
    	}
    
    }
    add_action( 'mp_shipping_process', 'AM_Record_affiliate' );
    
    // Paid order is a complete
    function AM_Paid_order( $order ) {
    	// Check for the affiliate referrer if there is one
    	$shipping_info = get_post_meta( $order->ID, 'mp_shipping_info', true);
    
    	if(isset($shipping_info['affiliate_referrer'])) {
    		$aff_id = $shipping_info['affiliate_referrer'];
    	}
    
    	if(!empty($aff_id)) {
    		$percentage = aff_get_option('affiliate_mp_percentage', 0);
    		$fixed = aff_get_option('affiliate_mp_fixed', 0);
    		if ($fixed == 0) {
    		  // We have a referrer - get the total
    		  $total_amount = get_post_meta($order->ID, 'mp_order_total', true);
    		  // calculate the amount to give the referrer - hardcoded for testing to 30%
    		  $amount = ($total_amount / 100) * $percentage;
    	  } else {
    	    $amount = $percentage;
    	  }
    	  // run the standard affiliate action to do the recording and assigning
    		do_action('affiliate_purchase', $aff_id, $amount, 'marketpress', $order->ID, 'Affiliate payment for MarketPress order.');
    		// record the amount paid / assigned in the meta for the order
    		add_post_meta($order->ID, 'affiliate_marketpress_order_paid', $amount, true);
    	}
    
    }
    add_action( 'mp_order_paid', 'AM_Paid_order' );
    
    function AM_Show_Affiliate_Settings( $settings ) {
    
    	if (isset($_POST['gateway_settings'])) {
          // Do processing here
    		if( !empty($_POST['affiliate_mp_percentage']) && $_POST['affiliate_mp_percentage'] > 0) {
    			aff_update_option( 'affiliate_mp_percentage', $_POST['affiliate_mp_percentage'] );
    		} else {
    			aff_delete_option( 'affiliate_mp_percentage' );
    		}
    
    		if( !empty($_POST['affiliate_mp_fixed']) && $_POST['affiliate_mp_fixed'] == 'on') {
    		  aff_update_option('affiliate_mp_fixed', 1);
    		} else {
    		  aff_update_option('affiliate_mp_fixed', 0);
    		}
      }
    
        $fixed = aff_get_option('affiliate_mp_fixed', 0);
    	?>
    		<div id="mp_gateways" class="postbox">
                <h3 class='hndle'><span><?php _e('Affiliate Settings', 'mp') ?></span></h3>
                <div class="inside">
    			  <span class="description"><?php _e('You can set the global commision amount paid to affiliates for referred purchases below. Set it to 0 for no payments.','affiliate'); ?></span>
                  <table class="form-table">
                    <tr>
              				<th scope="row"><?php $fixed == 0 ? _e('Set percentage to be paid to affiliates', 'affiliate') : _e('Set amount to be paid to affiliates', 'affiliate') ?></th>
              				<td>
    							      <?php $percentage = aff_get_option('affiliate_mp_percentage', 0); ?>
    							      <input type='text' name='affiliate_mp_percentage' value='<?php echo number_format($percentage, 2); ?>' style='width:5em;'/>
    							      &nbsp;<?php if($fixed == 0) _e('%', 'affiliate'); ?>
              				</td>
                    </tr>
                    <tr>
                      <?php $checked = $fixed == 1 ? 'checked="checked"': '' ?>
                      <th><?php _e('Treat this as fixed amount, not percentage', 'affiliate') ?></th>
                      <td><input type="checkbox" name='affiliate_mp_fixed' <?php echo $checked ?> /></td>
                    </tr>
                  </table>
                </div>
              </div>
    	<?php
    }
    add_action('mp_gateway_settings', 'AM_Show_Affiliate_Settings');
    
    ?>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.