Home > database >  Problem to check if a function is true in another function php
Problem to check if a function is true in another function php

Time:02-12

I use this code to check the function 1 is true. If it is true, function 2 assigns a CSS class, otherwise another one :

function first() {
  return TRUE;
}

function second() {
  if (first() == TRUE) {
  }
}

The result works but I have a problem. It shows me the boolean value at the front end site, which is "1" (the value is in the html code, without any tag around it). Do you have any idea how to not display this value?

Here is the front-end rendering: enter image description here

The HTML code looks like this :

<a>
    <span >-50%</span>
    1
    <img>

And here is my full code:

// Sales badge
add_action( 'woocommerce_sale_flash', 'sale_badge_percentage', 25 );
 
function sale_badge_percentage() {
   global $product;
   if ( ! $product->is_on_sale() ) return;
   if ( $product->is_type( 'simple' ) ) {
      $max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
   } elseif ( $product->is_type( 'variable' ) ) {
      $max_percentage = 0;
      foreach ( $product->get_children() as $child_id ) {
         $variation = wc_get_product( $child_id );
         $price = $variation->get_regular_price();
         $sale = $variation->get_sale_price();
         if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
         if ( $percentage > $max_percentage ) {
            $max_percentage = $percentage;
         }
      }
   }
   if ( $max_percentage > 0 ) {
        echo "<span class='onsale'>-" . round($max_percentage) . "%</span>"; 
        return TRUE;
   }
}



// New badge for recent products
add_action( 'woocommerce_before_shop_loop_item_title', 'new_badge', 3 );
          
function new_badge() {
   if (sale_badge_percentage() == TRUE) {
    /* la variable existe */
       global $product;
   $newness_days = 30; // Number of days the badge is shown
   $created = strtotime( $product->get_date_created() );
   if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
      echo '<span >' . esc_html__( 'NEW', 'woocommerce' ) . '</span>';
   }
}
else {
    /* la variable n'existe pas */
    global $product;
   $newness_days = 30; // Number of days the badge is shown
   $created = strtotime( $product->get_date_created() );
   if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
      echo '<span >' . esc_html__( 'NEW', 'woocommerce' ) . '</span>';
   }
}
}

CodePudding user response:

Resolved!! i only changed echo in return above the return TRUE;

  • Related