Public Ticket #3733494

how to set commission "0" in cancelled orders

Closed
Comments
2,762
KingsPlugins replied 2 years ago on September 29, 2024 at 11:18am

Hi there,

It is possible to set it to 0 on cancelled orders. To do that, you can add this PHP snippet to your site:

add_action('woocommerce_order_status_cancelled', 'update_salesking_commission_on_cancel', 1000, 1);

function update_salesking_commission_on_cancel($order_id) {
    $order = wc_get_order($order_id);
    $earning_id = $order->get_meta('salesking_earning_id');
    
    if ($earning_id) {
        $total_earnings_on_order = get_post_meta($earning_id, 'salesking_commission_total', true);
        
        // Update the commission total to 0
        update_post_meta($earning_id, 'salesking_commission_total', 0);
        
    }
}

 

Please note that it will no longer show on the "Earnings" page after cancelled (it will show only on the Orders page), because the plugin does not show earnings equal to 0 there.

 

Kind regards,

Stefan

2,762
KingsPlugins replied 2 years ago on November 28, 2024 at 1:48pm

To make that also apply to the "earnings from subagents" / "parent agent commission", please replace the snippet, with this code:

add_action('woocommerce_order_status_cancelled', 'update_salesking_commission_on_cancel', 1000, 1);

function update_salesking_commission_on_cancel($order_id) {
    $order = wc_get_order($order_id);
    $earning_id = $order->get_meta('salesking_earning_id');
    
    if ($earning_id) {
        $total_earnings_on_order = get_post_meta($earning_id, 'salesking_commission_total', true);
        
        // Update the commission total to 0
        update_post_meta($earning_id, 'salesking_commission_total', 0);
        

        // Update parent agent commission to 0 as well
        $agents_of_earning = get_post_meta($earning_id, 'agents_of_earning', true);
        if (empty($agents_of_earning)){
            $agents_of_earning = array();
        }

        foreach ($agents_of_earning as $ag_id){
            update_post_meta($earning_id, 'parent_agent_id_'.$ag_id.'_earnings', 0);
        }

    }
}

Sign In