2

Here's plugin function:

function dokan_get_admin_commission_by($order, $seller_id)
{
    $saved_admin_fee = get_post_meta(dokan_get_prop($order, 'id') , '_dokan_admin_fee', true);
    if ($saved_admin_fee != '')
    {
        return apply_filters('dokan_order_admin_commission', $saved_admin_fee, $order);
    }
    $admin_commission = 10;
    return apply_filters('dokan_order_admin_commission', $admin_commission, $order);
}

I have overwritten this function from theme's functions.php file:

function dokan_get_admin_commission_by_overwrite( $admin_commission, $order) {
    return $admin_commission+1;
}
add_filter( 'dokan_order_admin_commission', 'dokan_get_admin_commission_by_overwrite', 10, 2 );

Theme filter is working fine, but it runs two times. So it increments the value two times. The reason it runs two times is because in the plugin, function dokan_get_admin_commission_by is called two times. I don't know how to run it a single time by theme's functions.php file.

0

1 Answer 1

2

Probable cause of double increment:

The plugin function dokan_get_admin_commission_by() executes apply_filters('dokan_order_admin_commission' ... ) twice, but both times it uses return, so the filter is not executing twice.

The probable cause of getting double increment is because, in the following CODE:

if ($saved_admin_fee != '')
{
    return apply_filters('dokan_order_admin_commission', $saved_admin_fee, $order);
}

the plugin is giving your theme filter $saved_admin_fee as $admin_commission, on the other hand, in the following CODE:

$admin_commission = 10;
return apply_filters('dokan_order_admin_commission', $admin_commission, $order);

the plugin is always giving 10 as $admin_commission value to the theme filter.

So, you may need to handle this same condition in your theme function.

Also, if the filter is executing twice, then it must be from somewhere else. In that case, you'll have to remove the filter once the theme function is executed.

Example CODE fix for functions.php:

So your final theme CODE will be like:

function dokan_get_admin_commission_by_overwrite( $admin_commission, $order) {
    remove_filter( 'dokan_order_admin_commission', 'dokan_get_admin_commission_by_overwrite', 10, 2 );
    if ( $admin_commission === 10 ) {
        return $admin_commission + 1;
    }

    return $admin_commission;
}
add_filter( 'dokan_order_admin_commission', 'dokan_get_admin_commission_by_overwrite', 10, 2 );

Now this function should run only once and also increment with the proper condition checked.

Note: if you are still confused, or need further assistance, then I'd suggest you to contact that plugin's support team.

6
  • Fayaz I have already tried all the above solutions, no any work for me... Commented Sep 2, 2018 at 16:47
  • still not working,check this screenshot for better to understand why filter run two times: nimb.ws/eb5SUx Commented Sep 2, 2018 at 16:53
  • The plugin doesn't call the filter twice by those two lines. It uses the condition with return, so even though within the dokan_get_admin_commission_by function apply_filters('dokan_order_admin_commission' .. ) is called twice, only one of these CODE will execute at a time. So the problem is somewhere else.
    – Fayaz
    Commented Sep 2, 2018 at 16:55
  • Yes it uses condition, but after comment this code funcitons.php file code working fine... if ($saved_admin_fee != '') { return apply_filters('dokan_order_admin_commission', $saved_admin_fee, $order); } Commented Sep 2, 2018 at 16:59
  • Actually, i am looking for the solution, don't want to work on plugin files. Want to overwrite admin commission by theme functions.php Commented Sep 2, 2018 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.