Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag 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?
Avatar of eemit
eemit
Flag of Germany image

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

Avatar of 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.
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.
Can you give me a working example from above on how to word that.  From there I can probably get it.  Thanks in advance.
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 );
@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

ASKER CERTIFIED SOLUTION
Avatar of eemit
eemit
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked!  Thanks a ton!