1
votes

I have been trying to remove the link to the single product page of the Woocommerce plugin. On this forum many people asking questions about it and have read most of them. But nearly all of these questions are old. However as far as I understand now I have to use the functions.php and content-page.php to achieve this.

So after reading all the info on the i-net I came up with this code in the functions.php (which is inside my child theme folder)

if ( !function_exists('add_remove_hooks')):
function add_remove_hooks() {
    remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
    }
endif;
add_action( 'init', 'add_remove_hooks' );

I have tried all kind of variants like only put in the remove_action rows, not wrapt in a function or use an other action in the add_action function, but none of them seem to work. The syntax, however, is as it should be.

The (part of)content-page.php looks like

<li <?php post_class( $classes ); ?>>
    <?php do_action( 'woocommerce_before_shop_loop_item' ); ?>

        <?php
            /**
             * woocommerce_before_shop_loop_item_title hook
             *
             * @hooked woocommerce_show_product_loop_sale_flash - 10
             * @hooked woocommerce_template_loop_product_thumbnail - 10
             */
            do_action( 'woocommerce_before_shop_loop_item_title' ); ?>

        <?php if($product_style == 'classic') { 
            do_action( 'woocommerce_shop_loop_item_title' );
            do_action( 'woocommerce_after_shop_loop_item_title' ); 
        } ?>

    <?php do_action( 'woocommerce_after_shop_loop_item' ); ?>

</li>

I don't know how to debug in PHP, but I know the files are "used" by WP because if I change something in one of these files I see it on screen. However, the links are still in place. The remove_action is not working.

Does anyobody have another suggestion on how to accomplish this? Of what to look for?

Used WP 5.2 and Woocommerce 3.0

UPDATE:

Thanks to Alfaraz I was able to solve the problem. Putting the remove_actions in the functions.php of the child theme now also works. I didn't realize I also had to set the active theme in WP to my Child theme. That did the trick in the end.

2
Are you using the latest version of woocommerce? 3.6.4? Also, the file these hooks are called from is content-product.php. Is the file you are using an override?gregbast1994
@gregbast1994 Yes, I use 3.6.4 and no I am not using an override. I only added the functionality of removing the hooks in de functions.php. Why? Do I have to change something in the content-product.php to get it to work?Stephan

2 Answers

1
votes

Put below code in the functions.php file of your theme.

// Remove links to the product details pages from the product listing page of a WooCommerce store
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
0
votes

Try this:

function remove_direct_link_to_product_page() {
 if ( is_product() && is_product_category(array('category-slug') ) ) { 
remove_action( ‘woocommerce_before_shop_loop_item’,
‘woocommerce_template_loop_product_link_open’, 10 ); 
remove_action( ‘woocommerce_after_shop_loop_item’,
‘woocommerce_template_loop_product_link_close’, 5 );
 }
 }