MarketPress Discount Based Off Of User Role (MOD)

Marketpress 2.4.2
WP 3.3.1

So i decided to tackle offering my users a "Member Discount" based off of there user role. The way my code currently is set up is to let authors or higher receive a 20% discount before taxes and shipping. If the item costs $10 and shipping costs $5 and tax is 0. the user would pay $13. (10*.8)+5+0 = 13

This only works for the paypal-express gateway, so if you have multiple, you might be able to adapt the changes to the other gateways.

I imagine the dev adding a settings area in the gateway to set the %, and choose membership levels with check boxes to receive the discount.

- I tested the code on single site, not on multi site.
- coupons are not stackable. They can either take the member discount, or use a coupon, its either or, not both
- it works with item variations.
- works with quantities higher than 1 or more
- I tested with logged in and logged out with coupons.
- Tested as author, editor, administrator for member discount.
- Subscriber does not receive member discount, but can use coupon
- email confirmations will display member discount
- discount shows in admin back-end
- discount shows on user invoice

Here are my changes
I hard coded in a 20% discount, and I choose author,editor,administrator. You can change them to what you want. the discount needs to be a decimal. This hard coding is in multiple places.

www\wp-content\plugins\marketpress\marketpress-includes\plugins-gateway\paypal-express.php

in the function SetExpressCheckout

FIND

$total = array_sum($totals);

//coupon line
if ( $coupon = $mp->coupon_value($mp->get_coupon_code(), $total) ) {
		$total = $coupon['new_total'];
}

REPLACE WITH

$total = array_sum($totals);

//var default to say no member discount was applied.  if there will be one, this will be changed to TRUE
$TAO_author_discount_applied = "FALSE";	      

//coupon line
if ( $coupon = $mp->coupon_value($mp->get_coupon_code(), $total) ) {
		$total = $coupon['new_total'];
}

//TAO adding a discount to author or above
elseif ((is_user_logged_in()) && ((current_user_can('editor')) || (current_user_can('author')) || (current_user_can('editor')) || (current_user_can('administrator'))))
{
	//TAO this is a built in 20% discount before shipping and tax
	$TAO_discount_multiplier = .8;
	$total = round($total*$TAO_discount_multiplier,2);
	//echo "gateway total with discount = " . $total;
	//echo "<br>";
	//echo "coupon = " . $coupon;
	//echo "<br>";
	//make a var to say weather a member discount was applied or not
	$TAO_author_discount_applied = "TRUE";
}

FIND

//only add cart details if no coupon, as that's unsupported (stupid PayPal)
if (!$coupon) {
		$nvpstr .= $detailstr;
}

REPLACE WITH

//only add cart details if no coupon, as that's unsupported (stupid PayPal)
//TAO add in check for author or better discount, if no discount then do this
if ((!$coupon) && (!$TAO_author_discount_applied)) {
		$nvpstr .= $detailstr;
		//echo "inside no coupons or author discounts statement";
		//echo "<br>";
}
//echo "everything added, the total = " . $total;
//echo "<br>";

www\wp-content\plugins\marketpress\marketpress-includes\template-functions.php

in the function _mp_cart_table

FIND

//coupon line TODO - figure out how to apply them on global checkout
  $coupon_code = $mp->get_coupon_code();
if ( $coupon = $mp->coupon_value($coupon_code, $total) ) {
  $content .=  '<tr>';
  $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="2">' . __('Subtotal:', 'mp') . '</td>';
  $content .=  '  <td class="mp_cart_col_subtotal">' . $mp->format_currency('', $total) . '</td>';
  $content .=  '  <td> </td>';
  $content .=  '</tr>';
  $content .=  '<tr>';
  $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="2">' . __('Discount:', 'mp') . '</td>';
  $content .=  '  <td class="mp_cart_col_discount">' . $coupon['discount'] . '</td>';
  $content .=  '  <td class="mp_cart_remove_coupon"><a href="?remove_coupon=1">' . __('Remove Coupon »', 'mp') . '</a></td>';
  $content .=  '</tr>';
  $total = $coupon['new_total'];
} else {

REPLACE WITH

//coupon line TODO - figure out how to apply them on global checkout
  $coupon_code = $mp->get_coupon_code();
if ( $coupon = $mp->coupon_value($coupon_code, $total) ) {
  $content .=  '<tr>';
  $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="2">' . __('Subtotal:', 'mp') . '</td>';
  $content .=  '  <td class="mp_cart_col_subtotal">' . $mp->format_currency('', $total) . '</td>';
  $content .=  '  <td> </td>';
  $content .=  '</tr>';
  $content .=  '<tr>';
  $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="2">' . __('Discount:', 'mp') . '</td>';
  $content .=  '  <td class="mp_cart_col_discount">' . $coupon['discount'] . '</td>';
  $content .=  '  <td class="mp_cart_remove_coupon"><a href="?remove_coupon=1">' . __('Remove Coupon »', 'mp') . '</a></td>';
  $content .=  '</tr>';
  $total = $coupon['new_total'];
}
elseif ((is_user_logged_in()) && ((current_user_can('editor')) || (current_user_can('author')) || (current_user_can('editor')) || (current_user_can('administrator'))))
	{
    	//TAO show a discount for an author or higher before shipping and tax
    	//TAO what is the discount amount.  chosse  a decimal, so 20% is .8
    	$TAO_discount_multiplier = .8;
    	$TAO_discount_display ="-20%";
    	$TAO_discount_title = "Premium Member Discount:";

		$content .=  '<tr>';
		$content .=  '  <td class="mp_cart_subtotal_lbl" colspan="2">' . __('Subtotal:', 'mp') . '</td>';
		$content .=  '  <td class="mp_cart_col_subtotal">' . $mp->format_currency('', $total) . '</td>';
		$content .=  '  <td> </td>';
		$content .=  '</tr>';
		$content .=  '<tr>';
		$content .=  '  <td class="mp_cart_subtotal_lbl" colspan="2">' . __($TAO_discount_title, 'mp') . '</td>';
		$content .=  '  <td class="mp_cart_col_discount">' . $TAO_discount_display . '</td>';
		$content .=  '  <td class="mp_cart_subtotal_lbl" colspan="4">
        <a id="coupon-link" class="alignright" href="#coupon-code">' . __('Have a coupon code?', 'mp') . '</a>
        <div id="coupon-code" class="alignright" style="display: none;">
          <label for="coupon_code">' . __('Enter your code:', 'mp') . '</label>
          <input type="text" name="coupon_code" id="coupon_code" />
          <input type="submit" name="update_cart_submit" value="' . __('Apply »', 'mp') . '" />
        </div>
    </td>';
		//$content .=  '  <td class="mp_cart_remove_coupon"><a href="?remove_coupon=1">' . __('Remove Coupon »', 'mp') . '</a></td>';
		$content .=  '</tr>';
		$total = round($total*$TAO_discount_multiplier,2);
	}

else {

FIND

//coupon line TODO - figure out how to apply them on global checkout
  $coupon_code = $mp->get_coupon_code();
if ( $coupon = $mp->coupon_value($coupon_code, $total) ) {
  $content .=  '<tr>';
  $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="3">' . __('Subtotal:', 'mp') . '</td>';
  $content .=  '  <td class="mp_cart_col_subtotal">' . $mp->format_currency('', $total) . '</td>';
  $content .=  '</tr>';
  $content .=  '<tr>';
  $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="3">' . __('Discount:', 'mp') . '</td>';
  $content .=  '  <td class="mp_cart_col_discount">' . $coupon['discount'] . '</td>';
  $content .=  '</tr>';
  $total = $coupon['new_total'];
}

//shipping line

REPLACE WITH

//coupon line TODO - figure out how to apply them on global checkout
  $coupon_code = $mp->get_coupon_code();
if ( $coupon = $mp->coupon_value($coupon_code, $total) ) {
  $content .=  '<tr>';
  $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="3">' . __('Subtotal:', 'mp') . '</td>';
  $content .=  '  <td class="mp_cart_col_subtotal">' . $mp->format_currency('', $total) . '</td>';
  $content .=  '</tr>';
  $content .=  '<tr>';
  $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="3">' . __('Discount:', 'mp') . '</td>';
  $content .=  '  <td class="mp_cart_col_discount">' . $coupon['discount'] . '</td>';
  $content .=  '</tr>';
  $total = $coupon['new_total'];
}

//TAO is no coupon but logged in and is an author or better give them a discount.  The discount well above this line, the var is called $TAO_discount_multiplier $TAO_discount_display $TAO_discount_title
elseif ((is_user_logged_in()) && ((current_user_can('editor')) || (current_user_can('author')) || (current_user_can('editor')) || (current_user_can('administrator'))))
	{
		//TAO show a discount for an author or higher before shipping and tax
		//TAO what is the discount amount.  chosse  a decimal, so 20% is .8
		$TAO_discount_multiplier = .8;
		$TAO_discount_display ="-20%";
		$TAO_discount_title = "Premium Member Discount:";
		$content .=  '<tr>';
		$content .=  '  <td class="mp_cart_subtotal_lbl" colspan="3">' . __('Subtotal:', 'mp') . '</td>';
		$content .=  '  <td class="mp_cart_col_subtotal">' . $mp->format_currency('', $total) . '</td>';
		$content .=  '</tr>';
		$content .=  '<tr>';
		$content .=  '  <td class="mp_cart_subtotal_lbl" colspan="3">' . __($TAO_discount_title, 'mp') . '</td>';
		$content .=  '  <td class="mp_cart_col_discount">' . $TAO_discount_display . '</td>';
		$content .=  '</tr>';
		$total = round($total*$TAO_discount_multiplier,2);
	}      

//shipping line

www\wp-content\plugins\marketpress\marketpress-includes\template-functions.php

IN THE FUNCTION
mp_order_status

FIND

<?php //coupon line
if ( $order->mp_discount_info ) { ?>
<li><?php _e('Coupon Discount:', 'mp'); ?> <strong><?php echo $order->mp_discount_info['discount']; ?></strong></li>
<?php } ?>

REPLACE WITH

<?php //coupon line
if ( $order->mp_discount_info ) { ?>
<li><?php _e('Coupon Discount:', 'mp'); ?> <strong><?php echo $order->mp_discount_info['discount']; ?></strong></li>
<?php } ?>

<?php //TAO Member Discount line
if ( $order->mp_member_discount_info ) { ?>
<li><?php _e($order->mp_member_discount_info['discount_title'], 'mp'); ?> <strong><?php echo $order->mp_member_discount_info['discount_percentage']; ?></strong></li>
<?php } ?>

www\wp-content\plugins\marketpress\marketpress.php

in the function manage_orders_custom_columns

FIND

case "mp_orders_discount":
if ($meta["mp_discount_info"][0])
		  echo $meta["mp_discount_info"][0]['discount'];

REPLACE WITH

//TAO show members discount in manage orders
case "mp_orders_discount":
if ($meta["mp_discount_info"][0])
		  echo $meta["mp_discount_info"][0]['discount'];
elseif ($meta["mp_member_discount_info"][0])
		  echo $meta["mp_member_discount_info"][0]['discount_percentage'];

www\wp-content\plugins\marketpress\marketpress.php

in the function create_order

FIND

//mark coupon as used
$this->use_coupon($code);
}

REPLACE WITH

//mark coupon as used
$this->use_coupon($code);
}

//TAO add meta to reflect a premium member discount
elseif ((is_user_logged_in()) && ((current_user_can('editor')) || (current_user_can('author')) || (current_user_can('editor')) || (current_user_can('administrator'))))
{
	//TAO make post meta under a mp_member_discount_info key
	$TAO_discount_display ="20%";
	$TAO_discount_title = "Premium Member Discount:";
	add_post_meta($post_id, 'mp_member_discount_info', array('discount_percentage' => $TAO_discount_display, 'discount_title' => $TAO_discount_title), true);
}

www\wp-content\plugins\marketpress\marketpress.php

in the function filter_email

FIND

//coupon line
if ( $order->mp_discount_info ) {
  $order_info .= "\n" . __('Coupon Discount:', 'mp') . ' ' . $order->mp_discount_info['discount'];
}

REPLACE WITH

//coupon line
if ( $order->mp_discount_info ) {
  //TAO coupon code has a % sign in it, and the sprintf function does not like it so im converting it to the words percent
  $TAO_discount_percentage_words = str_replace('%', ' Percent', $order->mp_discount_info['discount'] );
  $order_info .= "\n" . __('Coupon Discount:', 'mp') . ' ' . $TAO_discount_percentage_words;
}

    //TAO member discount line
    elseif ( $order->mp_member_discount_info )
    	{

		    //the sprintf function does not like the % sign so im changing the % to the word percent to play nice.  This seems to only effect the admin e-mail that goes out, not the user confirmation e-mail
			if($order->mp_member_discount_info['discount_percentage'])
			  	{
		    		$TAO_discount_percentage_words = str_replace('%', ' Percent', $order->mp_member_discount_info['discount_percentage'] );
			  	}

	      	$order_info .= "\n" . __('Member Discount:', 'mp') . ' ' . $TAO_discount_percentage_words;
    	}

www\wp-content\plugins\marketpress\marketpress.php

in the function single_order_page

FIND

<?php //coupon line
if ( $order->mp_discount_info ) { ?>
<h3><?php _e('Coupon Discount:', 'mp'); ?></h3>
<p><?php echo $order->mp_discount_info['discount']; ?> (<?php echo $order->mp_discount_info['code']; ?>)</p>
<?php } ?>

REPLACE WITH

<?php //coupon line
if ( $order->mp_discount_info ) { ?>
<h3><?php _e('Coupon Discount:', 'mp'); ?></h3>
<p><?php echo $order->mp_discount_info['discount']; ?> (<?php echo $order->mp_discount_info['code']; ?>)</p>
<?php }

	elseif ( $order->mp_member_discount_info )
		{?>
	        <h3><?php _e($order->mp_member_discount_info['discount_title'], 'mp'); ?></h3>
	        <p><?php echo $order->mp_member_discount_info['discount_title']; ?> (<?php echo $order->mp_member_discount_info['discount_percentage']; ?>)</p>
	        <?php
	    }
?>
  • aecnu

    Greetings bigt11,

    Thanks again for your code submission and for being a WPMU Dev member!

    Your contribution is grealty appreciated and this could very well be an awesome customization to the plugin.

    In this particular case I believe this coding will be best served by making the developer aware of it and moving this ticket to the Features Suggestion section of the forum.

    Thanks again for being a WPMU Dev member!

    Cheers, Joe :slight_smile:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.