想要覆盖 woocommerce-functions.php 文件中编写的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21397325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:07:21  来源:igfitidea点击:

Want to overwrite functions written in woocommerce-functions.php file

phpwordpresswordpress-pluginwordpress-theming

提问by Naresh Walia

I want to modify/overwrite functions written in woocommerce-functions.php file but I don't want to modify woocommerce-functions.php file. That is I want to achieve this in plug-in or in my theme.

我想修改/覆盖在 woocommerce-functions.php 文件中编写的函数,但我不想修改 woocommerce-functions.php 文件。那就是我想在插件或我的主题中实现这一点。

采纳答案by flauntster

It is possible to override woocommerce functions, I did this recently and added all of my woocommerce extended functionality to my theme's functions.php file so that the woocommerce plugin files remained untouched and are safe to update.

可以覆盖 woocommerce 功能,我最近做了这个,并将我所有的 woocommerce 扩展功能添加到我的主题的 functions.php 文件中,以便 woocommerce 插件文件保持不变并且可以安全更新。

This page gives an example of how you can remove their action and replace it with your own - http://wordpress.org/support/topic/overriding-woocommerce_process_registration-in-child-theme-functionsphp

此页面提供了一个示例,说明如何删除他们的操作并将其替换为您自己的操作 - http://wordpress.org/support/topic/overriding-woocommerce_process_registration-in-child-theme-functionsphp

This page gives an example of extending upon their functions without removing their function, as well as using child themes - http://uploadwp.com/customizing-the-woocommerce-checkout-page/

此页面提供了一个在不删除其功能的情况下扩展其功能以及使用子主题的示例 - http://uploadwp.com/customizing-the-woocommerce-checkout-page/

Hope this helps :)

希望这可以帮助 :)

回答by Husain Ahmed

If you have a child theme, you can copy the relevant file to your theme and rewrite the copy. The copy will be used in preference to the WooCommerce version.

如果您有子主题,您可以将相关文件复制到您的主题并重写副本。该副本将优先于 WooCommerce 版本使用。

回答by Swapnali

WooCommerceprovides a templating system. It is possible to override woocommerce functions. great way to customize WooCommerce without modifying core files, is to use hooks -

WooCommerce提供了一个模板系统。可以覆盖 woocommerce 功能。在不修改核心文件的情况下自定义 WooCommerce 的好方法是使用钩子 -

If you use a hook to add or manipulate code, you can add your custom code to your theme functions.php file.

如果您使用钩子来添加或操作代码,您可以将您的自定义代码添加到您的主题 functions.php 文件中。

  • Using action hooks-
  • 使用动作钩子-

To execute your own code, you hook in by using the action hook do_action(‘action_name');.

要执行您自己的代码,您可以使用动作钩子 do_action('action_name'); 进行钩子。

See below for a great example on where to place your code:

有关放置代码的位置的一个很好的示例,请参见下面的示例:

add_action('action_name', 'your_function_name');

function your_function_name() 
{
// Your code
}
  • Using filter hooks-
  • 使用过滤器钩子-

Filter hooks are called throughout are code using apply_filter(‘filter_name', $variable);

过滤器钩子在整个代码中使用 apply_filter('filter_name', $variable);

To manipulate the passed variable, you can do something like the following:

要操作传递的变量,您可以执行以下操作:

add_filter('filter_name', 'your_function_name');

function your_function_name( $variable )
 {
// Your code
return $variable;
}

Here you can get WooCommerce Action and Filter Hook - https://docs.woothemes.com/wc-apidocs/hook-docs.html

在这里您可以获得 WooCommerce 操作和过滤器挂钩 - https://docs.woothemes.com/wc-apidocs/hook-docs.html

回答by TitanFighter

I was needed to add "Play" button for videos on mobile devices (by default this button is shown just on desktop).

我需要为移动设备上的视频添加“播放”按钮(默认情况下,此按钮仅显示在桌面上)。

I was needed to override the function in wp-content/themes/gon/framework/theme_functions.php:

我需要覆盖以下功能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>';
    }
}

I found this instructionwhich states If you use a hook to add or manipulate code, you can add your custom code to your theme's functions.php file.

我发现这个指令说明If you use a hook to add or manipulate code, you can add your custom code to your theme's functions.php file.

I already had wp-content/themes/gon-child/functions.php, (ie the original gontheme had been copied to gon-child), so what I did was:

我已经有了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>';
    }
}

?>