skip to Main Content

I have a Woocommmerce plugin that alters the text of the single add to cart button text.

The function in question does the following:

public function single_add_to_cart_text() {
    $price = $this->get_price();
    if ( $price ){
        $price       = apply_filters( 'formatted_woocommerce_price', number_format( $price, wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator() ), $price, wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator() );
    }
    $text = sprintf(__( 'Participate now for <span class="atct-price" data-price="%s" data-id="%d">%s</span>', 'wc_lottery' ), $price, $this->get_id(), $price ? wc_price($price) : __('free', 'woocommerce'));
    return apply_filters( 'woocommerce_product_single_add_to_cart_text',$text, $this );
}

How can I update the text in my functions.php file? I need to override the returned $text value.

2

Answers


  1. Update: Put bellow code to your theme’s functions.php file:

    function single_add_to_cart_text() {
        global $product;
        $price = $product->get_price();
        if ( $price ){
            $price       = apply_filters( 'formatted_woocommerce_price', number_format(  $price, wc_get_price_decimals(), wc_get_price_decimal_separator(),  wc_get_price_thousand_separator() ), $price,  wc_get_price_decimals(), wc_get_price_decimal_separator(), wc_get_price_thousand_separator() );
        }
        $text = sprintf(__( 'Participate now for <span class="atct-price" data-price="%s"  data-id="%d">%s</span>', 'wc_lottery' ), $price, $product->get_id(), $price ?  wc_price($price) : __('free', 'woocommerce'));
        return $text;
    }
    
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'single_add_to_cart_text');
    
    Login or Signup to reply.
  2. Please use following snippet on your functions.php

    add_filter( 'woocommerce_product_single_add_to_cart_text', 'update_single_cart_text', 10, 1 );
    function update_single_cart_text( $cart_text ) {
        $cart_text = 'your custom text here';
        return $cart_text ;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search