-1

I'm trying to display custom availability text for products by 4 rules in two languages.

I set this function but I get both availability text (from english and greek). I want to display the specific message for each language.

//Availability Text for products 
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
   global $product;
    $defined_shipping_class = "Κατόπιν Παραγγελίας 7-15 ημέρες";
    $defined_shipping_class_en = "Available 7-15 days";

 $term = get_term_by( 'slug', $product->get_shipping_class(), 'product_shipping_class' );

 //Availability for greek language

 if(ICL_LANGUAGE_CODE=='gr');{

     //message if has shipping class
     if( is_a($term, 'WP_Term') && $term->name == $defined_shipping_class ){
        echo '<p class="product-shipping-class">' . $term->name . '</p>';
    }
     //message if is low stock
    elseif ( $_product->is_in_stock() && $product->get_stock_quantity() >= 1 && $product->get_stock_quantity() <= 10 ) {
    echo 'Διαθεσιμότητα: ' . $product->get_stock_quantity() . ' τεμ.'; 
    }
     // message if is  out of stock 
    elseif ( $_product->is_in_stock() && $product->get_stock_quantity() <1  ) {
    echo "<p><a href='https://...../επικοινωνια/' style='background-color:#e1e2e2; padding:5px 15px;color:#ed1c24;'>Αναμένεται. Επικοινωνήστε μαζί μας.</a></p>" ;
    }
     // message if is in stock
    elseif( $_product->is_in_stock() && $product->get_stock_quantity() > 11  ) {
       echo  'Σε απόθεμα' . $product->get_stock_quantity() . ' τεμ.'; 
    }

 }

 //Availability for english language
if(ICL_LANGUAGE_CODE=='en'); {

    //message if has shipping class
    if( is_a($term, 'WP_Term') && $term->name == $defined_shipping_class_en ){
        echo '<p class="product-shipping-class">' . $term->name . '</p>';
    }
    //message if is low stock
    elseif ( $_product->is_in_stock() && $product->get_stock_quantity() >= 1 && $product->get_stock_quantity() <= 10 ) {
    echo 'Αvailability: ' . $product->get_stock_quantity() . ' pcs.'; 
    }
    // message if is  out of stock 
    elseif ( $_product->is_in_stock() && $product->get_stock_quantity() <1  ) {
    echo "<p><a href='https://....../επικοινωνια/' style='background-color:#e1e2e2; padding:5px 15px;color:#ed1c24;'>Expected.Please contact us.</a></p>" ;
    }
    // message if is in stock
    elseif( $_product->is_in_stock() && $product->get_stock_quantity() > 11  ) {
       echo  'In stock' . $product->get_stock_quantity() . ' pcs.'; 
    }

 }
}

I want to display the specific message for each language. This code prints the text in both languages together. When I put elseif for english language it brokes the site. I can't figure what am I doing wrong... Thank you in advance for helping!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Helen_zs
  • 75
  • 2
  • 10
  • I don't get this - you want to print text in both languages, this code does that as you state. So what exactly is going wrong here? – Nico Haase Apr 15 '19 at 07:17
  • Hi, Thanks for your answer. This code prints both messages for each language. Ex. if a product is in stock it displays in greek language :"Διαθεσιμότητα¨2τεμ. Availability 2pcs." I want to display each message for each language . – Helen_zs Apr 15 '19 at 07:21
  • Then you should write code for **exactly that**: If you've come up with a solution for greek and english, why not go on for the other languages? – Nico Haase Apr 15 '19 at 07:29
  • But this code should print the message coresponding language. I don't get what am I doing wrong... – Helen_zs Apr 15 '19 at 07:38
  • What do you mean by "print the message coresponding language"? Can you explain further what goes wrong? You expect that code to output the message in both languages, it does do exactly that, but you're still confused by what? – Nico Haase Apr 15 '19 at 07:40
  • I expected this code to display messages according language. Ex. in greek it should display only "Διαθεσιμότητα 2 τεμ." and in english it should display only "Availability 2pcs.". Now it displays both messages together in both languages. – Helen_zs Apr 15 '19 at 07:44
  • Ah, now that makes sense – Nico Haase Apr 15 '19 at 08:05

2 Answers2

0

Have a look at your if statements: if you finish them with a semicolon, the following block is no longer connected to that statement, so it will be executed regardless of the condition.

To make it even clearer: the following two blocks produce the same output:

// First
if(ICL_LANGUAGE_CODE=='en'); {
    echo 'test';
}

// Second
echo 'test';

If you remove the semicolon in that if line, the first output is not shown if the condition does not fire

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
  • When ``` if(ICL_LANGUAGE_CODE=='gr');{......} if(ICL_LANGUAGE_CODE=='en') {....} ``` I get displayed the greek message on both languages. If I remove both semicolons it display right messages only in english and in greek laguage doesn;t display anything.. – Helen_zs Apr 15 '19 at 08:50
  • Have you checked what **exactly** that constant contains in case of greek language? Maybe the code is not given as `gr`? – Nico Haase Apr 15 '19 at 09:09
0

This solved the problem:

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
   global $product;
    $defined_shipping_class = "Κατόπιν Παραγγελίας 7-15 ημέρες";
    $defined_shipping_class_en = "Available 7-15 days";

 $term = get_term_by( 'slug', $product->get_shipping_class(), 'product_shipping_class' );

 //Availability for greek language
 if(ICL_LANGUAGE_CODE=='en') {

    //message if has shipping class
    if( is_a($term, 'WP_Term') && $term->name == $defined_shipping_class_en ){
        echo '<p class="product-shipping-class">' . $term->name . '</p>';
    }
    //message if is low stock
    elseif ( $_product->is_in_stock() && $product->get_stock_quantity() >= 1 && $product->get_stock_quantity() <= 10 ) {
    echo 'Αvailability: ' . $product->get_stock_quantity() . ' pcs.'; 
    }
    // message if is  out of stock 
    elseif ( $_product->is_in_stock() && $product->get_stock_quantity() <1  ) {
    echo "<p><a href='https://....../επικοινωνια/' style='background-color:#e1e2e2; padding:5px 15px;color:#ed1c24;'>Expected.Please contact us.</a></p>" ;
    }
    // message if is in stock
    elseif( $_product->is_in_stock() && $product->get_stock_quantity() > 11  ) {
       echo  'In stock' . $product->get_stock_quantity() . ' pcs.'; 
    }

 }


 //Availability for english language
elseif(ICL_LANGUAGE_CODE=='en') {

    //message if has shipping class
    if( is_a($term, 'WP_Term') && $term->name == $defined_shipping_class_en ){
        echo '<p class="product-shipping-class">' . $term->name . '</p>';
    }
    //message if is low stock
    elseif ( $_product->is_in_stock() && $product->get_stock_quantity() >= 1 && $product->get_stock_quantity() <= 10 ) {
    echo 'Αvailability: ' . $product->get_stock_quantity() . ' pcs.'; 
    }
    // message if is  out of stock 
    elseif ( $_product->is_in_stock() && $product->get_stock_quantity() <1  ) {
    echo "<p><a href='https://....../επικοινωνια/' style='background-color:#e1e2e2; padding:5px 15px;color:#ed1c24;'>Expected.Please contact us.</a></p>" ;
    }
    // message if is in stock
    elseif( $_product->is_in_stock() && $product->get_stock_quantity() > 11  ) {
       echo  'In stock' . $product->get_stock_quantity() . ' pcs.'; 
    }
}
    else{

     //message if has shipping class
     if( is_a($term, 'WP_Term') && $term->name == $defined_shipping_class ){
        echo '<p class="product-shipping-class">' . $term->name . '</p>';
    }
     //message if is low stock
    elseif ( $_product->is_in_stock() && $product->get_stock_quantity() >= 1 && $product->get_stock_quantity() <= 10 ) {
    echo 'Διαθεσιμότητα: ' . $product->get_stock_quantity() . ' τεμ.'; 
    }
     // message if is  out of stock 
    elseif ( $_product->is_in_stock() && $product->get_stock_quantity() <1  ) {
    echo "<p><a href='https://...../επικοινωνια/' style='background-color:#e1e2e2; padding:5px 15px;color:#ed1c24;'>Αναμένεται. Επικοινωνήστε μαζί μας.</a></p>" ;
    }
     // message if is in stock
    elseif( $_product->is_in_stock() && $product->get_stock_quantity() > 11  ) {
       echo  'Σε απόθεμα' . $product->get_stock_quantity() . ' τεμ.'; 
    }

 }


}
Helen_zs
  • 75
  • 2
  • 10