Home / eCommerce / WWPP – Show retail reference price including tax for specific wholesale roles only
Duplicate Snippet

Embed Snippet on Your Site

WWPP – Show retail reference price including tax for specific wholesale roles only

Code Preview
php
<?php
  // Show retail reference price including tax for specific wholesale roles only
  // Requires: WWPP Settings > Tax > "Tax Display on Wholesale Price" = "Excluding Tax"
  add_filter( 'wwp_product_original_price', function( $price_html, $wholesale_price,
  $product_price, $product, $user_wholesale_role ) {
      $target_roles = array( 'wholesale_vip' ); // Replace this with your own custom wholesale role IDs
      if ( empty( $user_wholesale_role ) || ! ( $product instanceof WC_Product ) ) {
          return $price_html;
      }
      if ( empty( array_intersect( $user_wholesale_role, $target_roles ) ) ) {
          return $price_html;
      }
      $tax_label = WC()->countries->inc_tax_or_vat();
      if ( $product->is_type( 'variable' ) ) {
          $incl_tax_prices = array();
          foreach ( $product->get_children() as $child_id ) {
              $variation = wc_get_product( $child_id );
              if ( ! $variation || ! $variation->is_visible() ) {
                  continue;
              }
              $regular_price = $variation->get_regular_price();
              if ( '' === $regular_price ) {
                  continue;
              }
              $incl_tax_prices[] = (float) wc_get_price_including_tax( $variation, array(
  'price' => (float) $regular_price ) );
          }
          if ( empty( $incl_tax_prices ) ) {
              return $price_html;
          }
          $min       = min( $incl_tax_prices );
          $max       = max( $incl_tax_prices );
          $formatted = ( $min !== $max ) ? wc_format_price_range( $min, $max ) : wc_price(
  $min );
      } else {
          $regular_price = $product->get_regular_price();
          $price_incl_tax = (float) wc_get_price_including_tax( $product, array( 'price' =>
  (float) $regular_price ) );
          $formatted      = wc_price( $price_incl_tax );
      }
      return '<del class="original-computed-price">'
          . $formatted
          . ' <small class="woocommerce-price-suffix">' . esc_html( $tax_label ) . '</small>'
          . '</del>';
  }, 20, 5 );

Comments

Add a Comment