Remove an action - Wordpress (Specifically Woocommerce PIP plugin) - wordpress

I have Woocommerce website with the plugin Print Invoices/Packing Lists installed.
I'm trying to remove an action and I've narrowed down the action to this (shortened) code;
class WC_PIP_Document_Invoice extends WC_PIP_Document {
public function __construct( array $args ) {
parent::__construct( $args );
// add a "View Invoice" link on order processing/complete emails sent to customer
add_action( 'woocommerce_email_order_meta', array( $this, 'order_paid_email_view_invoice_link' ), 40, 3 );
}
}
So I'm looking at removing this using; https://codex.wordpress.org/Function_Reference/remove_action
However as the action as added within a class, I can't quite work out what to pass into the function name. Would it be something like;
remove_action( 'woocommerce_email_order_meta', array( 'WC_PIP_Document_Invoice', 'order_paid_email_view_invoice_link' ), 40 );
Can anyone point me in the right direction?
Many thanks

Try following code that should work for you.
add_action( 'init', 'wpse_106269_remove_hooks', 11 );
function wpse_106269_remove_hooks(){
remove_action( 'woocommerce_email_order_meta', 'action_woocommerce_email_order_meta', 10, 4 );
}

Related

Action init not firing after update WP 5.2.2

I recently updated my website to WP 5.2.2 and 'init' action seems not to fire anymore.. I had few VC element mapped to that action but they are not working anymore..
This is the vc_map init code:
class VC_Extensions_FancyBox extends WPBakeryShortCode {
function __construct() {
if( has_action('init') ){
die('has init'); // this is printing correctly
}
add_action( 'init', array($this, 'banner_init'));
add_shortcode('vc_fancybox', array($this, 'vc_fancybox_func'));
}
function banner_init() {
if( has_action('init') ){
die('has banner_init'); //this is not printing at all..
}
vc_map( array(........) );
}
function vc_fancybox_func() {
....
}
}
I added 2 checks in the code, the first one debugs correctly, the other one doesn't. Any idea why this is happening?
Thank you very much
EDIT: using action 'wp_loaded' the element is showing correctly...
As per Edit, I just replace 'init' to 'wp_loaded' and Worked perfectly
add_action( 'wp_loaded', array( $this, 'vc_progressbar_mapping' ) );
//add_action( 'init', array( $this, 'vc_progressbar_mapping' ) );

Wordpress remove_action within class

I am trying to remove an action added by the Woocommerce Memberships plugin. Tracing back the actions contains functions it is initially added to the hook under
class WC_Memberships_Frontend {
public function __construct() {
add_action( 'woocommerce_thankyou', array( $this, 'maybe_render_thank_you_content' ), 9 );
But this class is called by a private function and is a protected by the variable that its parent class declaration (as far as I can tell)
class WC_Memberships extends Framework\SV_WC_Plugin {
protected $frontend;
private function frontend_includes() {
// init shortcodes
require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-shortcodes.php' );
\WC_Memberships_Shortcodes::initialize();
// load front end
$this->frontend = $this->load_class( '/includes/frontend/class-wc-memberships-frontend.php', 'WC_Memberships_Frontend' );
}
I have search and tried a number of ways to remove this
remove_action( 'woocommerce_thankyou', array( 'WC_Memberships', 'maybe_render_thank_you_content' ), 9 );
remove_action( 'woocommerce_thankyou', array( 'WC_Memberships_Frontend', 'maybe_render_thank_you_content' ), 9 );
global $WC_Memberships;
remove_action( 'woocommerce_thankyou', array( $WC_Memberships, 'maybe_render_thank_you_content' ), 9 );
global $WC_Memberships_Frontend;
remove_action( 'woocommerce_thankyou', array( $WC_Memberships_Frontend, 'maybe_render_thank_you_content' ), 9 );
None of the above work, and other ways of trying to call WC_Memberships()->frontend; throw errors like 'Cannot access protected property WC_Memberships::$frontend'
Im not sure if the private function or protected variable are getting in the way, or if Im just not understanding something about removing an action within a class or nested classes but help would be greatly appreciated.
Edit:
Based on the code found here Ive tried
remove_action( 'woocommerce_thankyou', array( wc_memberships()->get_frontend_instance(), 'maybe_render_thank_you_content', 9 ) );
but still no success.
My issue was trying to figure out how to match the $this in the original add_action add_action( 'woocommerce_thankyou', array( $this, 'maybe_render_thank_you_content' ), 9 ); to remove it.
I finally stumbled on this chunk of code which showed me how to reference the proper instance of the class.
Final working code:
remove_action( 'woocommerce_thankyou', array( wc_memberships()->get_frontend_instance(), 'maybe_render_thank_you_content', 9 ) );
As you probably know, it's usually not good practice to modify core or plugin files. Using a modification to the answer here , you could try adding to your themes functions.php file
function remove_aggravating_wcactions()
{
remove_action( 'woocommerce_thankyou', 'maybe_render_thank_you_content', 19 );
remove_action( 'woocommerce_thankyou', 'maybe_some_other_content', 20);
...
}
add_action('template_redirect','remove_aggravating_wcactions');

Removing the my account-dashboard and redirect to another endpoint like downloads

I don't want the customers to get linked to the myaccount-dashboard with the annoying Hello xy, from here you can blabla, when they click on "My Account".
Is there an endpoint for the my account dashboard? I didn't find it on the backend in woocommerce>Settings>Accounts...
What's working is: I set up a custom link under menu/navigation... called it "My Account" and set the link to /myaccount/downloads for example.
So, when customers are logged in and they click on My Account, they get redirected to Downloads.
I'm wondering, is there another way to get rid of the dashboard?
Or a redirect solution? Thanks.
Remove it using below function. change downloads to your requirement.
function custom_my_account_menu_items( $items ) {
unset($items['downloads']);
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
Looking more closely it seems like there are a handful of ways to do this. Take a look at the my-account.php template. You could override this, but I found that if you remove the woocommerce_account_content hook, you get a deprecation notice and WooCommerce thinks you have an outdated template and adds the content anyway.
Looking at the template you will see two hooks. The side menu navigation is added to the woocommerce_account_navigation and the content is added to woocommerce_account_content function. You can remove the defaults from their hooks and add back just the downloads content.
function so_41983566_remove_account_dadshboard(){
remove_action( 'woocommerce_account_navigation', 'woocommerce_account_navigation' );
remove_action( 'woocommerce_account_content', 'woocommerce_account_content' );
add_action( 'woocommerce_account_content', 'so_41983566_download_content' );
}
add_action( 'woocommerce_account_navigation', 'so_41983566_remove_account_dadshboard', 1 );
function so_41983566_download_content(){
do_action( 'woocommerce_account_downloads_endpoint' );
}
Or woocommerce_account_content() and woocommerce_account_navigation() are both pluggable functions and you can just define new versions in your theme/plugin.
This link explains everything:
https://github.com/woocommerce/woocommerce/wiki/Customising-account-page-tabs
First you need to create an endpoint:
function my_custom_endpoints() {
add_rewrite_endpoint( 'my-custom-endpoint', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_custom_endpoints' );
function my_custom_query_vars( $vars ) {
$vars[] = 'my-custom-endpoint';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 0 );
Then create the menu item:
function my_custom_my_account_menu_items( $items ) {
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
$items['my-custom-endpoint'] = __( 'My Custom Endpoint', 'woocommerce' );
$items['customer-logout'] = $logout;
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'my_custom_my_account_menu_items' );
Add content to the endpoint
function my_custom_endpoint_content() {
echo '<p>Hello World!</p>';
}
add_action( 'woocommerce_account_my-custom-endpoint_endpoint', 'my_custom_endpoint_content' );

Woocommerce override plugin action

The file email-order-items.php has the following line of code:
echo "\n" . sprintf( __( 'Cost: %s', 'woocommerce' ), $order->get_formatted_line_subtotal( $item ) );
The following action hook has been added to a plugin I am using (Woocommerce Composite Products):
add_action( 'woocommerce_order_formatted_line_subtotal', array( $this, 'wc_cp_order_item_subtotal' ), 10, 3 );
I would like to override the function wc_cp_order_item_subtotal to change the way to item subtotal is displayed. I have tried adding the following to my child theme functions.php, but it doesn't do anything.
remove_action( 'woocommerce_order_formatted_line_subtotal', 'wc_cp_order_item_subtotal', 10);
add_action( 'woocommerce_order_formatted_line_subtotal', 'child_wc_cp_order_item_subtotal', 10, 3);
function child_wc_cp_add_order_item_meta( $order_item_id, $cart_item_values, $cart_item_key = '' ) {
return 'xxxxxxx';
}
Any tips to help me get this working would be greatly appreciated.
It isn't mentioned in the Codex, but I usually call the remove_action() function from a hook.
Also, as covered in the Codex example for action added from classes, you need to access the class instance or variable.
I don't see wc_cp_order_item_subtotal in the Composite plugin anywhere, so I presume you aren't using Woo's version. In which case, I don't have access to the code and can't tell you exactly how to access the class variable.
But if you were using Woo's Composite Products it would be as follows:
Edited for Composites 2.4
function so_remove_action(){
global $woocommerce_composite_products;
remove_action( 'woocommerce_order_formatted_line_subtotal', array( $woocommerce_composite_products->order, 'wc_cp_order_item_subtotal' ), 10, 3 ); //not sure why it isn't a filter, but also not sure if there is a huge difference
}
add_action('init', 'so_remove_action');

WP Plugin: using add_filter inside of a class

I am using WP v3.3.1, and I am trying to make a plugin. I have gotten it semi-working. It initiated, and add_action works, but for some reason my filters don't get triggered. When I googled around, I saw I was supposed to do it like this, but it is not working. I also tried including it outside of the class, which didn't work either. The error log is written to from the constructor, but not the xmlAddMethod. I tested the xmlrpc call in a single file, and it worked, but having problems making classes.
//DOESN'T WORK HERE
add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );
class TargetDomain extends Domain
{
public function __construct()
{
error_log('TARGET: __construct');
//DOESN'T WORK HERE EITHER
add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );
parent::__construct();
}
function xmlAddMethod( $methods )
{
error_log('TARGET: xml_add_method');
$methods['myBlog.publishPost'] = 'publishMyPost';
return $methods;
}
Change this:
add_filter( 'xmlrpc_methods', array( &$this, 'xmlAddMethod') );
To:
add_filter( 'xmlrpc_methods', array( 'TargetDomain', 'xmlAddMethod') );
You can also use php's magic __CLASS__ constant.
add_filter( 'xmlrpc_methods', array( __CLASS__, 'xmlAddMethod') );

Resources