Fjott Fjott - 4 months ago 16
PHP Question

Removing product Link from the product price on content-product.php template

I am using the Woocommerce WP plugin. On my Shop page, I would like to remove the link

<a>
to the product page from the
<span class="price">
.

In this article the author is suggesting to alter the tags in /your-child-theme/woocommerce/content-product.php WooCommerce template, but this is no longer possible in the newest WooCommerce version 2.6+.

I have only found a way to remove the whole link, not just the price-link.

How can I move the link only from the price?

Here it is what i would like:

<a href="www.link-to-single-product.com">
<h3>TITLE</h3>
<img width="300" height="200" src="www.link.com">

</a> <!-- <===== TO HERE -->

<span class="price">
<span class="amount">PRICE</span>
</span>

</a> <!-- Moving this close tag FROM HERE -->

Answer

Update2 (there where 2 missing ";" in the code, that was doing the 500 error. So you should retry.

Here is an extract from content-product.php template (with solution (1) and (2):

/**
 * woocommerce_after_shop_loop_item_title hook.
 *
 * @hooked woocommerce_template_loop_rating - 5
 * <===  <===  <===  <===  <===  <===  <===  <===  <=== (2) To here
 * @hooked woocommerce_template_loop_price - 10  ====> (1) From here
 */
do_action( 'woocommerce_after_shop_loop_item_title' );

/**
 * woocommerce_after_shop_loop_item hook.
 * 
 * @hooked woocommerce_template_loop_product_link_close - 5  ==> (2) From here
 * <===  <===  <===  <===  <===  <===  <===  <===  <===  <=== (1) To here
 * @hooked woocommerce_template_loop_add_to_cart - 10
 */
do_action( 'woocommerce_after_shop_loop_item' );

So there is 2 alternatives to achieve take out the price from <a href="…></a>:

  1. We are going to try to unhook 'woocommerce_template_loop_price' from one location to another just after 'woocommerce_template_loop_product_link_close' hooked function.

Then you could try to add this code snippet to the function.php file of your active child theme:

add_action('init', function(){
   remove_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
   add_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_price', 7);
}
  1. We are going to try to unhook 'woocommerce_template_loop_product_link_close' from one location to another just before 'woocommerce_template_loop_price' hooked function.

Then you could try to add this code snippet to the function.php file of your active child theme:

add_action('init', function(){
   remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
   add_action('woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 7);
}

I haven't test it yet, but this time it should work without error 500.

You can try them also add_action('init', function(){ … } in case.