我有一个问题:是否有一种方法可以通过插件覆盖WooCommerce的默认模板,就像您使用主题一样?我有这样的代码:
Class WoocommerceOverride {
public function woocommerce_locate_template( $template, $template_name, $template_path ) {
$plugin_path = SPSL_PLUGIN_PATH;
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path .= '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name,
$template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
//echo $template."<br>";
// Return what we found
return $template;
}
}
add_filter( 'woocommerce_locate_template', array('WoocommerceOverride', 'woocommerce_locate_template'), 10, 3 );
这段代码的问题是它只能部分地工作。它在某些方面起作用,在其他方面不起作用。例如,我根本无法定制archive-product.php。不管我在里面写什么,不管是代码还是纯文本,我都得不到任何结果。我复制了完全相同的模板文件从我的插件文件夹到我的主题文件夹,它的工作原理。然而,因为我需要这个插件,我不能走主题路线。
许多谢谢。- 使用过滤器
wc_get_template_part
,我们可以覆盖默认的WooCommerce模板部分。使用过滤器woocommerce_locate_template
,我们可以覆盖默认WooCommerce模板的。试试下面的示例代码片段。
<?php
/**
* Override default WooCommerce templates and template parts from plugin.
*
* E.g.
* Override template 'woocommerce/loop/result-count.php' with 'my-plugin/woocommerce/loop/result-count.php'.
* Override template part 'woocommerce/content-product.php' with 'my-plugin/woocommerce/content-product.php'.
*
* Note: We used folder name 'woocommerce' in plugin to override all woocommerce templates and template parts.
* You can change it as per your requirement.
*/
// Override Template Part's.
add_filter( 'wc_get_template_part', 'override_woocommerce_template_part', 10, 3 );
// Override Template's.
add_filter( 'woocommerce_locate_template', 'override_woocommerce_template', 10, 3 );
/**
* Template Part's
*
* @param string $template Default template file path.
* @param string $slug Template file slug.
* @param string $name Template file name.
* @return string Return the template part from plugin.
*/
function override_woocommerce_template_part( $template, $slug, $name ) {
// UNCOMMENT FOR @DEBUGGING
// echo '<pre>';
// echo 'template: ' . $template . '<br/>';
// echo 'slug: ' . $slug . '<br/>';
// echo 'name: ' . $name . '<br/>';
// echo '</pre>';
// Template directory.
// E.g. /wp-content/plugins/my-plugin/woocommerce/
$template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/';
if ( $name ) {
$path = $template_directory . "{$slug}-{$name}.php";
} else {
$path = $template_directory . "{$slug}.php";
}
return file_exists( $path ) ? $path : $template;
}
/**
* Template File
*
* @param string $template Default template file path.
* @param string $template_name Template file name.
* @param string $template_path Template file directory file path.
* @return string Return the template file from plugin.
*/
function override_woocommerce_template( $template, $template_name, $template_path ) {
// UNCOMMENT FOR @DEBUGGING
// echo '<pre>';
// echo 'template: ' . $template . '<br/>';
// echo 'template_name: ' . $template_name . '<br/>';
// echo 'template_path: ' . $template_path . '<br/>';
// echo '</pre>';
// Template directory.
// E.g. /wp-content/plugins/my-plugin/woocommerce/
$template_directory = untrailingslashit( plugin_dir_path( __FILE__ ) ) . 'woocommerce/';
$path = $template_directory . $template_name;
return file_exists( $path ) ? $path : $template;
}
几个月前我也有同样的要求。所以在网上搜索了更多,找到了有用的代码,这对我有帮助(根据我的要求进行了更多的定制)。
有关详细的代码和解释,请检查此链接和此链接。这种方法可能与您目前使用的方法不同,但它会导致在插件
您应该尝试在// Use default template
代码之前添加此代码:
if( $template_name == '{template part name}') {
$template = $plugin_path . $template_name;
}
在我的例子中{模板部件名称}是global/quantity-input.php
您可以通过在代码中临时添加这一行来查找模板部件的确切名称:
print_r($template_name);
我知道现在回答有点晚了,但也许它会对其他人有用。记住woocommerce_locate_template
是被废弃的。因此,可能在某个地方有更多的"最新"解决方案。