How to display availability for products depending on shipping classes in two languages [duplicate]

19
April 16, 2019, at 10:30 AM

This question already has an answer here:

  • Woocommerce custom availability text shown multiple times when selecting variations 2 answers
  • Get language code of WPML 2 answers

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!

Answer 1

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

Answer 2

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() . ' τεμ.'; 
    }
 }

}
READ ALSO
Serverload using own postfix with mail() vs phpmailer with external smtp

Serverload using own postfix with mail() vs phpmailer with external smtp

I got a smaller VPS server at the moment ~20%-30% avg load 3 websitesClients need to send emails for order, registration, invoices and newsletter

37
ErrorException htmlspecialchars() expects parameter 1 to be string, array given

ErrorException htmlspecialchars() expects parameter 1 to be string, array given

I want to get data "report_to" from databaseBut i got error when I try to print variable in my view page

17
How to add Attachment field compulsory in WordPress Add to Cart Page [on hold]

How to add Attachment field compulsory in WordPress Add to Cart Page [on hold]

i have added File attachment field in my add to cart page but if someone did not upload their attachment they are still able to place their orderNow i want to make attachment field compulsory that no one place their order successfully without uploading...

32