Modifying the css class of add to cart link

So I managed to change the text on Add to cart link inside the WP_Query but how about changing the class?

It seems to me that the filter is inside the add-to-cart.php file as follow:

echo apply_filters( 'woocommerce_loop_add_to_cart_link', // WPCS: XSS ok.
	sprintf( '%s',
		esc_url( $product->add_to_cart_url() ),
		esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
		esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
		isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
		esc_html( $product->add_to_cart_text() )
$product, $args );

But where does this $args[‘class’] come from? My first attempt would be to check where the file has been included ie searching for the filename everywhere. … Oh hey, I have included it myself.

Let’s get an output of $args before the function to see what I get: result: the $args is actually for the WP_Query.

How about calling

woocommerce_template_loop_add_to_cart( $args = array())

which it in-turn calls for the add-to-cart.php
Result: This is better, it automatically brings all the classes necessary so don’t need to modify it anymore. But just to learn something here let’s see if I can add a class to it, I tried:

woocommerce_template_loop_add_to_cart( array('class' => 'test-test'));

Result: all the classes vanished except test-test. I expected it to add to the list of classes but instead, it removed the others. As I search the use of this function apparently only the “quantity” is supposed to be passed to the function like this:

woocommerce_template_loop_add_to_cart( array(
    'quantity' => $atts['quantity'],

Inside the file: woocommerce\includes\class-wc-shortcodes.php

Leave a Reply

Your email address will not be published. Required fields are marked *