1 year ago

#76821

test-img

Loris GFT

Conditional display of css container

EDIT : I use return true; in the first function and if(fonction1() === true) in the second. However it displays me the boolean value at the front-end, namely "1". Any idea how not to display this?


I'm trying to do this with this code:

Here is my code with my try but it doesn't work (the "if" "else" should just allow to change the associated css class)

// 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 class="ct-woo-card-extra new-badge">' . 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 class="ct-woo-card-extra new-badge solobadge">' . esc_html__( 'NEW', 'woocommerce' ) . '</span>';
   }
}
}

php

css

woocommerce

0 Answers

Your Answer

Accepted video resources