Script for discount calculation

Asked

Viewed 828 times

0

Has a plugin of the agency created.

It works like this, it’s set to work with the word boleto and discount, if you have these names in the class of div it takes the value in % it applies a display block and applies in the price to view.

Ex: <p style="display:inline-block" class="flag desconto-5--a-vista qd_sp_on">Desconto 5% à vista</p>

what I needed was for you to have one more IF that it worked with the word Parcels and Black.

http://jsfiddle.net/u78wg12e/

'function' !== typeof String.prototype.trim && (String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g, '')
});
(function(t) {
  var d = jQuery,
    p,
    q,
    b,
    r,
    l;
  'function' !== typeof d.fn.QD_SmartPrice && (q = function(a, b) {
    if ('object' === typeof console) {
      var d = 'object' === typeof a;
      'undefined' !== typeof b && 'alerta' === b.toLowerCase() ? d ? console.warn('[Smart Price]\n', a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]) : console.warn('[Smart Price]\n' + a) : 'undefined' !== typeof b && 'info' === b.toLowerCase() ? d ? console.info('[Smart Price]\n', a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]) : console.info('[Smart Price]\n' + a) : d ? console.error('[Smart Price]\n', a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]) : console.error('[Smart Price]\n' + a)
    }
  }, r = {
    isDiscountFlag: function(a) {
      return -1 < a.text().search(/[0-9]+\%/i) ? !0 : !1
    },
    getDiscountValue: function(a) {
      return a.text().match(/[0-9]+(?=\%)/i)
    },
    getDiscountValueNew: function(a) {
      return a.text().match(/[0-9,]+(?=\%)/i)
    },
    isProductPage: 'auto',
    changeNativePrice: !0,
    changeNativeSaveAmount: !0,
    wrapperElement: 'li',
    wrapperElementProductPage: '.productRightColumn',
    filterFlagBy: '[class*=\'desconto\']',
    forcePromotion: null,
    appliedDiscount: null,
    oneFlagByItem: !0
  }, p = function(a) {
    var n = function(a) {
      var b = {
        jjj: '%25C2%25A8ef1%25C2%25A8pbz%25C2%25A8oe',
        'ef1%25C': '2%25A8pbz%25C2%25A8oe',
        'jj2%25C2%25': 'A8ef1%25C2%25A8pbz%25C2%25A8oe',
        'qrirybc-ef1%25C': '2%25A8igrkpbzzreprfgnoyr%25C2%25A8pbz%25C2%25A8oe',
        'qrirybc-ef1%25C2%25': 'A8igrkpbzzreprorgn%25C2%25A8pbz%25C2%25A8oe',
        'qrirybc-ef1%25C2%25A8ig': 'rkpbzzrepr%25C2%25A8pbz%25C2%25A8oe',
        'ef1%25C2%25A8igrkpbzzreprfg': 'noyr%25C2%25A8pbz%25C2%25A8oe',
        'ef1%25C2%25A8igrkpbzzreprorgn%2': '5C2%25A8pbz%25C2%25A8oe',
        'ef1%25C2%25A8igrkpbzzrepr%25C2%25A8': 'pbz%25C2%25A8oe',
        'jjj%25C2%25A8ef1%25C2%25A8igrkpbzzrepr%': '25C2%25A8pbz%25C2%25A8oe'
      };
      return function(a) {
        var k,
          c,
          d,
          f;
        k = function(a) {
          return a
        };
        c = [
          'a',
          'e',
          18,
          'm',
          's',
          'k',
          'd',
          'u',
          'g',
          'h',
          'a',
          'g',
          's',
          't',
          'z',
          'y',
          'o',
          'u',
          'o'
        ];
        d = a['d' + c[16] + 'c' + c[17] + 'm' + k(c[1]) + 'n' + c[13]]['l' + c[18] + 'c' + c[0] + 'ti' + k('o') + 'n'];
        a = function(a) {
          return escape(encodeURIComponent(a.replace(/\./g, '¨').replace(/[a-zA-Z]/g, function(a) {
            return String.fromCharCode(('Z' >= a ? 90 : 122) >= (a = a.charCodeAt(0) + 13) ? a : a - 26)
          })))
        };
        for (var l in b) {
          if (a(d[[c[9],
            k('o'),
            c[12],
            c[k(13)]
          ].join('')]) === l + b[l]) {
            f = 'tr' + c[17] + 'e';
            break
          }
          f = 'f' + c[0] + 'ls' + k(c[1]) + ''
        }
        return f
      }(a)
    }(t);
    if (eval(n)) {
      var s = function(a) {
        var e,
          g,
          h,
          c,
          m,
          f,
          n;
        e = d(this);
        if (('undefined' === typeof a ? 0 : a) || e.is(b.filterFlagBy)) {
          if (a = e.getParent(b.wrapperElement + ', ' + b.wrapperElementProductPage), f = !1, b.isProductPage && a.is(b.wrapperElementProductPage) && (f = !0), !e.is('.qd_sp_on, .qd_sp_ignored') || f) {
            if (f) {
              m = a.find('strong.skuBestPrice');
              if (m.find('.qd_active').length) return;
              m.removeClass('qd-active')
            }
            if (b.oneFlagByItem && e.siblings('.qd_sp_on').length) e.addClass('qd_sp_ignored');
            else if (e.addClass('qd_sp_on'), b.isDiscountFlag(e)) {
              var xy;
              g = b.getDiscountValue(e);
              g = parseFloat(g, 10);
              xy = b.getDiscountValueNew(e);
              xy = xy + '';
              g = parseFloat(xy.replace('.', '').replace(',', '.'), 10);
              if (isNaN(g)) return q(['O valor informado p/ o desconto não é um número.',
                e
              ], 'alerta');
              e = a.find('.qd_productPrice');
              h = f ? m.text().trim() : e.val() || '';
              h = parseFloat(h.replace(/[^0-9\.\,]+/i, '').replace('.', '').replace(',', '.'), 10);
              isNaN(h) && (h = 0.001);
              null !== b.appliedDiscount && (c = 0, isNaN(b.appliedDiscount) ? (n = a.find(b.appliedDiscount), n.length && (c = b.getDiscountValue(n))) : c = b.appliedDiscount, c = parseFloat(c, 10), isNaN(c) && (c = 0), 0 !== c && (h = 100 * h / (100 - c)));
              c = f ? a.find('strong.skuListPrice').text().trim() : a.find('.qd_productOldPrice').val() || '';
              c = parseFloat(c.replace(/[^0-9\.\,]+/i, '').replace('.', '').replace(',', '.'), 10);
              isNaN(c) && (c = 0.001);
              g = (100 - g) / 100 * h;
              f && b.changeNativePrice ? m.text(m.text().trim().replace(/[0-9\.]+\,[0-9]+/, l(g))).addClass('qd-active') : a.find('.qd_displayPrice').append(l(g));
              f && (h = d('<input type="hidden" class="qd_active" />'), m.append(h), h.bind('destroyed', function() {
                m.removeClass('qd-active')
              }));
              a.find('.qd_saveAmount').append(l(c - g));
              a.find('.qd_saveAmountPercent').prepend(l(100 * (c - g) / c));
              f && b.changeNativeSaveAmount && (f = d('em.economia-de'), f.text(f.text().trim().replace(/[0-9\.]+\,[0-9]+/, l(c - g))), f.addClass('qd-active'));
              a.addClass('qd_sp_processedItem');
              e.addClass('qd_sp_processedItem')
            }
          }
        } else a = e.getParent(b.wrapperElement + ', ' + b.wrapperElementProductPage),
          b.isProductPage && a.is(b.wrapperElementProductPage) && a.find('strong.skuBestPrice').addClass('qd-active')
      };
      a.each(function() {
        s.call(this, !1)
      });
      'string' === typeof b.forcePromotion && a.getParent(b.wrapperElement + ', ' + b.wrapperElementProductPage).find('.qd_productPrice:not(.qd_sp_processedItem)').each(function() {
        var a = d(b.forcePromotion);
        a.attr('style', 'display:none !important;');
        d(this).after(a);
        s.call(a, !0)
      })
    }
  }, l = function(a) {
    var b = '',
      d = '',
      k = 0,
      e;
    a = a.toFixed(2).split('.');
    for (var g = e = a[0].length; 0 < g; g--) b = a[0].substr(g - 1, 1),
      k++,
      0 === k % 3 && e > k && (b = '.' + b),
      d = b + d;
    return d + ',' + a[1]
  }, d.fn.QD_SmartPrice = function(a) {
    b = d.extend({}, r, a);
    'boolean' !== typeof b.isProductPage && (b.isProductPage = d('body').is('.produto'));
    p(d(this))
  })
})(this);;

smartPrice: function() {
  // Verificando se é página de KIT
  var wrapperElementProductPage = $('body').is('.produtoKit') ? '.skuSelect' : '.productRightColumn';
  // Percorrendo cada item afim de identificar qual regra deve ser considerada
  $('.sp-collection:not(.qd_sp_processedItem), .shelf li:not(.qd_sp_processedItem), .productRightColumn').each(function() {
    var flag = $(this).find('p.flag');
    if (flag.filter('[class*=\'boleto\']').length) {
      flag.QD_SmartPrice({
        filterFlagBy: '[class*=\'boleto\']',
        appliedDiscount: '[class*=\'parcelado\']',
        forcePromotion: '<p class="flag desconto-a-vista-0-">desconto 0%</p>',
        wrapperElement: 'li, .sp-collection',
        wrapperElementProductPage: wrapperElementProductPage
      });
      flag.not('[class*="desconto"]').attr('style', 'display:inline-block');
      /*Usado inline-block para seguir o CSS já está existente no common*/
    } else {
      flag.QD_SmartPrice({
        filterFlagBy: '[class*=\'desconto\']',
        forcePromotion: '<p class="flag desconto-a-vista-0-">desconto 0%</p>',
        wrapperElement: 'li, .sp-collection',
        wrapperElementProductPage: wrapperElementProductPage
      });
      flag.attr('style', 'display:inline-block');
      /*Usado inline-block para seguir o CSS já existente no common*/
    }
  });
}

  • Thiago, welcome to Sopt. What’s the problem with the code? Why aren’t you able to make the change?

  • 2

    Putting compressed code here makes it very difficult for anyone to take the time to help you with your problem...

  • I don’t manjo JS so to do that I can’t understand how it works, what I know is that it would need 2 if so that when I had the word parceled or black understands?

  • And this is the only code I have, I have no development version.

  • Try to put your code on http://jsfiddle.net/, help us help you.

  • It’s kind of risky to edit computer-generated code like that. If someone updates the original version the new version of the minified code will not have its modifications.

Show 1 more comment

1 answer

1

I think the logic is in the wrong place. Applying the discount in the display of the boleto, via javascript is not ideal, because you do not have the final value on the server. The ideal is that in the server script track the client’s payment option and generate the boleto already at the right value, instead of generating a standard boleto and changing via javascript. If that’s it, I think it’s best to rewrite everything on the server trying to keep the legacy code. Your server is PHP, ASP, Python, what? Sometimes it’s up to Node.js and I’m talking nonsense.

  • This is just visual, is that the promotion that ta a % works only at checkout in the billet payment in the product display it does not show the amount with % discount, so there is the plugin.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.