• Resolved eng0el

    (@eng0el)


    HEllo, can someone help me writing a php function to change the ‘Add to Cart’ text whenever the price is empty or equal to 0.
    I just want to change the label.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @eng0el

    The following snippet can help you change the add to cart button text: https://woocommerce.com/document/change-add-to-cart-button-text/

    We recommend using a plugin like Code Snippets to add the snippet to your site.

    change the ‘Add to Cart’ text whenever the price is empty or equal to 0

    However, this doesn’t seem to be realistic situation. What would cause a product’s price to become zero? Can you please elaborate a bit?

    Hi

    function wc_filter_woocommerce_product_add_to_cart_text( $add_to_cart_text, $product ) {
        // Price empty & Price is 0
        if ( empty ( $product->get_price() ) ||   $product->get_price()) == 0 ) {
            $add_to_cart_text = __( 'Not available right now', 'woocommerce' );
        }
    
        return $add_to_cart_text;
    }
    add_filter( 'woocommerce_product_add_to_cart_text', 'wc_filter_woocommerce_product_add_to_cart_text', 10, 2 );
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'wc_filter_woocommerce_product_add_to_cart_text', 10, 2 );

    try this. not tested but should work

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @make-carlos

    Your code produces a fatal on line 3.

    @eng0el

    Try the following:

    function wc_filter_woocommerce_product_add_to_cart_text( $add_to_cart_text, $product ) {
        // Price empty & Price is 0
        if ( 0 == $product->get_price() ) {
            $add_to_cart_text = __( 'Not available right now', 'woocommerce' );
        }
    
        return $add_to_cart_text;
    }
    add_filter( 'woocommerce_product_add_to_cart_text', 'wc_filter_woocommerce_product_add_to_cart_text', 10, 2 );
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'wc_filter_woocommerce_product_add_to_cart_text', 10, 2 );
    Thread Starter eng0el

    (@eng0el)

    @rynald0s
    I don’t know why nothing is changing in the product page, and the button is still displaying ‘Add to cart’

    Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @eng0el

    Where and how are you adding the code?

    I added the code snippet via the free https://www.ads-software.com/plugins/code-snippets/ plugin and the output was as follows:

    https://snipboard.io/tjRA4u.jpg

    Is the product price set to be 0 on your end? Also, is that a simple or a variable product? The latter may require a code adjustment.

    Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘PHP: Change Add to Cart text when price is empty or 0’ is closed to new replies.