-2

/**
 * AxonCreator - Website Builder
 *
 * NOTICE OF LICENSE
 *
 * @author    axonvip.com <support@axonvip.com>
 * @copyright 2021 axonvip.com
 * @license   You can not resell or redistribute this software.
 *
 * https://www.gnu.org/licenses/gpl-3.0.html
 */
var AxpsSearchModule;
!(function (d) {
    "use strict";
    AxpsSearchModule = {
        init: function () {
            this.mdCategoryDropdown(), this.mdSearch();
        },
        mdSearch: function () {
            d("body").on("shown.bs.modal", "#search-popup", function () {
                var e,
                    a,
                    i = d("#search-popup").find("input[name=s]").val();
                d("#search-popup").find("input[name=s]").val("").focus().val(i),
                    d(window).width() < 768 ||
                        ((a = (e = d("#search-popup").find(".category-dropdown-inner")).innerWidth() + e.closest("form").find(".search-submit").innerWidth() + 17),
                        (i = "padding-right"),
                        d("body").hasClass("rtl") && (i = "padding-left"),
                        e.closest("form").find("input[name=s]").css(i, a));
            });
            function a() {
                d("form.has-ajax-search").each(function () {
                    var a,
                        e,
                        i,
                        n,
                        r,
                        s,
                        t,
                        o,
                        c = d(this);
                    c.hasClass("fs-initialized") ||
                        (c.addClass("fs-initialized"),
                        (a = '<div class="view-all-results view-all-products"><span>' + opSearch.all_results_product + "</span></div>"),
                        (e = c.attr("action")),
                        (i = parseInt(opSearch.count)),
                        (n = c.find('[name="c"]')),
                        (r = "cart_default"),
                        (s = c.closest(".search-wrapper").find(".search-results")),
                        c.removeClass("has-ajax-search"),
                        void 0 !== opSearch.imageType && "" != opSearch.imageType && (r = opSearch.imageType),
                        0 != opSearch.search_string && c.find("input[name=s]").val(opSearch.search_string),
                        c.find("input[name=s]").click(function () {
                            3 <= c.find("input[name=s]").val().length && "" !== s.find(".autocomplete-suggestions").html() && s.find(".autocomplete-suggestions").css({ display: "flex" });
                        }),
                        s.on("click", ".view-all-products", function () {
                            c.submit();
                        }),
                        c.find("input[name=s]").devbridgeAutocomplete({
                            serviceUrl: e,
                            appendTo: s,
                            type: "POST",
                            dataType: "json",
                            paramName: "s",
                            minChars: 3,
                            params: { c: n.val(), resultsPerPage: i },
                            onSelect: function (e) {
                                void 0 !== e.permalink && 0 < e.permalink.length && (d("body").hasClass("product-id-" + e.id) ? location.reload() : (window.location.href = e.permalink));
                            },
                            onSearchStart: function (e) {
                                c.addClass("search-loading");
                            },
                            beforeRender: function (e) {
                                d(e).find(".search-divider-text").parent().addClass("search-divider"), d(e).find(".no-result").parent().addClass("search-no-result"), d(e).find(".view-all-products").parent().replaceWith(a);
                            },
                            onSearchComplete: function (e, a) {
                                c.removeClass("search-loading");
                            },
                            transformResult: function (e) {
                                var s = [];
                                return (
                                    s.push({ value: "", divider: opSearch.divider }),
                                    0 < e.products.length
                                        ? (e.products.forEach(function (e) {
                                              var a = (e.has_discount ? '<span class="regular-price">' + e.regular_price + "</span> " : "") + '<span class="price">' + e.price + "</span>",
                                                  i = "";
                                              e.cover && void 0 !== e.cover.bySize[r].url && (i = e.cover.bySize[r].url);
                                              var n = opSearch.sku + " " + (e.reference || "N/A");
                                              s.push({ value: e.name, permalink: e.url, price: a, thumbnail: '<img class="img-responsive" src="' + i + '" alt="" title="">', sku: n });
                                          }),
                                          s.push({ value: "", view_all_products: !0, permalink: "" }))
                                        : s.push({ value: opSearch.noProducts, no_found: !0, permalink: "" }),
                                    { suggestions: s }
                                );
                            },
                            formatResult: function (e, a) {
                                var i = "(" + (a = "&" === a ? "&#038;" : a).replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + ")",
                                    a = "";
                                return (
                                    e.divider && (a += ' <h5 class="search-divider-text">' + e.divider + "</h5>"),
                                    e.thumbnail && (a += ' <div class="search-thumb">' + e.thumbnail + "</div>"),
                                    e.value && (a += '<h4 class="search-title">' + e.value.replace(new RegExp(i, "gi"), "<strong>$1</strong>").replace(/&lt;(\/?strong)&gt;/g, "<$1>") + "</h4>"),
                                    e.no_found && (a = '<div class="no-result">' + e.value + "</div>"),
                                    e.view_all_products && (a = '<div class="view-all-products"></div>'),
                                    e.sku && (a += ' <div class="search-sku">' + e.sku + "</div>"),
                                    e.price && (a += ' <div class="search-price">' + e.price + "</div>"),
                                    a
                                );
                            },
                        }),
                        n.length &&
                            ((t = c.find("input[name=s]").devbridgeAutocomplete()),
                            (o = e),
                            n.on("cat_selected", function () {
                                t.setOptions({ serviceUrl: o, params: { c: n.val(), resultsPerPage: i } }), t.hide(), t.onValueChange();
                            })),
                        d("body").on("click", function (e) {
                            e = e.target;
                            d(e).is(".search-wrapper") || d(e).parents().is(".search-wrapper") || c.find("input[name=s]").devbridgeAutocomplete("hide");
                        }),
                        d(".search-results").on("click", function (e) {
                            e.stopPropagation();
                        }));
                });
            }
            a(),
                prestashop.on("updatedProductList", function (e) {
                    a();
                });
        },
        mdCategoryDropdown: function () {
            d(".category-dropdown-inner").each(function () {
                var i,
                    n,
                    s,
                    r = d(this);
                function t() {
                    r.removeClass("shown");
                }
                function o() {
                    var e, a;
                    d(window).width() < 768 ||
                        ((e = r.innerWidth() + r.closest("form").find(".search-submit").innerWidth() + 17), (a = "padding-right"), d("body").hasClass("rtl") && (a = "padding-left"), r.closest("form").find("input[name=s]").css(a, e));
                }
                r.hasClass("cd-initialized") ||
                    (r.addClass("cd-initialized"),
                    (i = r.find("> a")),
                    (n = r.find("> input")),
                    (s = r.find("> .list-wrapper")),
                    opSearch.current_category_id &&
                        (s.find(".active-item").removeClass("active-item"),
                        s
                            .find("a[data-value=" + opSearch.current_category_id + "]")
                            .parent()
                            .addClass("active-item"),
                        i.text(s.find("a[data-value=" + opSearch.current_category_id + "]").text()),
                        n.val(opSearch.current_category_id),
                        s.find("ul:not(.children) > li:first-child").show()),
                    o(),
                    d("body").on("click", function (e) {
                        e = e.target;
                        if (r.hasClass("shown") && !d(e).is(".category-dropdown-inner") && !d(e).parents().is(".category-dropdown-inner")) return t(), !1;
                    }),
                    i.on("click", function (e) {
                        return e.preventDefault(), r.hasClass("shown") ? t() : (r.addClass("shown"), void 0 !== d.fn.devbridgeAutocomplete && r.closest("form").find("input[name=s]").devbridgeAutocomplete("hide")), !1;
                    }),
                    s.on("click", "a", function (e) {
                        e.preventDefault();
                        var a = d(this).data("value"),
                            e = d(this).text();
                        s.find(".active-item").removeClass("active-item"),
                            d(this).parent().addClass("active-item"),
                            0 !== a ? s.find("ul:not(.children) > li:first-child").show() : 0 === a && s.find("ul:not(.children) > li:first-child").hide(),
                            i.text(e),
                            n.val(a).trigger("cat_selected"),
                            t(),
                            o();
                    }));
            });
        },
    };
})(jQuery),
    jQuery(document).ready(function () {
        AxpsSearchModule.init();
    });

In the below code the e.price is the price of a product, i want to know how i can put If conditional statement in this so that price with price 0 shall not be show(no price),and if amount is greater then 0 then it will be show. I tried if(e.price == 0) but i dont know how and in which line it should be written.

formatResult: function(e, a) {
  var i = "(" + (a = "&" === a ? "&#038;" : a).replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + ")",
    a = "";
  return (
    e.divider && (a += ' <h5 class="search-divider-text">' + e.divider + "</h5>"),
    e.thumbnail && (a += ' <div class="search-thumb">' + e.thumbnail + "</div>"),
    e.value && (a += '<h4 class="search-title">' + e.value.replace(new RegExp(i, "gi"), "<strong>$1</strong>").replace(/&lt;(\/?strong)&gt;/g, "<$1>") + "</h4>"),
    e.no_found && (a = '<div class="no-result">' + e.value + "</div>"),
    e.view_all_products && (a = '<div class="view-all-products"></div>'),
    e.sku && (a += ' <div class="search-sku">' + e.sku + "</div>"),
    e.price && (a += ' <div class="search-price">' + e.price + "</div>"),
    a
  );
},
  • `'
    ' + e.price + "
    "` is where the output happens. You can use a [conditional operator](/q/6259982/4642212) here. Note that `e.price &&` already covers the value `0`, so which data type is `e.price`? String or number? You say it’s a number, so there is nothing to do here.
    – Sebastian Simon Sep 30 '21 at 17:31
  • 2
    I'm sorry but this is a really bad piece of code. Why on Earth are all these lines a single statement? No wonder you can't edit it. Please refactor the code so each of those lines is a separate statement and the answer should be more obvious. – Ruan Mendes Sep 30 '21 at 17:32
  • @JuanMendes let me put the whole code – Revol Begins Sep 30 '21 at 17:33
  • 1
    Adding more code is not going to make this code look better. But it would be good if you can provide code with which your issue can be reproduced. Remove anything that is not related to the issue (e.g. no other variables than price) – trincot Sep 30 '21 at 17:34
  • @RevolBegins And what exactly do you want us to do with these additional 185 lines of code? – Sebastian Simon Sep 30 '21 at 17:35
  • 1
    @RevolBegins I did not ask for the full code, that makes for a lower quality question with unnecessary code. Note that if `e.price` is zero, you will not get the price in the output. See https://jsfiddle.net/mendesjuan/y57ctg8h/3/ – Ruan Mendes Sep 30 '21 at 17:36
  • @SebastianSimon its a number – Revol Begins Sep 30 '21 at 17:37
  • i am sorry but i am learning all this, not a professional like you people , so that is why i am asking may be you can correct me :/ – Revol Begins Sep 30 '21 at 17:38
  • @RevolBegins First learn how to ask a question. https://stackoverflow.com/help/how-to-ask Please t[ake a look at the jsfiddle](https://jsfiddle.net/mendesjuan/y57ctg8h/3/) I posted and notice that what you are claiming is happening does not seem to be happening. – Ruan Mendes Sep 30 '21 at 17:38
  • Please provide **only** code that is necessary to reproduce the issue you are asking about. Your question just became 100% worse. – trincot Sep 30 '21 at 17:38
  • I feel bad for you, just getting started and having to deal with this piece of garbage! It feels like this code was generated, not hand-written. Arrrrggggggh – Ruan Mendes Sep 30 '21 at 17:43
  • i also feeling so bad , the way i am getting answers :( the code is in fact hand written and is showing normal results , i just wanted to create if statement in it. but feels down after these answers – Revol Begins Sep 30 '21 at 17:48
  • 1
    @RevolBegins Have you tried the suggestions? You REALLY should learn how to ask a question that requires minimal effort from those trying to help you (for free). I have even told you that I doubt what you are saying and gave you an example to prove it but you ignored it. I should be down that I invested my time but you did not make anything of it. – Ruan Mendes Oct 01 '21 at 12:12

2 Answers2

1

you can use a Template literal with a short hand condition like so -
<div class="search-price"> ${e.price === 0 ? '' : e.price} </div>

Guy Perry
  • 306
  • 1
  • 7
  • yes, maybe `${e.price <= 0 ? '' : e.price}` – Benoit Drogou Sep 30 '21 at 11:38
  • Thanks for replying , you mean i shall write it like this e.price && (a += '
    ' ${e.price === 0 ? '' : e.price} "
    "),
    – Revol Begins Sep 30 '21 at 11:45
  • yep, the whole line should look like this - e.price && (a += `
    ${e.price === 0 ? '' : e.price}
    `), just make sure the whole div element is wrapped with backticks - you can read more about it here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
    – Guy Perry Sep 30 '21 at 11:58
  • i had put this exact line but the JS does not work with it e.price && (a += '
    ${e.price === 0 ? '' : e.price}
    '),
    – Revol Begins Sep 30 '21 at 15:13
  • @RevolBegins Guy Perry’s comment was incorrectly formatted. They meant ``e.price && (a += `
    ${e.price === 0 ? '' : e.price}
    `)``. Please be aware of the [difference between `\`` and `'`](/q/37245679/4642212); read the [documentation](//developer.mozilla.org/docs/Web/JavaScript/Reference/Template_literals).
    – Sebastian Simon Sep 30 '21 at 17:38
  • 1
    @SebastianSimon That doesn't make sense, it won't go into the `a +=` section because `e.price` is already tested – Ruan Mendes Oct 01 '21 at 13:44
-1

Just a suggestion, no guarantee it works 100%


formatResult: function(e, a) {
  var i = "(" + (a = "&" === a ? "&#038;" : a).replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + ")",
    a = "";

// Here
   if(e.price<=0){
      e.price = ""
   }
// Here

  return (
    e.divider && (a += ' <h5 class="search-divider-text">' + e.divider + "</h5>"),
    e.thumbnail && (a += ' <div class="search-thumb">' + e.thumbnail + "</div>"),
    e.value && (a += '<h4 class="search-title">' + e.value.replace(new RegExp(i, "gi"), "<strong>$1</strong>").replace(/&lt;(\/?strong)&gt;/g, "<$1>") + "</h4>"),
    e.no_found && (a = '<div class="no-result">' + e.value + "</div>"),
    e.view_all_products && (a = '<div class="view-all-products"></div>'),
    e.sku && (a += ' <div class="search-sku">' + e.sku + "</div>"),
    e.price && (a += ' <div class="search-price">' + e.price + "</div>"),
    a
  );
},
Benoit Drogou
  • 969
  • 1
  • 5
  • 15