How can we improve WooCommerce?

Change add to cart button and redirect to product single page

This is a bit complex for non developers but, instead adding more code I change the code and the result is the same.

See below:

Inside your plugin folder find woocommerce/templates/loop/add-to-cart.php

Firstly, backup the original file in case something goes wrong.

Secondly, open the file and replace the original code with:

<?php
/**
* Loop Add to Cart
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $product;
?>

<?php if ( ! $product->is_in_stock() ) : ?>

<a href="<?php echo apply_filters( 'out_of_stock_add_to_cart_url', get_permalink( $product->id ) ); ?>" class="button"><?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?></a>

<?php else : ?>

<?php
$link = array(
'url' => '',
'label' => '',
'class' => ''
);

$handler = apply_filters( 'woocommerce_add_to_cart_handler', $product->product_type, $product );

switch ( $handler ) {
case "variable" :
$link['url'] = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'variable_add_to_cart_text', __( 'Select options', 'woocommerce' ) );
break;
case "grouped" :
$link['url'] = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'grouped_add_to_cart_text', __( 'View options', 'woocommerce' ) );
break;
case "external" :
$link['url'] = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) );
break;
default :
if ( $product->is_purchasable() ) {
$link['url'] = apply_filters( 'add_to_cart_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'add_to_cart_text', __( 'See details', 'woocommerce' ) );
$link['class'] = apply_filters( 'add_to_cart_class', 'add_to_cart_button' );
} else {
$link['url'] = apply_filters( 'not_purchasable_url', get_permalink( $product->id ) );
$link['label'] = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) );
}
break;
}

echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="%s button attachment-shop_catalog_%s">%s</a>', esc_url( $link['url'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_attr( $product->product_type ), esc_html( $link['label'] ) ), $product, $link );
?>

<?php endif; ?>

Where you see 'See details', is the new text on the button and when you click on, the link will point to the product page instead to add to the cart.

The trick is here:

I replaced 'button product_type' for 'button attachment-shop_catalog' on here:

echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="%s button attachment-shop_catalog_%s">%s</a>', esc_url( $link['url'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_attr( $product->product_type ), esc_html( $link['label'] ) ), $product, $link );

and on this line:

$link['url'] = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );

replaced for

$link['url'] = apply_filters( 'add_to_cart_url', get_permalink( $product->id ) );

That's it.

Works perfectly well without adding any extra code.

7 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
fernandofas shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

1 comment

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...
  • Rob commented  ·   ·  Flag as inappropriate

    worked great, but will this be overwritten in a woocommerce update? I tried putting it in child-theme/woocommerce/templates/loop/add-to-cart.php but that didn't work.

    How else can I make sure this doesn't get overwritten in a woocommerce update?

WooCommerce: Category - Core

Feedback and Knowledge Base