1

So, I am currently using a Wholesale plugin for Woocommerce which, by default, redirects users to it’s own Wholesale Store page. The caveat is that the Wholesale Store page only uses the archive-product.php template directly from within WooCommerce’s plugin files. I would like it to instead load the archive-product.php template from my child theme. The snippet below is from the plugin’s Wholesale_Store.php file, where the load_template function is being called:

class Wholesale_Store implements Registerable, Service
{

    /**
     * {@inheritdoc}
     */
    public function register()
    {
        add_action('wp_loaded', [$this, 'load'], 20);
    }

    public function load()
    {
        add_filter('template_include', [$this, 'load_template'], 10, 1);
        add_filter('theme_page_templates', [$this, 'hide_page_templates'], 10, 3);
        add_action('pre_get_posts', [$this, 'store_products_query'], 10, 1);
        add_filter('the_posts', [$this, 'remove_product_query_filters']);

        // Redirects
        add_action('template_redirect', [$this, 'handle_redirects']);

        add_filter('woocommerce_page_title', [$this, 'page_title'], 10, 1);
        add_action('woocommerce_archive_description', [$this, 'page_content'], 2);
        add_filter('woocommerce_get_breadcrumb', [$this, 'page_breadcrumb'], 10, 2);
    }

    /**
     * Load the archive-product.php template
     *
     * @param   string $template
     * @return  string $template
     */
    public function load_template($template)
    {
        if (!Util::is_wholesale_store_page()) {
            return $template;
        }
        // THIS NEEDS TO LOAD archive-product.php template in CHILD THEME
         if ( ! $this->has_block_template( 'archive-product' ) ) {
            $template = WC()->plugin_path() . '/templates/archive-product.php';
         }

        return $template;
    }

I found that I can just comment out the code below from the load_template function, which results in my child theme’s archive-product.php being used, but I also know that’s not the best practice, as this will get overwritten when I need to update the plugin.

if ( ! $this->has_block_template( 'archive-product' ) ) {
            $template = WC()->plugin_path() . '/templates/archive-product.php';
         }

Is there anyway to add a function/filter to my child theme's functions.php file to overwrite the $template path that’s being loaded in the plugin? Or is this not a possibility?

1
  • 2
    Just override the template by placing your archive template in your theme's woocommerce folder (see documentation here), or you could just override the template using the same filter hook - template_include..
    – Sally CJ
    Feb 5 at 2:56

0

Your Answer

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