Advanced Dynamic Pricing For WooCommerce

WooCommerce Advanced Product Labels, by Jeroen Sormani

Add provided code to functions.php and update Settings.

Calculation option “Use prices modified by other plugins” must be ON.

System option “Suppress other pricing plugins in frontend” must be OFF.

remove_filter('wapl_product_label', 'wapl_smart_product_label_filter');
add_filter('wapl_product_label', function($label) {

  global $product;

  // This in here for the admin preview to select one random product
  if ( ! $product ) {
    $product_posts = get_posts( array( 'post_type' => 'product', 'posts_per_page' => 1 ) );
    $product       = reset( $product_posts );
  }

  $product            = wc_get_product( $product );
  $highest_percentage = 0;

  if ( ! $product ) { // Check to be sure the global $product is set properly
    trigger_error( 'The global $product is not a valid variable type: ' . gettype( $product ) );
    return $label;
  }

  if ( $product->is_type( 'composite' ) ) {

    $regular_price      = $product->get_composite_regular_price();
    $sale_price         = $product->get_composite_price();
    $highest_percentage = ( $sale_price !== '' && $regular_price != 0 ) ? ( ( $regular_price - $sale_price ) / $regular_price * 100 ) : $highest_percentage;

  } elseif ( ! $product->is_type( 'variable' ) ) {

    $regular_price      = $product->get_regular_price();
    $sale_price         = $product->get_sale_price();
    $highest_percentage = ( $sale_price !== '' && $regular_price != 0 ) ? ( ( $regular_price - $sale_price ) / $regular_price * 100 ) : $highest_percentage;

  } else { // Get the right variable percentage

    foreach ( $product->get_children() as $child_id ) {
      $child = wc_get_product($child_id);

      $price = $child->get_regular_price();
      $sale  = $child->get_sale_price();

      $percentage = $price != 0 && ! empty( $sale ) ? ( ( $price - $sale ) / $price * 100 ) : $highest_percentage;

      if ( $percentage >= $highest_percentage ) {
        $highest_percentage = $percentage;
        $regular_price      = $product->get_variation_regular_price( 'min' );
        $sale_price         = $product->get_variation_sale_price( 'min' );
      }
    }
  }

  $label = str_replace( '{percentage}', round( $highest_percentage, apply_filters( 'wapl_filter_discount_round', 1 ) ) . '%', $label );
  $label = str_replace( '{discount}', wc_price( (float) $regular_price - (float) $sale_price ), $label );
  $label = str_replace( '{price}', wc_price( $regular_price ), $label );
  $label = str_replace( '{saleprice}', wc_price( $sale_price ), $label );
  $label = str_replace( '{delprice}', '<del>' . wc_price( $regular_price ) . '</del>', $label );

  return $label;
});

 

Have questions? Please submit a support request. We're always happy to help!

Advanced Dynamic Pricing
for WooCommerce
PRO

From $60
Buy
✓ 30-day money-back guarantee