Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a filter to determine commission on subtotal or total #53

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions adminpages/affiliates.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@
$enabled = true;
}

$filter_on = "total";

$calculate_subtotal = apply_filters( 'pmpro_affiliates_calculate_on_subtotal', false );

if ( $calculate_subtotal ) {
$filter_on = "subtotal";
}

if($edit && $save)
{
//updating or new?
Expand Down Expand Up @@ -442,7 +450,7 @@
</td>
<td>
<?php
$norders = $wpdb->get_var("SELECT COUNT(total) FROM $wpdb->pmpro_membership_orders WHERE affiliate_id = '" . esc_sql($affiliate->id) . "' AND status NOT IN('pending', 'error', 'refunded', 'refund', 'token', 'review')");
$norders = $wpdb->get_var("SELECT COUNT(".esc_sql( $filter_on ).") FROM $wpdb->pmpro_membership_orders WHERE affiliate_id = '" . esc_sql($affiliate->id) . "' AND status NOT IN('pending', 'error', 'refunded', 'refund', 'token', 'review')");
if(empty($affiliate->visits))
echo "0%";
else
Expand All @@ -451,7 +459,7 @@
</td>
<?php
// Calculate earnings so we can show commission earned and total earnings.
$earnings = $wpdb->get_var("SELECT SUM(total) FROM $wpdb->pmpro_membership_orders WHERE affiliate_id = '" . esc_sql($affiliate->id) . "' AND status NOT IN('pending', 'error', 'refunded', 'refund', 'token', 'review')");
$earnings = $wpdb->get_var("SELECT SUM(".esc_sql( $filter_on ).") FROM $wpdb->pmpro_membership_orders WHERE affiliate_id = '" . esc_sql($affiliate->id) . "' AND status NOT IN('pending', 'error', 'refunded', 'refund', 'token', 'review')");

?>

Expand Down
16 changes: 12 additions & 4 deletions adminpages/report-csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@
die( __( 'You do not have permissions to perform this action.', 'pmpro-affiliates' ) );
}

$sql_query = "SELECT o.id as order_id, a.code, a.commissionrate, o.affiliate_subid as subid, a.name, u.user_login, o.user_id, o.membership_id, UNIX_TIMESTAMP(o.timestamp) as timestamp, o.total, o.status, om.meta_value as affiliate_paid
FROM $wpdb->pmpro_membership_orders o
$filter_on = "o.total";

$calculate_subtotal = apply_filters( 'pmpro_affiliates_calculate_on_subtotal', false );

if ( $calculate_subtotal ) {
$filter_on = "o.subtotal";
}

$sql_query = "SELECT o.id as order_id, a.code, a.commissionrate, o.affiliate_subid as subid, a.name, u.user_login, o.user_id, o.membership_id, UNIX_TIMESTAMP(o.timestamp) as timestamp, ".esc_sql( $filter_on ).", o.status, om.meta_value as affiliate_paid
FROM $wpdb->pmpro_membership_orders o
LEFT JOIN $wpdb->pmpro_affiliates a
ON o.affiliate_id = a.id
LEFT JOIN $wpdb->users u
Expand Down Expand Up @@ -80,8 +88,8 @@
pmpro_enclose( $order->name ),
pmpro_enclose( date_i18n( 'Y-m-d', $order->timestamp ) ),
pmpro_enclose( $order->commissionrate * 100 ) . '%',
pmpro_enclose( number_format( $order->total * $order->commissionrate, 2 ) ),
pmpro_enclose( $order->total ),
( $calculate_subtotal ) ? pmpro_enclose( number_format( $order->subtotal * $order->commissionrate, 2 ) ) : pmpro_enclose( number_format( $order->total * $order->commissionrate, 2 ) ),
( $calculate_subtotal ) ? pmpro_enclose( $order->subtotal ) : pmpro_enclose( $order->total ),
);

$pmpro_affiliate_report_data = apply_filters( 'pmpro_affiliate_list_csv_extra_column_data', $pmpro_affiliate_report_data, $order, $level );
Expand Down
22 changes: 19 additions & 3 deletions adminpages/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,17 @@
?>

<?php

$filter_on = "o.total";

$calculate_subtotal = apply_filters( 'pmpro_affiliates_calculate_on_subtotal', false );

if ( $calculate_subtotal ) {
$filter_on = "o.subtotal";
}

$sqlQuery =
"SELECT o.id as order_id, o.code as order_code, a.id as affiliate_id, a.code, a.commissionrate, o.affiliate_subid as subid, a.name, u.ID as user_id, u.user_login, o.membership_id, UNIX_TIMESTAMP(o.timestamp) as timestamp, o.total, o.status, om.meta_value as affiliate_paid
"SELECT o.id as order_id, o.code as order_code, a.id as affiliate_id, a.code, a.commissionrate, o.affiliate_subid as subid, a.name, u.ID as user_id, u.user_login, o.membership_id, UNIX_TIMESTAMP(o.timestamp) as timestamp, ".esc_sql( $filter_on).", o.status, om.meta_value as affiliate_paid
FROM $wpdb->pmpro_membership_orders o
LEFT JOIN $wpdb->pmpro_affiliates a
ON o.affiliate_id = a.id
Expand Down Expand Up @@ -103,6 +112,13 @@
<?php
global $pmpro_currency_symbol;
foreach ( $affiliate_orders as $order ) {

if( $calculate_subtotal ) {
$order_total = $order->subtotal;
} else {
$order_total = $order->total;
}

$level = pmpro_getLevel( $order->membership_id );
// Get the affiliate paid status and generate a mark as paid link if not paid. Add nonce.
$affiliate_paid = $order->affiliate_paid;
Expand Down Expand Up @@ -153,8 +169,8 @@
</td>
<td><?php echo date_i18n( get_option( 'date_format' ), $order->timestamp );?></td>
<td><?php echo esc_html( $order->commissionrate * 100 );?>%</td>
<td><?php echo pmpro_formatPrice( $order->total * $order->commissionrate ); ?></td>
<td><?php echo pmpro_formatPrice( $order->total ); ?></td>
<td><?php echo pmpro_formatPrice( $order_total * $order->commissionrate ); ?></td>
<td><?php echo pmpro_formatPrice( $order_total ); ?></td>
<td><?php echo '<span class="pmpro_affiliate_paid_status" id="order_' . esc_attr( $order->order_id ) . '">' . $affiliate_paid . '</span>'; // We escape the $affiliate_paid before outputting further up.?></td>
<?php do_action( "pmpro_affiliate_report_extra_cols_body", $order ); ?>
</tr>
Expand Down
16 changes: 13 additions & 3 deletions pages/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,17 @@ function pmpro_affiliates_report_shortcode( $atts, $content = null, $code = '' )
<?php } ?>
<h2><?php echo ucwords( $pmpro_affiliates_singular_name ); ?> <?php echo esc_html__( 'Report for Code:', 'pmpro-affiliates' ) . ' ' . esc_html( $affiliate->code ); ?></h2>
<?php
$sqlQuery = "SELECT a.code, o.affiliate_subid as subid, a.name, u.user_login, UNIX_TIMESTAMP(o.timestamp) as timestamp, o.total, o.membership_id, o.status FROM $wpdb->pmpro_membership_orders o LEFT JOIN $wpdb->pmpro_affiliates a ON o.affiliate_id = a.id LEFT JOIN $wpdb->users u ON o.user_id = u.ID WHERE o.affiliate_id <> '' AND o.status NOT IN('pending', 'error', 'refunded', 'refund', 'token', 'review') ";

$filter_on = "o.total";

$calculate_subtotal = apply_filters( 'pmpro_affiliates_calculate_on_subtotal', false );

if ( $calculate_subtotal ) {
$filter_on = "o.subtotal";
}

$sqlQuery = "SELECT a.code, o.affiliate_subid as subid, a.name, u.user_login, UNIX_TIMESTAMP(o.timestamp) as timestamp, ".esc_sql( $filter_on ).", o.membership_id, o.status FROM $wpdb->pmpro_membership_orders o LEFT JOIN $wpdb->pmpro_affiliates a ON o.affiliate_id = a.id LEFT JOIN $wpdb->users u ON o.user_id = u.ID WHERE o.affiliate_id <> '' AND o.status NOT IN('pending', 'error', 'refunded', 'refund', 'token', 'review') ";

if ( $report != 'all' ) {
$sqlQuery .= " AND a.id = '" . esc_sql( $report ) . "' ";
}
Expand Down Expand Up @@ -229,10 +239,10 @@ function pmpro_affiliates_report_shortcode( $atts, $content = null, $code = '' )
</div>
<?php } ?>
<?php if ( in_array( 'show_commission', $fields ) ) { ?>
<div class="row-item"><?php echo pmpro_formatPrice( $order->total * $affiliate->commissionrate ); ?></div>
<div class="row-item"><?php echo ( $calculate_subtotal ) ? pmpro_formatPrice($order->subtotal * $affiliate->commissionrate) : pmpro_formatPrice($order->total * $affiliate->commissionrate); ?></div>
<?php } ?>
<?php if ( in_array( 'total', $fields ) ) { ?>
<div class="row-item"><?php echo pmpro_formatPrice( $order->total ); ?></div>
<div class="row-item"><?php echo ( $calculate_subtotal ) ? pmpro_formatPrice( $order->subtotal ) : pmpro_formatPrice( $order->total ); ?></div>
<?php } ?>
</div>
<?php
Expand Down
8 changes: 7 additions & 1 deletion pmpro-affiliates.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,13 @@ function pmpro_affiliates_get_commissions( $affiliate_code, $state = 'paid' ) {

$paid_commission = 0;

$sql_query = "SELECT SUM(o.total) as total, a.commissionrate
$filter_on = "o.total";

if ( apply_filters( 'pmpro_affiliates_calculate_on_subtotal', false ) ) {
$filter_on = "o.subtotal";
}

$sql_query = "SELECT SUM(".esc_sql( $filter_on ).") as total, a.commissionrate
FROM $wpdb->pmpro_membership_orders o
LEFT JOIN $wpdb->pmpro_membership_ordermeta om
ON o.id = om.pmpro_membership_order_id
Expand Down