function eStore_award_author_commission($payment_data, $cart_items)
{
    eStore_payment_debug('Checking if the Author ID field has been used in any of the purchased product for revenue sharing purpose.', true);
    $share_revenue = get_option('eStore_aff_enable_revenue_sharing');
    if ($share_revenue != '1') {
        eStore_payment_debug('Revenue sharing feature is not in use.', true);
        return;
    }
    global $wpdb;
    $products_table_name = WP_ESTORE_PRODUCTS_TABLE_NAME;
    $buyer_email = $payment_data['payer_email'];
    $buyer_name = $payment_data['first_name'] . " " . $payment_data['last_name'];
    $txn_id = $payment_data['txn_id'];
    global $eStore_affiliate_individual_product_commisions;
    if (!empty($eStore_affiliate_individual_product_commisions)) {
        $cart_items = $eStore_affiliate_individual_product_commisions;
    }
    foreach ($cart_items as $current_cart_item) {
        $cart_item_number = $current_cart_item['item_number'];
        $total_item_price = $current_cart_item['mc_gross'] - $current_cart_item['mc_shipping'];
        $item_qty = $current_cart_item['quantity'];
        $retrieved_product = $wpdb->get_row("SELECT * FROM {$products_table_name} WHERE id = '{$cart_item_number}'", OBJECT);
        if (!empty($retrieved_product->author_id)) {
            //award commission for the author of this book
            eStore_payment_debug('Sharing Revenue.... Total Price of the currently processing item : ' . $total_item_price, true);
            $referrer = $retrieved_product->author_id;
            $rev_share_commission = $retrieved_product->rev_share_commission;
            $sale_amount = $total_item_price;
            //The total item price includes the (individual item price * quantity)
            if (!empty($current_cart_item['product_commission'])) {
                //lets subtract the affiliate commission before calculating the author's share
                $sale_amount = $sale_amount - $current_cart_item['product_commission'];
            }
            eStore_payment_debug('Sale amount that will be used (this amount has taken the affiliate commission into consideration if it applies): ' . $sale_amount, true);
            if (empty($rev_share_commission)) {
                $rev_share_commission = "";
            }
            eStore_payment_debug('Sharing revenue with: ' . $referrer . ' Commission override value (if any): ' . $rev_share_commission, true);
            eStore_payment_debug('WP Affiliate platform plugin version: ' . WP_AFFILIATE_PLATFORM_VERSION, true);
            $result = wp_aff_award_commission($referrer, $sale_amount, $txn_id, $cart_item_number, $buyer_email, '', $rev_share_commission, $buyer_name);
            eStore_payment_debug(eStore_br2nl($result), true);
        }
    }
}
Пример #2
0
 public static function awardCommission($orderId, $referrer)
 {
     global $wpdb;
     if (!empty($referrer)) {
         $order = new Cart66Order($orderId);
         if ($order->id > 0) {
             $subtractAmount = 0;
             $discount = $order->discountAmount;
             $order_items = array();
             foreach ($order->getItems() as $item) {
                 $order_items[] = $item->item_number;
                 $price = $item->product_price * $item->quantity;
                 if ($price > $discount) {
                     $subtractAmount = $discount;
                     $discount = 0;
                 } else {
                     $subtractAmount = $price;
                     $discount = $discount - $price;
                 }
                 if ($subtractAmount > 0) {
                     $price = $price - $subtractAmount;
                 }
                 // Transaction if for commission is the id in th order items table
                 $txn_id = $order->trans_id;
                 $sale_amount = $price;
                 $item_id = $item->item_number;
                 $buyer_email = $order->email;
                 if (function_exists('wp_aff_award_commission')) {
                     // Make sure commission has not already been granted for this transaction
                     $aff_sales_table = $wpdb->prefix . "affiliates_sales_tbl";
                     $txnCount = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM {$aff_sales_table} where txn_id = %s", $txn_id));
                     if ($txnCount < 1) {
                         wp_aff_award_commission($referrer, $sale_amount, $txn_id, $item_id, $buyer_email);
                     }
                 }
             }
             // valid order id
             // Transaction if for commission is the id in th order items table
             $txn_id = $order->trans_id;
             $sale_amount = $order->total - ($order->shipping + $order->tax);
             // set eligible amount to total sans shipping
             $item_id = implode(',', $order_items);
             $buyer_email = $order->email;
             // Affiliate Royale
             Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "]\n         Running wafp_award_commission\n\n         referrer {$referrer}\n\n         sale_amount {$sale_amount}\n\n         txn_id {$txn_id}\n\n         item_id {$item_id}\n\n         buyer_email {$buyer_email}");
             do_action('wafp_award_commission', $referrer, $sale_amount, $txn_id, $item_id, $buyer_email);
         }
     }
 }