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

Okay
  Public Ticket #3341943
Dynamic "Save Percentage" possible?
Closed

Comments

  •  3
    Rudolf started the conversation

    Hello,

    we have variations with different prices. Is there a way to update [wpcbm_save_percentage] or [wpcbm_save_amount] when selecting  a variation?

    Thanks!

  •  767
    Janilyn replied

    Hi Rudolf,

    Thank you for contacting WPClever Support Forum. 

    We’ve received your ticket and assigned one of our developers to help you with that. 

    He’ll be looking into your question and responding with specific instructions as soon as possible. 

    Please stay patient as we're having very heavy workloads, it might take some time for our developers to reach back.

    Best regards.

    Janilyn T. - WPClever Support Agent

  •  1,222
    Dustin replied

    Hi Rudolf,

    Yes, you can do that!

    First, please update our plugin to the latest version 2.2.0.

    Then add the below custom code (How to add custom code?)

    add_action( 'wp_footer', function () {
        ?>
        <script type="text/javascript">
          (function($) {
            $(document).on('found_variation', function(e, t) {
              var product_id = $(e['target']).attr('data-product_id');
              if ($('.wpcbm-pid-' + product_id).length) {
                $('.wpcbm-pid-' + product_id).each(function() {
                  if ($(this).hasClass('wpcbm-badge-3749')) {
                    // badge 3749 have the shortcode [wpcbm_saved_percentage]
                    var $this = $(this);
                    if ($this.attr('data-ori-content') == undefined) {
                      $this.attr('data-ori-content', $this.text());
                    }
                    if (parseFloat(t['display_price']) < parseFloat(t['display_regular_price'])) {
                      // calculate new saved percentage
                      var percentage = Math.round(
                          ((t['display_regular_price'] - t['display_price']) / t['display_regular_price']) * 100);
                      $this.text('Saved ' + percentage + '%');
                    } else {
                      $this.text($this.attr('data-ori-content'));
                    }
                  }
                });
              }
            });
            $(document).on('reset_data', function(e) {
              var product_id = $(e['target']).attr('data-product_id');
              $('.wpcbm-pid-' + product_id).each(function() {
                if ($(this).hasClass('wpcbm-badge-3749')) {
                  // badge 3749 have the shortcode [wpcbm_saved_percentage]
                  $(this).text($(this).attr('data-ori-content'));
                }
              });
            });
          })(jQuery);
        </script>
        <?php
    }, 99 );

    And the result (screen record video) https://www.screencast.com/t/zW1IkU9aQr

    You need to change the number '3749' in the above snippet to the badge ID on your site.

    Best regards,
    Dustin