Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
1.9 kB
0
Indexable
Never
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($earnings, $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();
    }

    $earnings = $earnings + $monthly_spent;
    return $earnings;

}, 10, 2);
Leave a Comment