Advertisement
borkolivic

WoodMart theme - sale label in percentage w/decimals

Apr 14th, 2020
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.27 KB | None | 0 0
  1. function woodmart_product_label() {
  2.    global $product;
  3.    $output = array();
  4.    $product_attributes = woodmart_get_product_attributes_label();
  5.    $percentage_label = woodmart_get_opt( 'percentage_label' );
  6.    if ( $product->is_on_sale() ) {
  7.       $percentage = '';
  8.       if ( $product->get_type() == 'variable' && $percentage_label ) {
  9.          $available_variations = $product->get_variation_prices();
  10.          $max_percentage = 0;
  11.          foreach( $available_variations['regular_price'] as $key => $regular_price ) {
  12.             $sale_price = $available_variations['sale_price'][$key];
  13.             if ( $sale_price < $regular_price ) {
  14.                $percentage = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
  15.                if ( $percentage > $max_percentage ) {
  16.                   $max_percentage = $percentage;
  17.                }
  18.             }
  19.          }
  20.          $percentage = $max_percentage;
  21.       } elseif ( ( $product->get_type() == 'simple' || $product->get_type() == 'external' ) && $percentage_label ) {
  22.          $percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100, 2 );
  23.       }
  24.       if ( $percentage ) {
  25.          $output[] = '<span class="onsale product-label">-' . $percentage . '%' . '</span>';
  26.       }else{
  27.          $output[] = '<span class="onsale product-label">' . esc_html__( 'Sale', 'woodmart' ) . '</span>';
  28.       }
  29.    }
  30.    if( !$product->is_in_stock() ){
  31.       $output[] = '<span class="out-of-stock product-label">' . esc_html__( 'Sold out', 'woodmart' ) . '</span>';
  32.    }
  33.    if ( $product->is_featured() && woodmart_get_opt( 'hot_label' ) ) {
  34.       $output[] = '<span class="featured product-label">' . esc_html__( 'Hot', 'woodmart' ) . '</span>';
  35.    }
  36.    if ( get_post_meta( get_the_ID(), '_woodmart_new_label', true ) && woodmart_get_opt( 'new_label' ) ) {
  37.       $output[] = '<span class="new product-label">' . esc_html__( 'New', 'woodmart' ) . '</span>';
  38.    }
  39.    if ( $product_attributes ) {
  40.       foreach ( $product_attributes as $attribute ) {
  41.          $output[] = $attribute;
  42.       }
  43.    }
  44.    if ( $output ) {
  45.       echo '<div class="product-labels labels-' . woodmart_get_opt( 'label_shape' ) . '">' . implode( '', $output ) . '</div>';
  46.    }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement