2013-03-10 108 views
0

我正在開發一個WooCommerce插件(實際上是通常的WP插件,但只在啓用WooCommerce時才起作用),它應該改變標準的WooCommerce輸出邏輯。特別是我需要自己重寫標準的archive-product.php模板。 我發現在主題中更改模板沒有問題,但不能在插件中如何執行。我可以怎麼做沒有 WP & WooCommerce核心的任何變化?WooCommerce插件模板覆蓋

回答

0

這是我嘗試這樣的事情。希望它會有所幫助。

添加此過濾器到你的插件:

add_filter('template_include', 'my_include_template_function'); 

然後回調函數將

function my_include_template_function($template_path) { 

      if (is_single() && get_post_type() == 'product') { 

       // checks if the file exists in the theme first, 
       // otherwise serve the file from the plugin 
       if ($theme_file = locate_template(array ('single-product.php'))) { 
        $template_path = $theme_file; 
       } else { 
        $template_path = PLUGIN_TEMPLATE_PATH . 'single-product.php'; 
       } 

      } elseif (is_product_taxonomy()) { 

       if (is_tax('product_cat')) { 

        // checks if the file exists in the theme first, 
        // otherwise serve the file from the plugin 
        if ($theme_file = locate_template(array ('taxonomy-product_cat.php'))) { 
         $template_path = $theme_file; 
        } else { 
         $template_path = PLUGIN_TEMPLATE_PATH . 'taxonomy-product_cat.php'; 
        } 

       } else { 

        // checks if the file exists in the theme first, 
        // otherwise serve the file from the plugin 
        if ($theme_file = locate_template(array ('archive-product.php'))) { 
         $template_path = $theme_file; 
        } else { 
         $template_path = PLUGIN_TEMPLATE_PATH . 'archive-product.php'; 
        } 
       } 

      } elseif (is_archive() && get_post_type() == 'product') { 

       // checks if the file exists in the theme first, 
       // otherwise serve the file from the plugin 
       if ($theme_file = locate_template(array ('archive-product.php'))) { 
        $template_path = $theme_file; 
       } else { 
        $template_path = PLUGIN_TEMPLATE_PATH . 'archive-product.php'; 
       } 

      } 

     return $template_path; 
    } 

我檢查了這對主題先行加載。如果在主題中找不到該文件,則會從插件中加載該文件。

您可以在此更改邏輯。

希望它會做你的工作。

謝謝