Disable SVI from running on certain conditions

Sometimes you may wish to disable SVI and fallback to your default gallery presentation if certain conditions are met. You can do this by adding a little snipet to your theme functions.php.

Replace the __PUT_CONDITION_HERE__ with one of the following conditions or any other you may come up with:

  1. !$product->is_type(‘variable’) -> If the product is not a Variable product
  2. empty(get_post_meta($pid, ‘woosvi_slug’, true)) -> If no SVI data is present in the product, you have not created any SVI galleries

add_action('woocommerce_before_main_content', 'convert_disable_svi', 10);

function convert_disable_svi()
{
    global $product;

    $pid = $product->get_id();

    if (__PUT_CONDITION_HERE__) {
        SVI_remove_filters_with_method_name('wc_get_template', 'filter_wc_get_template', 1);
        SVI_remove_filters_with_method_name('woocommerce_before_single_product', 'remove_hooks', 20);
        SVI_remove_filters_with_method_name('woocommerce_before_single_product_summary', 'render_frontend', 20);
    }
}

function SVI_remove_filters_with_method_name($hook_name = '', $method_name = '', $priority = 0)
{
    global $wp_filter;
    if (!isset($wp_filter[$hook_name][$priority]) || !is_array($wp_filter[$hook_name][$priority])) {
        return false;
    }
    foreach ((array) $wp_filter[$hook_name][$priority] as $unique_id => $filter_array) {
        if (isset($filter_array['function']) && is_array($filter_array['function'])) {
            if (is_object($filter_array['function'][0]) && get_class($filter_array['function'][0]) && $filter_array['function'][1] == $method_name) {
                if (is_a($wp_filter[$hook_name], 'WP_Hook')) {
                    unset($wp_filter[$hook_name]->callbacks[$priority][$unique_id]);
                } else {
                    unset($wp_filter[$hook_name][$priority][$unique_id]);
                }
            }
        }
    }
    return false;
}