public function onAjaxVmsearch() { $app = JFactory::getApplication(); $db = JFactory::getDbo(); $term = trim($app->input->getString('term', '')); $limit = (int) $app->input->getInt('limit', (int)$this->params->get('limit', 12)); $minChars = (int) $this->params->get('minchars', 2); if (JString::strlen($term) < $minChars) { return array('results' => array(), 'total' => 0); } // Language table detection (PRODUCTS ONLY) $lang = JFactory::getLanguage(); $tag = strtolower(str_replace('-', '_', $lang->getTag())); $dbprefix = $db->getPrefix(); $langTableName = $dbprefix . 'virtuemart_products_' . $tag; try { $db->setQuery('SHOW TABLES LIKE ' . $db->quote($langTableName)); $exists = (bool) $db->loadResult(); } catch (Exception $e) { $exists = false; } $langTbl = $db->qn($exists ? $langTableName : ($dbprefix . 'virtuemart_products_en_gb')); // NOTE: removed category language detection and usage, // we no longer search in category name/description. // Subqueries (image, category, min price) $subQ = " SELECT pm.virtuemart_product_id, COALESCE(NULLIF(m.file_url_thumb,''), m.file_url) AS file_url_thumb FROM #__virtuemart_product_medias AS pm INNER JOIN ( SELECT pm2.virtuemart_product_id, MIN(pm2.ordering) AS min_ordering FROM #__virtuemart_product_medias AS pm2 INNER JOIN #__virtuemart_medias AS m2 ON m2.virtuemart_media_id = pm2.virtuemart_media_id WHERE (m2.file_mimetype LIKE 'image/%' OR m2.file_type = 'product') GROUP BY pm2.virtuemart_product_id ) AS firstimg ON firstimg.virtuemart_product_id = pm.virtuemart_product_id AND firstimg.min_ordering = pm.ordering INNER JOIN #__virtuemart_medias AS m ON m.virtuemart_media_id = pm.virtuemart_media_id "; $catQ = " SELECT pc.virtuemart_product_id, MIN(pc.virtuemart_category_id) AS cat_id FROM #__virtuemart_product_categories AS pc GROUP BY pc.virtuemart_product_id "; $priceQ = " SELECT virtuemart_product_id, MIN(product_price) AS product_price FROM #__virtuemart_product_prices GROUP BY virtuemart_product_id "; // --- Search logic (PRODUCT FIELDS ONLY) ------------------------ $clean = preg_replace('/\s+/u', ' ', $term); $tokens = array(); foreach (explode(' ', $clean) as $tk) { $tk = trim($tk); if ($tk !== '') $tokens[] = $tk; } $phraseLike = $db->quote('%' . $term . '%'); // Phrase ORs – only product fields (no category_* here) $phraseOrs = array( 'pl.product_name LIKE ' . $phraseLike, 'p.product_sku LIKE ' . $phraseLike, 'pl.product_s_desc LIKE ' . $phraseLike, 'pl.product_desc LIKE ' . $phraseLike, 'pl.metadesc LIKE ' . $phraseLike ); $phraseCond = '(' . implode(' OR ', $phraseOrs) . ')'; // AND per token, OR across product fields $andTokens = array(); foreach ($tokens as $tk) { $like = $db->quote('%' . $tk . '%'); $ors = array( 'pl.product_name LIKE ' . $like, 'p.product_sku LIKE ' . $like, 'pl.product_s_desc LIKE ' . $like, 'pl.product_desc LIKE ' . $like, 'pl.metadesc LIKE ' . $like ); $andTokens[] = '(' . implode(' OR ', $ors) . ')'; } // final WHERE: phrase OR (AND of tokens) $where = !empty($andTokens) ? '(' . $phraseCond . ' OR (' . implode(' AND ', $andTokens) . '))' : $phraseCond; // --- END unified search block --- // Page slice query (NOTE: no category-lang or category-name joins anymore) $query = $db->getQuery(true) ->select('p.virtuemart_product_id AS id') ->select('pl.product_name AS name') ->select('p.product_sku AS sku') ->select('pp.product_price AS price') ->select('img.file_url_thumb AS thumb') ->select('cat.cat_id AS cat_id') ->from($db->qn('#__virtuemart_products', 'p')) ->join('LEFT', $langTbl . ' AS pl ON pl.virtuemart_product_id = p.virtuemart_product_id') ->join('LEFT', '(' . $priceQ . ') AS pp ON pp.virtuemart_product_id = p.virtuemart_product_id') ->join('LEFT', '(' . $subQ . ') AS img ON img.virtuemart_product_id = p.virtuemart_product_id') ->join('LEFT', '(' . $catQ . ') AS cat ON cat.virtuemart_product_id = p.virtuemart_product_id') ->where('p.published = 1') // Only top-level products (no children in results) ->where('(p.product_parent_id = 0 OR p.product_parent_id IS NULL)') ->where('cat.cat_id IS NOT NULL AND cat.cat_id > 0') ->where($where) ->group('p.virtuemart_product_id') ->order('pl.product_name ASC'); $db->setQuery($query, 0, $limit); $rows = $db->loadAssocList(); // Total count with same filters $countQuery = $db->getQuery(true) ->select('COUNT(DISTINCT p.virtuemart_product_id)') ->from($db->qn('#__virtuemart_products','p')) ->join('LEFT', $langTbl . ' AS pl ON pl.virtuemart_product_id = p.virtuemart_product_id') ->join('LEFT', '(' . $catQ . ') AS cat ON cat.virtuemart_product_id = p.virtuemart_product_id') ->where('p.published = 1') ->where('(p.product_parent_id = 0 OR p.product_parent_id IS NULL)') ->where('cat.cat_id IS NOT NULL AND cat.cat_id > 0') ->where($where); $db->setQuery($countQuery); $total = (int)$db->loadResult(); // VM helpers for currency formatting ONLY (no product model / parent-child loading) if (!class_exists('VmConfig')) { @include_once JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/config.php'; } if (class_exists('VmConfig')) { VmConfig::loadConfig(); } if (!class_exists('CurrencyDisplay')) { @include_once JPATH_ADMINISTRATOR . '/components/com_virtuemart/helpers/currencydisplay.php'; } $currency = class_exists('CurrencyDisplay') ? CurrencyDisplay::getInstance() : null; $itemid = ''; if (!class_exists('VmRoute')) { @include_once JPATH_SITE . '/components/com_virtuemart/helpers/vmrouter.php'; } $results = array(); foreach ((array)$rows as $r) { $id = (int)$r['id']; $cid = (int)$r['cat_id']; if ($cid <= 0) continue; $label = $r['name'] ? $r['name'] : ('#' . $id); $link = 'index.php?option=com_virtuemart&view=productdetails&virtuemart_product_id=' . $id; if ($cid > 0) { $link .= '&virtuemart_category_id=' . $cid; } if ($itemid !== '') { $link .= '&Itemid=' . urlencode($itemid); } $url = ''; if (class_exists('VmRoute')) { $url = VmRoute::_($link, true); } if (!$url || $url === '0' || $url === 0) { $routed = JRoute::_($link); $url = ($routed && $routed !== '0') ? $routed : (rtrim(JUri::root(), '/') . '/' . ltrim($link, '/')); } // SIMPLE price: do NOT call getProduct() (no parent/child traversal) $priceStr = ''; if ($r['price'] !== null && $r['price'] !== '') { $rawPrice = (float)$r['price']; if ($currency) { $priceStr = $currency->priceDisplay($rawPrice); } else { $priceStr = number_format($rawPrice, 2, ',', '.') . ' €'; } } $img = ''; if (!empty($r['thumb'])) { $img = $r['thumb']; if (strpos($img, 'http') !== 0) { $img = rtrim(JUri::root(), '/') . '/' . ltrim($img, '/'); } } $results[] = array( 'label' => $label, 'value' => $label, 'url' => $url, 'image' => $img, 'price' => $priceStr, 'sku' => $r['sku'] ); } return array('results' => $results, 'total' => $total); } results
Φίλτρα filters

ΜΠΑΟΥΛΟ - ΠΟΥΦ - ΣΚΑΜΠΟ