Woo Commerce Override Actions Functions

Robert Granlund
Robert Granlund used Ask the Experts™
on
I have a Wordpress Installation and am using Woo Commerce.  I want to change / Override one of the WooCommerce Public Functions.

add_action( 'woocommerce-product-addons_end', array( $this, 'totals' ), 10 );	
do_action( 'woocommerce-product-addons_end', $post_id );

public function totals( $post_id ) {

		global $product;

		if ( ! isset( $product ) || $product->id != $post_id )
			$the_product = get_product( $post_id );
		else
			$the_product = $product;

		echo '<div id="product-addons-total" data-type="' . $the_product->product_type . '" data-price="' . ( is_object( $the_product ) ? $the_product->get_price() : '' ) . '"></div>';
	}

Open in new window

BUT I'm not sure how to write it.  This is what I have so far but it shows both my action and the original.

add_action( 'woocommerce-product-addons_end', 'add_custom_price');
do_action( 'woocommerce-product-addons_end', 'add_custom_price');

function add_custom_price( $post_id ) {

		global $product;

			$the_product = $product;

		echo '<div id="CUSTOM-ID" data-type="' . $the_product->product_type . '" data-price="MY CUSTOM PRICE"></div>';
	}

Open in new window


Help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012

Commented:
Try:
do_action( 'woocommerce-product-addons_end', $post_id );

Open in new window

Instead of:
do_action( 'woocommerce-product-addons_end', 'add_custom_price');

Open in new window

Author

Commented:
@eemit
It still adds the new action but it also adds the other action also.  It adds both.  i just want to add my new action and not the old.
Top Expert 2012

Commented:
Check if you also need to do remove_action.
This function removes a function attached to a specified action hook.
This method can be used to remove default functions attached to a specific action hook and possibly replace them with a substitute.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
Can you give me a working example from above on how to word that.  From there I can probably get it.  Thanks in advance.
Top Expert 2012

Commented:
Usage:
<?php remove_action( $tag, $function_to_remove, $priority ); ?>

$tag is action hook name.
To remove a hook, the $function_to_remove and $priority arguments must match when the hook was added.

If an action has been added from within a class, for example by a plugin,
removing it will require accessing the class variable.

global $your_class;
remove_action( 'yourhook', array( $your_class, 'your_function' ), 10 );

Author

Commented:
@eemit, I really appreciate your help!  I think I an going down the wrong path.  What I need to do is replace the function "totals" with my function "add_custom_price".
When I do the following, it still inserts the Totals Function:

global $Product_Addon_Display;
remove_action( 'woocommerce-product-addons_end', array( $Product_Addon_Display, 'totals' ), 10 ); 

add_action( 'woocommerce-product-addons_end', 'add_custom_price');
do_action( 'woocommerce-product-addons_end', '$post_id ' );

function add_custom_price( $post_id ) {
	
	
	global $product;

	$the_product = $product;

	echo '<div id="product-addons-total" data-type="' . $the_product->product_type . '" data-price="1000"></div>';
	}

Open in new window

Top Expert 2012
Commented:
What happens if you call your remove_action and add_action within 'init' hook,
or if your code is in a plugin during 'plugins_loaded' hook (you cannot successfully remove the action before it has been added):
add_action( 'init', 'yourprefix_remove_wc_hook');
function yourprefix_remove_wc_hook {
	global $Product_Addon_Display;
	remove_action( 'woocommerce-product-addons_end', array( $Product_Addon_Display, 'totals' ), 10 ); 

	add_action( 'woocommerce-product-addons_end', 'add_custom_price');
}

Open in new window

Author

Commented:
That worked!  Thanks a ton!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial