I'm trying to remove the navigation between products that appears right after the product title in my products pages.
The navigation is set in the woocommerce.php
file of my theme:
/**
* Class for all WooCommerce template modification
*
* @version 1.0
*/
class MrBara_WooCommerce {
...
/**
* Hooks to WooCommerce actions, filters
*
* @since 1.0
* @return void
*/
function hooks() {
...
// Add prev/next product
if ( in_array( mrbara_theme_option( 'product_page_layout' ), array( '1', '2', '8', '9', '12' ) ) ) {
add_action( 'woocommerce_single_product_summary', array( $this, 'products_links' ), 0 );
}
...
/**
* Display products link
*
* @since 1.0
*/
function products_links() {
if ( function_exists( 'is_product' ) && ! is_product() ) {
return;
}
$prev_link = '<span class="ion-ios-arrow-back"></span>';
$next_link = '<span class="ion-ios-arrow-forward"></span>';
...
?>
<div class="products-links">
<?php
previous_post_link( '<div class="nav-previous">%link</div>', $prev_link );
next_post_link( '<div class="nav-next">%link</div>', $next_link );
?>
</div>
<?php
}
}
I'm just starting understand how Wordpress work, and for what I've learn until now, I think I have to remove the action products_links
.
Test 1
So, in my functions.php
in my child theme, I've wrote this:
// Remove previous and next link in the product title
add_action('woocommerce_single_product_summary', 'remove_products_links');
function remove_products_links() {
remove_action('products_links', 'woocommerce_single_product_summary' );
}
I added my "remove function" to the woocommerce_single_product_summary
as the action that adds the products links is hooked to it.
But this doesn't work.
So I gone deeper in the documentation
But this not worked.
So, digging deeper in the Wordpress' documentation, I read this:
If an action has been added from within a class, for example by a plugin, removing it will require accessing the class through a variable that holds the class instance. Unless the function is static in which case you could call the class and function directly.
So, this is my case! My action is added by the class MrBara_WooCommerce
in the method hooks()
and the action's method products_links()
isn't static, so I need to call the class, too.
Test 2
add_action('woocommerce_single_product_summary', 'remove_products_links');
function remove_products_links() {
remove_action('products_links', ['MrBara_WooCommerce', 'woocommerce_single_product_summary'] );
}
Also this didn't work.
I've also tried to play with priorities, setting my remove_products_links
at 9
, at 11
, at 10
and at 0
(to match the priority of the original added action) but nothing of this worked.
I think I'm very close to the solution but my inexperience causes me to miss some relevant settings.
Can you help me better understand why my remove_action
doesn't work?
UPDATE
Continuing reading I've discovered that the functions.php
file of my child theme is loaded BEFORE the files of the parent theme (https://wordpress.stackexchange.com/a/170665/57145).
I've had a perception of this printing the entire $wp_filter
variable using the functions.php
file of the child theme.
So, printing, I saw the remove_products_links
action that I registered, but didn't saw the products_links
action that I'm trying to remove.
So I thought that there must be some problems in the registered order.
Now, I think that I can't remove the action as when I register the remove action the action that I want to remove doesn't exist.
The question, so, becomes: how can I remove an action from the parent theme using the child theme?