• Resolved Elettromeccanica 2000

    (@elettromeccanica2000)


    Hi, I would like to know if you someone can help me regarding this code snippet for woocommerce stock translation, because here in italy the number 0 and the numbers <2 are plural, instead the 1 is singular so when there is only 1 left in stock will be “disponibile”, when there are 0 left or <2 will be “disponibili”. The major difference is the last letter change when is singular in “e” and when is plural “i”. Said this, here the code:
    `add_filter( ‘woocommerce_get_availability’, ‘custom_get_availability’, 1, 2);

    function custom_get_availability( $availability, $_product ) {

    global $product;

    $stock = $product->get_total_stock();

    if($stock < 2):

    $availability[‘availability’] = __(‘Solo ‘ . $stock . ‘ disponibile’, ‘toolsjet’);

    endif;

    return $availability;

    }

    In this case what I can do for being the “if stock 0 and <2” is possible doing in some way?

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hi @elettromeccanica2000,

    If I understand correctly, you are attempting to change the availability message so it uses the word “disponibili” when there are zero or more than two items left, and “disponibile” when there is only one left?

    If so, this code should achieve that:

    add_filter( 'woocommerce_get_availability', function ( $availability ) {
    	global $product;
    	$stock = $product->get_total_stock();
    
    	$message = _n( 'Solo %d disponibile', 'Solo %d disponibili', $stock, 'toolsjet' );
    	$availability['availability'] = sprintf( $message, $stock );
    
    	return $availability;
    
    }, 1 );
Viewing 1 replies (of 1 total)
  • The topic ‘How can I add the “and” or “&” in the snippet?’ is closed to new replies.