8

I have the following function inside an existing plugin:

public static function init() {

    add_filter( 'wcs_view_subscription_actions', __CLASS__ . '::add_edit_address_subscription_action', 10, 2 );
}

public static function add_edit_address_subscription_action( $actions, $subscription ) {

        if ( $subscription->needs_shipping_address() && $subscription->has_status( array( 'active', 'on-hold' ) ) ) {
            $actions['change_address'] = array(
                'url'  => add_query_arg( array( 'subscription' => $subscription->get_id() ), wc_get_endpoint_url( 'edit-address', 'shipping' ) ),
                'name' => __( 'Change Address', 'woocommerce-subscriptions' ),
            );
        }

    return $actions;
}

I am trying to modify this, so that I can add something to the $actions array. Is this possible without modifying the plugin directly, can I do it by filtering in the functions.php file?

2 Answers 2

10

You can simply use the same filter with a lower or higher priority parameter to make the appropriate changes to the $actions array. That way you may create a small custom plugin (or modify the theme's functions.php file), without having to modify the existing plugin directly.

For example: if you want your custom code to execute after add_edit_address_subscription_action function, then use a bigger priority argument (lower priority) to the wcs_view_subscription_actions filter.

Sample CODE (use this as part of a custom plugin or in your theme's functions.php file):

// original filter uses priority 10, so priority 11 will make sure that this function executes after the original implementation
add_filter( 'wcs_view_subscription_actions', 'wpse_custom_action_after', 11, 2 );

function wpse_custom_action_after( $actions, $subscription ) {
    // your custom changes to $actions array HERE
    // this will be executed after add_edit_address_subscription_action function 
    return $actions;
}

On the other hand, if you want your custom code to execute before add_edit_address_subscription_action function, then use a smaller priority argument (higher priority).

Sample CODE (use this as part of a custom plugin or in your theme's functions.php file):

// original filter uses priority 10, so priority 9 will make sure that this function executes before the original implementation
add_filter( 'wcs_view_subscription_actions', 'wpse_custom_action_before', 9, 2 );    
function wpse_custom_action_before( $actions, $subscription ) {
    // your custom changes to $actions array HERE
    // this will be executed before add_edit_address_subscription_action function 
    return $actions;
}
2
  • I have a function on another plugin, in that function, I want to add some extra code to check a condition and if that is true, I need that function to work. How can I do it without touching the code of that plugin? In the above answer, I can see you have added filter, but in my case, I can't see any filter hook available in the plugin which i can use. Please guide me a bit, here is my code codepen.io/Nimesh58/pen/RwPRaaP
    – Nimesh
    Feb 18, 2020 at 4:45
  • 1
    @Nimesh: Generally similar implementation should work, i.e. find the filter or action hook that called the function you want to modify and then create another function and use the same hook with higher priority to execute the new custom function. However, every implementation is different, so if the above suggestion doesn't work for your case, better ask a new question.
    – Fayaz
    Feb 18, 2020 at 8:43
0

Yes, you can change $actions array in functions.php with your own function.

function your_function_name( $actions, $subscription ) {
    // here you can modify $actions array
    // 
    return $actions;
}

add_filter( 'wcs_view_subscription_actions', 'your_function_name', 15, 2 );

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.