【问题标题】:Extend woocommerce plugin扩展 woocommerce 插件
【发布时间】:2016-09-04 09:18:12
【问题描述】:

我有 wordpress 子主题,在其中我可以通过创建以下结构的文件夹来扩展 woocommerce:

/wp-content/themes/theme-child/woocommerce/single-product/add-to-cart/*.php

但问题是我不想扩展位于

/wp-content/plugins/woocommerce-plugin/templates/single-product/add-to-cart/*.php

通过第一种方法,我可以覆盖 woocommerce 文件,但如何为 woocommerce-plugin 覆盖?

【问题讨论】:

  • 您想要覆盖 woocommerce-plugin 或将 woocommerce 覆盖到 "woocommerce-plugin" 中,可以更具体。
  • 我想覆盖位于/wp-content/plugins/woocommerce-plugin/templates/single-product/add-to-cart/*.php 的文件而不更改原始文件。所以要制作某种形式的子主题/插件
  • 如果您只需要编辑 /single-product/add-to-cart/ *.php 文件,在您的活动主题文件夹中,创建一个带有 single-product 子文件夹的 woocommerce 文件夹。然后在这个子文件夹中复制位于 `plugins/woocommerce-plugin/templates/single-product/... 的整个 add-to-cart 文件夹之后最好只保留自定义 php 文件模板(当 woocommerce 模板更新时更容易)。

标签: php wordpress templates woocommerce wordpress-theming


【解决方案1】:

要覆盖 woocommerce 模板而不更改 woocommerce 插件文件夹中的任何内容,您需要将整个 templates 文件夹 (位于 woocommerce 插件中) 复制到您的活动子主题文件夹并重命名为 woocommerce (see here)。像这样,活动的 woocommerce 模板现在位于您的子主题文件夹中,您可以自定义它们……

【讨论】:

    【解决方案2】:

    您可以通过调用wc_get_template 钩子来做到这一点,只需告诉文件应该在哪里被覆盖:

    add_filter('wc_get_template', 'plugin_wc_templates', 10, 5);
    function plugin_wc_templates ($located, $template_name, $args, $template_path, $default_path) {
        $plugin_path = plugin_dir_path( __FILE__ );
        $newtpl = str_replace('woocommerce/templates', $plugin_path. '/templates', $located);
        if ( file_exists($newtpl) )
            return $newtpl;
        return $located;
    }
    

    记得打印$plugin_path 变量以确保路径生成正常。

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多