Скрыть цены с 0,00 руб в Prestashop 1.6
Как то возникла необходимость скрыть нули (0,00 руб) в товарах в которых нет цен, тем самым было решено сделать модуль для Prestashop который бы скрывал эти самые цены.
Начинаем делать модуль для Prestashop чтобы он скрывал div с нулевой ценой (убрать нули в ценах после запятой).
Создаем папку и называем ее hiddenpricenull
В ней создаем следующие файлы:
В папке js создаем файл hiddenpricenull.js
В вашей теме в файле global.css добавим стиль для скрытия
1 2 3 | .noneprice { display: none!important; } |
Также можно создать файл допустим в папке js hiddenpricenull.css и подключить его в файле hiddenpricenull.php
1 2 3 4 5 6 7 8 9 10 11 | public function hookdisplayHeader($params) { // $this->context->controller->addCSS($this->_path.'/css/hiddenpricenull.css', 'all'); $this->context->controller->addJS($this->_path.'/js/hiddenpricenull.js', 'all'); return; } |
Ну и собственно файл hiddenpricenull.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | $(document).ready( function() { var nullpricedisplay = $('#our_price_display').text(); var nullproductprice = $('.product-price').text(); nullpricedisplay = nullpricedisplay.replace(/[^-0-9]/gim,''); nullproductprice = nullproductprice.replace(/[^-0-9]/gim,''); if(nullpricedisplay == 0) { nullpricedisplay = $('#our_price_display').text('Уточнить цену'); nullpricedisplay = $('#quantity_wanted_p').remove(); } // if(nullproductprice == 0) { // nullproductprice = $('.content_price').text('уточнить цену'); // //nullproductprice = $('.content_price').remove(); // } let block_array = document.querySelectorAll(".product-price"); for (let i = 0; i < block_array.length; i++) { let child = block_array[i].lastChild; let value = +child.innerHTML; var content = block_array[i].innerHTML.trim(); //var drinks = []; contente = content.replace(/[^-0-9]/gim,''); //drinks.push(content); console.log(contente); if (contente == 0 ) { block_array[i].classList.add("noneprice"); } else { //block_array[i].classList.add("none2222"); } } //alert(nullproductprice); }); |
Пожалуй на этом все, кратко конечно, но работает!
Устанавливаем в престу, активируем и смотрим.
Скачать готовый модуль можно здесь!
23.03.2023 — Немного дополнил файлик по скрытии цен с 0,00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | $(document).ready( function() { var nullpricedisplay = $('#our_price_display').text(); var nullproductprice = $('.product-price').text(); var nullpricebox = $('.price-box').text(); var nullpricedisplay2 = $('.price_display').text(); nullpricedisplay = nullpricedisplay.replace(/[^-0-9]/gim,''); nullproductprice = nullproductprice.replace(/[^-0-9]/gim,''); nullpricebox = nullpricebox.replace(/[^-0-9]/gim,''); nullpricedisplay2 = nullpricedisplay2.replace(/[^-0-9]/gim,''); if(nullpricedisplay == 0) { nullpricedisplay = $('#our_price_display').html('<a href="#" ><i class="icon-download"></i> Прайс-лист</a>'); nullpricedisplay = $('#quantity_wanted_p').remove(); } if(nullpricebox == 0) { //nullpricebox = $('.content_price').text('уточнить цену'); nullpricebox = $('.price-box').remove(); } if(nullpricedisplay2 == 0) { //nullpricebox = $('.content_price').text('уточнить цену'); nullpricedisplay2 = $('.price_display').remove(); } let block_array = document.querySelectorAll(".product-price"); for (let i = 0; i < block_array.length; i++) { let child = block_array[i].lastChild; let value = +child.innerHTML; var content = block_array[i].innerHTML.trim(); contente = content.replace(/[^-0-9]/gim,''); if (contente == 0 ) { block_array[i].classList.add("noneprice"); } } // при выборе фильтра в категории $(document).ajaxComplete(function() { let block_array = document.querySelectorAll(".product-price"); for (let i = 0; i < block_array.length; i++) { var content = block_array[i].innerHTML.trim(); contente = content.replace(/[^-0-9]/gim,''); if (contente == 0 ) { block_array[i].classList.add("noneprice"); } } }); //если на странице продукта есть атрибуты $('#our_price_display').bind('DOMNodeInserted DOMNodeRemoved', function(event) { if (event.type == 'DOMNodeInserted') { nullpricedisplay = $('#our_price_display').html('<a href="#" ><i class="icon-download"></i> Прайс-лист</a>'); } }); }); |
prestashop 1.7 как сделать чтобы когда цена указать различные надписи можно поэтому же принципу.
Да еще на странице товара если есть сопутствующие тоже с нулевыми значениями, то можно и их скрыть. Просто добавить класс в selectorAll
типа так
let block_array = document.querySelectorAll(«.product-price, .product-box .price»);
Еще одно небольшое дополнение — был выявлен баг на странице с товарами там где присутствуют фильтры. Т.е. при изменении фильтра 0,00 руб снова появлялись. поэтому внутрь документ ready добавить $(document).ajaxComplete(function() {...}); $(document).ready(function() { $(document).ajaxComplete(function() { let block_array = document.querySelectorAll(".product-price"); for (let i = 0; i < block_array.length; i++) { var content = block_array[i].innerHTML.trim(); contente = content.replace(/[^-0-9]/gim,''); if (contente == 0 ) { block_array[i].classList.add("noneprice"); } } }); }); 12345678910111213 $(document).ready(function() { $(document).ajaxComplete(function() { let block_array = document.querySelectorAll(".product-price"); for (let i = 0; i < block_array.length; i++) { var content = block_array[i].innerHTML.trim(); contente = content.replace(/[^-0-9]/gim,''); if (contente ==… Read more »