【发布时间】:2014-02-19 06:19:25
【问题描述】:
我想修改/覆盖写在 woocommerce-functions.php 文件中的函数,但我不想修改 woocommerce-functions.php 文件。那就是我想在插件或我的主题中实现这一点。
【问题讨论】:
标签: php wordpress wordpress-theming
我想修改/覆盖写在 woocommerce-functions.php 文件中的函数,但我不想修改 woocommerce-functions.php 文件。那就是我想在插件或我的主题中实现这一点。
【问题讨论】:
标签: php wordpress wordpress-theming
可以覆盖 woocommerce 功能,我最近这样做了,并将我所有的 woocommerce 扩展功能添加到我的主题的 functions.php 文件中,以便 woocommerce 插件文件保持不变并且可以安全更新。
此页面提供了一个示例,说明如何删除他们的操作并将其替换为您自己的 - http://wordpress.org/support/topic/overriding-woocommerce_process_registration-in-child-theme-functionsphp
此页面提供了一个在不删除其功能的情况下扩展其功能以及使用子主题的示例 - http://uploadwp.com/customizing-the-woocommerce-checkout-page/
希望这会有所帮助:)
【讨论】:
如果您有子主题,您可以将相关文件复制到您的主题并重写副本。该副本将优先使用 WooCommerce 版本。
【讨论】:
WooCommerce 提供了一个模板系统。可以覆盖 woocommerce 功能。在不修改核心文件的情况下自定义 WooCommerce 的好方法是使用钩子 -
如果您使用挂钩来添加或操作代码,则可以将自定义代码添加到主题 functions.php 文件中。
要执行您自己的代码,您可以使用动作挂钩 do_action('action_name'); 进行挂钩。
请参阅下面的代码放置位置的一个很好的示例:
add_action('action_name', 'your_function_name');
function your_function_name()
{
// Your code
}
过滤器钩子在代码中使用 apply_filter(‘filter_name’, $variable);
要操作传递的变量,您可以执行以下操作:
add_filter('filter_name', 'your_function_name');
function your_function_name( $variable )
{
// Your code
return $variable;
}
在这里您可以获得 WooCommerce 操作和过滤器挂钩 - https://docs.woothemes.com/wc-apidocs/hook-docs.html
【讨论】:
我需要为移动设备上的视频添加“播放”按钮(默认情况下,此按钮仅显示在桌面上)。
我需要重写wp-content/themes/gon/framework/theme_functions.php中的函数:
function ts_template_single_product_video_button(){
if( wp_is_mobile() ){
return;
}
global $product;
$video_url = get_post_meta($product->id, 'ts_prod_video_url', true);
if( !empty($video_url) ){
$ajax_url = admin_url('admin-ajax.php', is_ssl()?'https':'http').'?ajax=true&action=load_product_video&product_id='.$product->id;
echo '<a class="ts-product-video-button" href="'.esc_url($ajax_url).'"></a>';
}
}
我找到了this instruction,上面写着If you use a hook to add or manipulate code, you can add your custom code to your theme’s functions.php file.
我已经有了wp-content/themes/gon-child/functions.php,(即原来的gon主题已经复制到gon-child),所以我做的是:
// Enable tour video on mobile devices
remove_action('ts_before_product_image', 'ts_template_single_product_video_button', 1);
add_action('ts_before_product_image', 'ts_template_single_product_video_button_w_mobile', 1);
function ts_template_single_product_video_button_w_mobile(){
global $product;
$video_url = get_post_meta($product->id, 'ts_prod_video_url', true);
if( !empty($video_url) ){
$ajax_url = admin_url('admin-ajax.php', is_ssl()?'https':'http').'?ajax=true&action=load_product_video&product_id='.$product->id;
echo '<a class="ts-product-video-button" href="'.esc_url($ajax_url).'"></a>';
}
}
?>
【讨论】: