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

巧了我就是萌 提交于 2019-12-05 15:29:04

问题


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.


回答1:


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.

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

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/

Hope this helps :)




回答2:


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.




回答3:


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

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

  • Using action hooks -

To execute your own code, you hook in by using the action hook 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);

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



来源:https://stackoverflow.com/questions/21397325/want-to-overwrite-functions-written-in-woocommerce-functions-php-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!