Avatar of Robert Granlund
Robert Granlund
Flag for United States of America asked on

Woo Commerce Override Actions Functions

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?
PHPWordPressSQL

Avatar of undefined
Last Comment
Robert Granlund

8/22/2022 - Mon
eemit

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

Robert Granlund

ASKER
@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.
eemit

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Robert Granlund

ASKER
Can you give me a working example from above on how to word that.  From there I can probably get it.  Thanks in advance.
eemit

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 );
Robert Granlund

ASKER
@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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
eemit

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Robert Granlund

ASKER
That worked!  Thanks a ton!