Skip to main content
clear title, formatting + tag
Source Link
Fayaz
  • 9k
  • 2
  • 32
  • 51

Modify existing plugin function using filter (without modifying the plugin directly)

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$actions array. Is this possible without modifying the plugin directly, can I do it by filtering in the functionsfunctions.php file?

Modify function using filter

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 file?

Modify existing plugin function using filter (without modifying the plugin directly)

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?

Tweeted twitter.com/StackWordPress/status/1029428099899908097
Source Link
fightstarr20
  • 1.1k
  • 8
  • 26
  • 46

Modify function using filter

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 file?