View Categories

Payment Methods by Product & Country: Developer Hooks and Filters Reference

Developer hooks and filters reference #

Payment Methods by Product & Country for WooCommerce filters the WooCommerce gateway list and exposes several of its own filters so you can extend or override its logic in code. This reference lists the filters that ship in the current version, with their signatures and typical uses. Add the snippets to your child theme’s functions.php or a small custom plugin.

How the plugin hooks WooCommerce #

The plugin adds a callback to the core WooCommerce filter woocommerce_available_payment_gateways at priority PHP_INT_MAX, so it runs after other plugins have built the gateway list. When exactly it registers that callback is controlled by the Add filter setting (alg_wc_pgpp_advanced_add_hook): constructor, init (default), or wp_loaded.

Filters #

Filter Arguments Purpose
wpfactory_wc_pgpp_pre_filter_gateways $result (false), $available_gateways Return a gateway array to short-circuit all filtering (used internally by the country rules). Return false to continue normally.
wpfactory_wc_pgpp_disable_gateway $disable (false), $gateway_id Return true to remove a gateway for the current cart (used internally by the product rules).
wpfactory_wc_pgpp_skip_disable_gateway $skip (false), $gateway_id Return true to keep a gateway that would otherwise be removed.
wpfactory_wc_pgpp_after_filter_gateways $available_gateways, $gateways Final say over the filtered list (used internally to apply the fallback gateway).
wpfactory_wc_pgpp_settings $value Gate that disables Pro-only settings in the free build; Pro returns an empty string to unlock them.
wpfactory_wc_pgpp_gateway_settings_options $options, $gateway_id, $options_id, $include_or_exclude Alter the choices shown in a section’s Include/Exclude fields.
wpfactory_wc_pgpp_gateway_settings_class $class, $gateway_id, $options_id Change the CSS class on a section’s select fields.
wpfactory_wc_pgpp_gateway_settings_custom_attributes $attributes, $gateway_id, $options_id Change the HTML attributes on a section’s select fields.
wpfactory_wc_pgpp_countries_restriction_number $number (1) Number of country restriction blocks to render and evaluate.
wpfactory_wc_pgpp_settings_countries_restriction_number $attributes Custom attributes for the “Number of restrictions” field (Pro sets a minimum of 1).

Example: hide a gateway with your own condition #

add_filter( 'wpfactory_wc_pgpp_disable_gateway', function ( $disable, $gateway_id ) {
    // Hide Cash on Delivery for logged-out users.
    if ( 'cod' === $gateway_id && ! is_user_logged_in() ) {
        return true;
    }
    return $disable;
}, 10, 2 );

Example: adjust the final list #

add_filter( 'wpfactory_wc_pgpp_after_filter_gateways', function ( $available, $all ) {
    // Never leave the customer with nothing: restore bank transfer.
    if ( empty( $available ) && isset( $all['bacs'] ) ) {
        $available['bacs'] = $all['bacs'];
    }
    return $available;
}, 20, 2 );

Option keys and the v2.0.0 prefix change #

In version 2.0.0 the plugin’s public filters were renamed from the old alg_wc_pgpp_* prefix to wpfactory_wc_pgpp_*. The stored option keys were not renamed and remain on the alg_wc_pgpp_* prefix, for example:

  • alg_wc_pgpp_categories_include_<gateway_id> / alg_wc_pgpp_categories_exclude_<gateway_id>
  • alg_wc_pgpp_tags_include_<gateway_id> / alg_wc_pgpp_tags_exclude_<gateway_id>
  • alg_wc_pgpp_products_include_<gateway_id> / alg_wc_pgpp_products_exclude_<gateway_id>
  • alg_wc_pgpp_advanced_add_hook, alg_wc_pgpp_advanced_fallback_gateway

If you have code hooking the old alg_wc_pgpp_* filter names, update it to the wpfactory_wc_pgpp_* names shown above. The option keys stay the same, so get_option() calls do not need changing.

Need a hand? #

Questions about extending the plugin are welcome on the Payment Gateways per Products support forum.