Okay
  Public Ticket #3733494
how to set commission "0" in cancelled orders
Closed

Comments

  •  2
    Lukasz started the conversation

    Hi is there any smart code snippet to change commission to 0 $ after order cancelled ??

    (picture attached)

    Attached files:  Zrzut ekranu 2024-09-26 o 15.59.38.png

  •  2,281
    WebWizards replied

    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
    Lukasz replied

    Hi can You update this snippet to make "0" commission for subagents orders as well? 

  •  2,281
    WebWizards replied

    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);
            }
    
        }
    }