Please submit a private ticket if you need to share sensitive information, such as license details and admin credentials.

Okay
  Public Ticket #4496749
Incorrect total price when selecting "replace by total"
Open

Comments

  • Simon started the conversation

    Bug title

    Product page shows the wrong total price when a variation doesn’t use volume pricing

    Summary

    When “Change price → Replace by total” is enabled in the WPC Price by Quantity (Premium) plugin, the product page should show the total price (unit price × selected quantity). This works for variations that use volume pricing, but if I select a variation without volume pricing, the total is wrong: it either shows the product’s price range or keeps showing the last total from a different variation.

    Case 1 – Mixed variations (some with volume pricing, some without)

    1. Create a variable product.

    Add at least one variation with volume pricing.

    Add another variation without volume pricing.

    2. Enable plugin setting: Change price → Replace by total.

    3. On the product page:

    Select the variation with volume pricing → the total updates correctly.

    Switch to the variation without volume pricing.

    Change the quantity (e.g., to 3).

    Case 2 – All variations without volume pricing

    1. Create a variable product where none of the variations use volume pricing.

    2. Enable plugin setting: Change price → Replace by total.

    3. On the product page:

    Select any variation.

    Increase the quantity (e.g., to 3).

    Expected result

    The price shown on the product page updates to the correct total for the selected variation and quantity (e.g., if the selected variation costs €10 each and quantity is 3, the page should show €30).

    Actual result

    Case 1: When switching to a variation without volume pricing, the product page either:

    Shows the previous total from the last variation that had volume pricing, or

    Shows the variable product’s price range instead of the correct total.

    Case 2: When all variations have no volume pricing, the range of the pricing is displayed

    Scope
    • Affects variable products that mix variations with and without volume pricing.
    • Affects variable products without any volume pricing


  •  1,542
    Dustin replied

    Hi Simon,

    I tried it, and it works fine. Please watch the screencast video: https://app.screencast.com/Nznhn0jCs8uzu

    Please provide me with the product link that exhibits the problem as you described, so I can assist you in checking it.

    To expedite the process, please provide your website credentials (wp-admin link, username, and password) or create a development or staging site with identical configurations so that I can check for any issues.

    Best regards,
    Dustin

  • Simon replied

    Hi Dustin,

    Thanks for the swift reply. The bug I logged occurs when some variants use volume prices and some do not use any tier/volume pricing. In your screencast all the variants use volume/tier pricing. You can reproduce the bug I mentioned on your own environment by having one variant with volume pricing and one without volume pricing (case 1). For case two, I would expect the 'replace by total' function to apply to all products, even the ones that don't use volume/tier pricing

  • Simon replied

    This code fixes the issues. Can you incorporate with the plugin?

    Why this fixes the issues

    • “Shows the range instead of total on non-tiered variants” → the added else block writes the correct total when there’s no table, replacing the default range.
    • “If some variants have tiers and some don’t, it shows the amount of the variant that does have tiers” → previously, the old table’s total remained. Now, when the plugin clears the table for a non-tiered variation, we still recompute from t.display_price and push the correct total into the main price element.
    • “Always show total of selected quantity × selected variation price” → the new quantity listener ensures totals stay correct even after the initial variation selection.

    <script>/* WPC Price by Quantity – non-tiered variation total fallback
       Enqueue this after the plugin's frontend.js (depends on jQuery).
    */
    (function ($) {
      'use strict';

      // Guard: required globals from the plugin
      if (typeof wpcpq_vars === 'undefined' || typeof wpcpq_format_price === 'undefined') {
        return;
      }

      // Update main price when there's no WPCPQ table visible for the current variation
      function updateMainPriceNoTable($form) {
        var pid   = $form.data('product_id');
        var $wrap = $(document).find('.wpcpq-wrap-' + pid);

        // If a table is present/visible, let the plugin handle totals.
        if ($wrap.find('.wpcpq-table:visible').length) return;

        var qty  = parseFloat($form.find('[name="quantity"]').val() || 1);
        var unit = parseFloat($form.data('wpcpq_unit_display_price') || 0);
        var $main_price = $('.wpcpq-price-' + pid);

        if (!$main_price.length || !isFinite(unit) || unit <= 0) return;

        if (wpcpq_vars.main_price === 'total') {
          $main_price.html(wpcpq_format_price(qty * unit));
        } else if (wpcpq_vars.main_price === 'price') {
          $main_price.html(wpcpq_format_price(unit));
        }
      }

      // When a variation is found, cache its unit price and, if there's no table, set the main price.
      $(document).on('found_variation', '.variations_form', function (e, t) {
        var $form = $(this);
        var pid   = $form.data('product_id');
        var $wrap = $(document).find('.wpcpq-wrap-' + pid);

        // Cache the display unit price for this variation (used for qty changes too)
        $form.data('wpcpq_unit_display_price', parseFloat(t && t['display_price'] ? t['display_price'] : 0));

        // If the plugin removed the table for this variation (no tiers), still update the main price
        var $table = t && t['variation_id'] ? $('.wpcpq-table-' + t['variation_id']) : $();
        if (!$table.length) {
          updateMainPriceNoTable($form);
        }
      });

      // Keep totals in sync on quantity change when there's no table
      $(document).on('input change', '.variations_form [name="quantity"]', function () {
        updateMainPriceNoTable($(this).closest('.variations_form'));
      });

      // Also react when the plugin signals its own variation handling completed
      $(document).on('wpcpq_found_variation', function () {
        $('.variations_form').each(function () {
          updateMainPriceNoTable($(this));
        });
      });

      // If a variation is preselected on load, ensure the price reflects qty × unit (no-table case)
      $(function () {
        $('.variations_form').each(function () {
          var $form = $(this);
          // Woo will trigger found_variation shortly; this is a safety net for themes that don't.
          setTimeout(function () {
            updateMainPriceNoTable($form);
          }, 50);
        });
      });

    })(jQuery);
    </script>

  •  1,542
    Dustin replied

    Thanks for that helpful information.

    I checked again and found the issue as you described. We will resolve the issue and release an official update shortly.

    Best regards,
    Dustin