• Resolved sosoluis

    (@sosoluis)


    I need to change the text shown in the button “add to cart” to explicit inform to costumer that pressing it will backorder the product that only can allow it.
    I created this code, but nothing changes. If I put add_filter out, it affects to everything, and I don’t want to specify with a function every type of purchase or free downloadable and free stuff I offer. I only need to change this condition.
    Thank you in advance!

    add_action( 'woocommerce_after_add_to_cart_form', 'custom_woocommerce_after_add_to_cart_form' );
    
    function custom_woocommerce_after_add_to_cart_form() {
    	global $product;
    	if ( $product->backorders_allowed() ) {
    		add_filter( 'woocommerce_product_single_add_to_cart_text', 'texto_boton_reserva' );
    		?>
    		PRODUCTO AGOTADO. PUEDES RESERVARLO Y TE LO ENVIAREMOS TAN PRONTO LLEGUE A NUESTROS ALMACENES.
    		<?php
    	}
    }
    
    function texto_boton_reserva(){
    return __('Reservar','woocommerce');
    }
    
Viewing 7 replies - 1 through 7 (of 7 total)
  • I’m thinking that at the “after_add_to_cart” moment, its is too late to change the button text – its already been done.

    Try having the ‘woocommerce_product_single_add_to_cart_text’ on its own, then inside texto_boton_reserva() have the $product->backorders_allowed() check.

    I can see you’ll still need the after_add_to_cart to output the message.

    Thread Starter sosoluis

    (@sosoluis)

    As I said previously, if I leave alone the button text filter, I would have to specify all and every type of button that can appear (add to cart, download… etc.). I don’t want to recreate every text in a sentence, I only need to control conditionally the case of backorder allowed, so the filter needs to go into a condition inside a function (someone can correct me if I mistake).

    I have created another function that executes at initializing (basically with the same sentences in the “alert” text function), but does nothing (again):

    add_action( 'init', 'cambiar_compra_reserva' );
    function cambiar_compra_reserva() {
    	global $product;
    	if ( $product->backorders_allowed() ) {
    		add_filter( 'woocommerce_product_single_add_to_cart_text', 'texto_boton_reserva' );
    	}
    }
    
    function texto_boton_reserva(){
    return __('Reservar','woocommerce');
    }
    
    add_action( 'woocommerce_after_add_to_cart_form', 'custom_woocommerce_after_add_to_cart_form' );
    
    //Alert backorder
    function custom_woocommerce_after_add_to_cart_form() {
    	global $product;
    	if ( $product->backorders_allowed() ) {
    		add_filter( 'woocommerce_product_single_add_to_cart_text', 'texto_boton_reserva' );
    		?>
    		PRODUCTO AGOTADO. PUEDES RESERVARLO Y TE LO ENVIAREMOS TAN PRONTO LLEGUE A NUESTROS ALMACENES.
    		<?php
    	}
    }
    

    The filter is for the single add-to-cart button only. I haven’t seen a filter inside an action before.

    I got this to work:

      add_filter( 'woocommerce_product_single_add_to_cart_text', 'texto_boton_reserva', 20, 2 );
      function texto_boton_reserva( $text, $product ){
        if ( !intval( $product->stock ) ) {
          if ( $product->backorders_allowed() ) {
            $text = __( 'Reservar', 'woocommerce' );
          }
        }
        return $text;
      }
    
      add_action( 'woocommerce_after_add_to_cart_form', 'custom_woocommerce_after_add_to_cart_form' );
      // alert backorder
      function custom_woocommerce_after_add_to_cart_form() {
        global $product;
        if ( !intval( $product->stock ) ) {
          if ( $product->backorders_allowed() ) {
            print 'PRODUCTO AGOTADO. PUEDES RESERVARLO Y TE LO ENVIAREMOS TAN PRONTO LLEGUE A NUESTROS ALMACENES.';
          }
        }
      }
    
    Thread Starter sosoluis

    (@sosoluis)

    that code causes my site crashes to blank

    I checked it again here and it validates:
    https://phpcodechecker.com/
    So it may work or not, but either way it should not crash your site to blank.

    Make sure you have not altered and straight quotes to smart quotes along the way. Smart (sometimes called angled) quotes will crash.

    If this code is the first snippet in functions.php, you will need a “<?php” on the first line.

    Copy and paste the whole of the file where you put this into the above validator.

    Thread Starter sosoluis

    (@sosoluis)

    If I change it to this, I have no message, only button text:

    add_filter( 'woocommerce_product_single_add_to_cart_text', 'texto_boton_reserva', 20, 2 );
      function texto_boton_reserva( $text, $product ){
        if ( $product->backorders_allowed() ) {
        	$text = __( 'Agotado: Reservar', 'woocommerce' );
        }
        return $text;
      }
    
    add_action( 'woocommerce_after_add_to_cart_form', 'custom_woocommerce_after_add_to_cart_form' );
    
    // alert backorder
    function custom_woocommerce_after_add_to_cart_form() {
        global $product;
        if ( $product->backorders_allowed() ) {
            ECHO 'OPCIóN DE PRODUCTO AGOTADA. <br />Puedes reservarlo y te lo enviaremos en cuanto llegue a nuestros almacenes.<br />';
        }
    }
    • This reply was modified 7 years, 12 months ago by sosoluis.
    • This reply was modified 7 years, 12 months ago by sosoluis. Reason: bad code insertion
    Thread Starter sosoluis

    (@sosoluis)

    Ah, not! Finally is runnig!

    Thank you to appoint me to the right direction (I need to know better the filters options).

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Changing add to cart button text if the product is backorders allowed’ is closed to new replies.