skip to Main Content

I want to add a class only on the add to cart button text for design purpose.

I tried but it doesn’t work :

add_filter( 'woocommerce_product_single_add_to_cart_text', 'add_custom_class_to_add_to_cart_button_text' );
function add_custom_class_to_add_to_cart_button_text() {
    $text = ('<span class="custom-class">' . __( 'Add to Cart Button Text', 'woocommerce' ) . '</span>') ;
    return $text;
}

Do you know how to target ATC button text only or wrap the text ?

2

Answers


  1. Please try this:

    add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' ); 
    function custom_woocommerce_product_add_to_cart_text() {
    global $product;
    
    $product_type = $product->product_type;
    
    switch ( $product_type ) {
        case 'external':
            return __( 'Take me to their site!', 'woocommerce' );
        break;
        case 'grouped':
            return __( 'VIEW THE GOOD STUFF', 'woocommerce' );
        break;
        case 'simple':
            return __( 'WANT. NEED. ADD!', 'woocommerce' );
        break;
        case 'variable':
            return __( 'Select the variations, yo!', 'woocommerce' );
        break;
        default:
            return __( 'Read more', 'woocommerce' );
    }
    

    }

    Login or Signup to reply.
  2. If you want to add a class to the "Add to Cart" button text on a single product page in WooCommerce, you can use the following code snippet. This code uses a filter to modify the button text and wrap it in a span element with your custom class:

    add_filter( 'woocommerce_product_single_add_to_cart_text', 'add_custom_class_to_add_to_cart_button_text' );
    
    function add_custom_class_to_add_to_cart_button_text( $text ) {
        // Add your custom class to the button text
        $text = '<span class="custom-class">' . esc_html__( 'Add to Cart', 'woocommerce' ) . '</span>';
        return $text;
    }
    

    Make sure to place this code in your theme’s functions.php file or in a custom plugin. This code will wrap the "Add to Cart" button text in a span element with the class "custom-class," which you can then style using CSS for your design purposes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search