Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter yuss246

    (@yuss246)

    i added this code to display the correct price conversion

    Thread Starter yuss246

    (@yuss246)

    /**

    • Show only the lowest variation price (no “min–max” range) for variable products.
      */
      add_filter( ‘woocommerce_variable_price_html’, ‘gcb_single_variable_price’, 10, 2 );
      function gcb_single_variable_price( $price_html, $product ) {
      // Get all variation prices (including sales)
      $prices = $product->get_variation_prices( true );
      if ( empty( $prices[‘price’] ) ) {
      return $price_html;
      } // Grab the lowest regular price
      $min_price = current( $prices[‘price’] ); // If any variation is on sale, show the lowest sale price instead
      if ( ! empty( $prices[‘sale_price’] ) ) {
      $sale_min = min( array_filter( $prices[‘sale_price’], ‘strlen’ ) );
      if ( $sale_min > 0 && $sale_min < $min_price ) {
      $min_price = $sale_min;
      }
      } return wc_price( $min_price );
      }

    /**

    • Do the same for the “on sale” price HTML.
      */
      add_filter( ‘woocommerce_variable_sale_price_html’, ‘gcb_single_variable_sale_price’, 10, 2 );
      function gcb_single_variable_sale_price( $price_html, $product ) {
      $prices = $product->get_variation_prices( true );
      if ( empty( $prices[‘sale_price’] ) ) {
      return $price_html;
      } // Lowest sale price
      $min_sale_price = min( array_filter( $prices[‘sale_price’], ‘strlen’ ) );
      return wc_price( $min_sale_price );
      }
      // ──────────────────────────────────────────────────────────────────────────
      // DISPLAY RAW PRODUCT PRICE IN CART & MINI-CART
      // ──────────────────────────────────────────────────────────────────────────

    // 1) Cart “Price” column
    add_filter( ‘woocommerce_cart_item_price’, ‘gcb_cart_raw_price’, 20, 3 );
    // 1b) Mini-cart “Price”
    add_filter( ‘woocommerce_widget_cart_item_price’, ‘gcb_cart_raw_price’, 20, 3 );
    function gcb_cart_raw_price( $price_html, $cart_item, $cart_item_key ) {
    $product = $cart_item[‘data’];
    if ( $product instanceof WC_Product ) {
    // grab the raw value (sale if on-sale, otherwise regular)
    $raw_price = $product->is_on_sale()
    ? $product->get_sale_price()
    : $product->get_regular_price();

        // fallback to get_price() if needed
        if ( '' === $raw_price ) {
            $raw_price = $product->get_price();
        }
    
        // re-render with wc_price() (lets WooCommerce/Multi-Currency plugin do its thing)
        return wc_price( floatval( $raw_price ) );
    }
    return $price_html;

    }

    // 2) Cart “Subtotal” column
    add_filter( ‘woocommerce_cart_item_subtotal’, ‘gcb_cart_raw_subtotal’, 20, 3 );
    // 2b) Mini-cart “Subtotal”
    add_filter( ‘woocommerce_widget_cart_item_subtotal’, ‘gcb_cart_raw_subtotal’, 20, 3 );
    function gcb_cart_raw_subtotal( $subtotal_html, $cart_item, $cart_item_key ) {
    $product = $cart_item[‘data’];
    if ( $product instanceof WC_Product ) {
    $qty = $cart_item[‘quantity’];

        $raw_unit = $product->is_on_sale()
    ? $product->get_sale_price()
    : $product->get_regular_price();

    if ( '' === $raw_unit ) {
    $raw_unit = $product->get_price();
    }

    // calculate line total
    $line_total = floatval( $raw_unit ) * $qty;

    return wc_price( $line_total );
    }
    return $subtotal_html;

    }

    i added this code to display the correct price conversion
Viewing 2 replies - 1 through 2 (of 2 total)