Untitled

 avatar
unknown
plain_text
a year ago
1.9 kB
7
Indexable
add_filter('b2bking_group_rules_monthly_spent', function($monthly_spent, $user_id){
	// add affiliate sales
	$group_rules_args = apply_filters('salesking_group_rules_args', array( 
	    'post_type' => 'salesking_earning',
	    'numberposts' => -1,
	    'post_status'    => 'any',
	    'fields'    => 'ids',
	    'meta_key'   => 'agent_id',
	    'meta_value' => $user_id,
	), $user_id);
	// get total agent commissions
	$earnings = get_posts( $group_rules_args );

	$total_agent_commissions = 0;
	$total_orders_amount = 0;

	foreach ($earnings as $earning_id){
	    $order_id = get_post_meta($earning_id,'order_id', true);
	    $orderobj = wc_get_order($order_id);
	    if ($orderobj !== false){
		    $earnings_total = get_post_meta($earning_id,'salesking_commission_total', true);
		    if (!empty($earnings_total) && floatval($earnings_total) !== 0){
		        $status = $orderobj->get_status();
		        $order_total = $orderobj->get_total();

		        $total_agent_commissions+=$earnings_total;
		        $total_orders_amount += $order_total;

		    }
		}
	}

	$monthly_spent += ($total_orders_amount - $total_agent_commissions);

	return $monthly_spent;

}, 10, 2);

add_filter('salesking_group_monthly_earnings', function($orders_total, $commission, $agent_id){
	$year = date('Y', strtotime(date('Y-m')." -1 month"));
	$month = date('m', strtotime(date('Y-m')." -1 month"));
	$days_in_month = date('t', strtotime(date('Y-m')." -1 month"));

	// get customer's monthly spend
	$args = array(
		'limit' => '-1',
        'customer_id' => $agent_id,
        'date_created' => $year.'-'.$month.'-01...'.$year.'-'.$month.'-'.$days_in_month,
        'type' => 'shop_order',

    );

    $orders = wc_get_orders( $args );
    $monthly_spent = 0;
    foreach ($orders as $order){
    	$monthly_spent += $order->get_total();
    }

    $orders_total = $orders_total + $monthly_spent - $commission;
    return $orders_total;

}, 10, 3);
Leave a Comment