I'm using the WooCommerce Points and Rewards plugin.
By default, the single product message (Earn up to x Points.) appears immediately after product excerpt. However, I want change it.
I already tried a lot of things, but without success... but I think that I'm close to the right answer.
I hope that someone can give me a help.
function test() {
global $wc_points_rewards;
remove_action( 'woocommerce_single_product_summary', array($wc_points_rewards->product, 'render_product_message' ) );
}
add_action( 'woocommerce_single_product_summary', 'test', 5 );
The class that adds the "render_product_message" is WC_Points_Rewards_Product that is initialized on WC_Points_Rewards.
WC_Points_Rewards_Product (i'm only posting the related method)
public function __construct()
{
// add single product message immediately after product excerpt
add_action('woocommerce_single_product_summary', array($this, 'render_product_message'));
}
WC_Points_Rewards (i'm only posting the related method).
The includes method is called on __construct method of this class.
private function includes()
{
// product class
require('includes/class-wc-points-rewards-product.php');
$this->product = new WC_Points_Rewards_Product();
}
This works for me:
add_action( 'init', 'remove_points');
function remove_points()
{
global $wc_points_rewards;
remove_action( 'woocommerce_before_add_to_cart_button', [ $wc_points_rewards->product, 'render_product_message' ], 15 );
}
You are better off renaming the fields in the class-wc-points-rewards-product.php file to show "after" or "before" an element. Sure, you'll have to rename every time there's an update but writing a hook might mess something up.
Related
I have a WooCommerce site and in the single product page I have a custom checkbox to agree to terms before the add to cart button, but I am trying to add a true/false field in the dashboard so that this checkbox can be moved to a different position on the page.
My functions look like this:
add_action( 'acf/init', "acf_move_checkbox", 10 );
function acf_move_checkbox() {
$move_cart = get_field('move_cart');
if ($move_cart) {
add_action( 'woocommerce_after_single_product', "acf_product_terms", 10 );
} else {
add_action( 'woocommerce_before_add_to_cart_form', "acf_product_terms", 10 );
}
// More code
}
And nothing is happening, am I along the right lines with this or way off?
While acf/init is similar to the WordPress init action, I wouldn't use it.
Init hooks are performed constantly.. since you want to apply an action on the single product page it's best to use a hook that only applies to that page, and will only run on those kinds of pages.
For example you can use the woocommerce_single_product_summary hook. It might also be useful to test fields by hard coding before retrieving them.
So you get:
function action_woocommerce_single_product_summary() {
// Get field
//$move_cart = get_field( 'move_cart' );
// Set true OR false
$move_cart = false;
// When true
if ( $move_cart ) {
add_action( 'woocommerce_after_single_product', 'my_callback_function', 9 );
} else {
add_action( 'woocommerce_before_add_to_cart_form', 'my_callback_function', 9 );
}
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 1 );
function my_callback_function() {
echo '<p style="color: red; font-size: 20px;">Hello World!</p>';
}
If the above step works, you can replace the hard coded field with the desired code/field
I'm trying to set validation on "add cart" button to my woocommerce.
I use this code in functions.php, but it is not executed:
function test( $passed ) {
//exit(); //it would broke the code
$passed = false;
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'test', 10, 5 );
I try to set exit() on test() function but nothing. What can i verify?
If you look at the woocommerce source code you can see an example of how filters are added to woocommerce_add_to_cart_validation: https://github.com/woocommerce/woocommerce/blob/d7f768f77919a3f7a059d915894d0e87b2cb3ab1/includes/wc-cart-functions.php#L13-L27
I have personally added woocommerce filters inside of a child theme: https://developer.wordpress.org/themes/advanced-topics/child-themes/ in the file functions.php, wrapped in the <?php ... ?> tag.
You could be running into a priority issue, see this comment for more info on how that could impact your function: https://wordpress.stackexchange.com/a/7998/120919
What is the procedure of overriding functions that are hooked into WordPress?
The following function in wc-template-functions.php is one of the functions I'm looking to override:
function woocommerce_template_loop_product_link_close() {
echo '</a>';
}
It is hooked in at wc-template-hooks.php:
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
If I write the following code, the function is then unhooked:
function remove_default_wc_behaviour() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
}
add_action( 'init', 'remove_default_wc_behaviour' );
But what if I want to override the function, ie. implement my own functionality instead of the default functionality from WooCommerce?
Would I call a remove_action() first, and then hook in my function with add_action(), or is there a procedure which can simply override the function that is hooked in my WooCommerce?
Thanks in advance
The first option is the right one: In your main function hooked in init action hook, you can add your replacement hooked function:
add_action( 'init', 'replace_default_wc_behaviour' );
function remove_default_wc_behaviour() {
// remove the default behavior
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
// Replace by your custom behavior
add_action( 'woocommerce_after_shop_loop_item', 'custom_WC_loop_product_link_close', 5 );
function custom_WC_loop_product_link_close() {
// Do your custumizations
// add the close tag
echo '</a>';
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
I am making an ecommerce website using woocommerce, now I want to call a function after use fill the checkout form and submit it. Is there any filter or hooks that allow me to do that?
Or you can:
add_action( 'woocommerce_thankyou', array('Wc_class', 'my_uber_function'));
class Wc_class{
public static function my_uber_function($order_id)
{
stuff to do here
}
}
If you want to do something after WooCommerce order completed you can hook into woocommerce_order_status_completed.
add_action( 'woocommerce_order_status_completed', 'my_function' );
/*
* Do something after WooCommerce sets an order on completed
*/
function my_function($order_id) {
// order object (optional but handy)
$order = new WC_Order( $order_id );
// do some stuff here
}
I'm developing a plugin to customise the woocommerce registration and trying to avoid direct editing of the core files.
I need to override or replace process_registration action in woocommerces includes/class-wc-form-handler.php file through my plugin.
add_action( 'init', array( $this, 'process_registration' ) );
I tried following links, but they didn't work. Also, the files mentioned on those pages doesn't exist on woocommerce current version. I also checked woocommerce documentation, but it seems they don't have a hook for that.
http://wordpress.org/support/topic/overriding-woocommerce_process_registration-in-child-theme-functionsphp
Woocommerce Hooks:
http://docs.woothemes.com/document/hooks/
I'd really appreciate any help!
Two options for that and considering that WC method starts like:
class WC_Form_Handler
public function __construct() {
add_action( 'init', array( $this, 'process_registration' ) );
}
public function process_registration() {
if ( ! empty( $_POST['register'] ) ) {
wp_verify_nonce( $_POST['register'], 'woocommerce-register' );
# etc
}
}
}
new WC_Form_Handler();
Add an init hook with top priority and duplicate then unset($_POST['register']). WC doesn't specify a priority, so it's running on 10, which is default.
add_action( 'init', function() { /* dup, unset, do your thing */ }, 1 ); // priority 1
Track down that evil anonymous object where Woo is hidding its hook, so that you can:
// pseudo code, go to WPSE for the real thing
remove_hook( 'init', 'woo_process_registration' );