Jump to content
ILexus

Фукнция getProductSpecials, модифировать запрос для вывода дополнительных товаров.

Recommended Posts

Posted (edited)

Всем привет. Помогите разобраться.

 

Дано:
Имеется функция getProductSpecials в catalog/model/catalog/product.php
Используется для вывода товаров на странице с акциями.

 

Задача:

Необходимо дополнительно вывести на странице с акциями, товары, у которых поле fake_special > 0 в oc_product

 

Поле есть, в карточке товара значение больше 0. Но никак не получается вывести данные товары.

 

Попытки вставить p.fake_special > '0' в SQL запрос не увенчались успехом. Не могу понять в чем причина. Вставлял по разному, через AND и OR, все равно не проходит проверку. Отображается только если в карточке товара прописана Акция.

 

Код оригинал

$sql = "SELECT DISTINCT ps.product_id, (
SELECT AVG(rating) FROM " . DB_PREFIX . "review r1 
WHERE r1.product_id = ps.product_id AND r1.status = '1' 
GROUP BY r1.product_id) AS rating 
FROM " . DB_PREFIX . "product_special ps 
LEFT JOIN " . DB_PREFIX . "product p ON (ps.product_id = p.product_id) 
LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) 
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE p.status = '1' 
AND p.date_available <= NOW() 
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' 
AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' 
AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) 
AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) 
GROUP BY ps.product_id";

Вот так по идеи должно работать, но нет. Видимо что то упускаю.

$sql = "SELECT DISTINCT ps.product_id, (
SELECT AVG(rating) 
FROM " . DB_PREFIX . "review r1 
WHERE r1.product_id = ps.product_id AND r1.status = '1' 
GROUP BY r1.product_id) AS rating 
FROM " . DB_PREFIX . "product_special ps 
LEFT JOIN " . DB_PREFIX . "product p ON (ps.product_id = p.product_id) 
LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) 
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE p.status = '1' 
AND p.date_available <= NOW() 
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' 
AND ((ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "') OR p.fake_special > '0') 
AND (((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) 
AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) OR (p.fake_special > '0')) 
GROUP BY ps.product_id";

 

Edited by ILexus

Share this post


Link to post
Share on other sites
Posted (edited)

del

Edited by Vetroff
точно

Share this post


Link to post
Share on other sites
6 часов назад, ILexus сказал:

Вот так по идеи должно работать, но нет. Видимо что то упускаю.

 

вы упускаете это

 

image.thumb.png.a2765a3692d196508fdaea42e516f1e7.png

 

то есть товаров у которых нет акций там в принципе быть не может

Share this post


Link to post
Share on other sites
В 16.05.2020 в 09:45, spectre сказал:

 

вы упускаете это

 

image.thumb.png.a2765a3692d196508fdaea42e516f1e7.png

 

то есть товаров у которых нет акций там в принципе быть не может

Спасибо за ответ. Да действительно, мы ведь получаем данные из oc_product_special, а там не могут быть товары у которых не прописаны акции.

 

Тогда к сожалению моих знаний не хватает, чтобы реализовать данную хотелку. Если только кто нибудь подскажет, как правильно и лучше сделать.

Share this post


Link to post
Share on other sites

Union

Share this post


Link to post
Share on other sites
В 19.05.2020 в 02:27, chukcha сказал:

Union

Спасибо за совет, получилось объединить два SELECT с помощью UNION, дополнительные товары вывелись. Правда не знаю правильно ли использовал UNION

$sql = "SELECT ps.product_id, (
SELECT AVG(rating) FROM " . DB_PREFIX . "review r1 
WHERE r1.product_id = ps.product_id AND r1.status = '1' 
GROUP BY r1.product_id) AS rating 
FROM " . DB_PREFIX . "product_special ps 
LEFT JOIN " . DB_PREFIX . "product p ON (ps.product_id = p.product_id) 
LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) 
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE p.status = '1' 
AND p.date_available <= NOW() 
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' 
AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' 
AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) 
AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) 
GROUP BY ps.product_id
UNION
SELECT p.product_id, (
SELECT AVG(rating) FROM " . DB_PREFIX . "review r1 
WHERE r1.product_id = p.product_id AND r1.status = '1' 
GROUP BY r1.product_id) AS rating 
FROM " . DB_PREFIX . "product p
LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) 
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE p.status = '1' 
AND p.fake_special > '0'
AND p.date_available <= NOW() 
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' 
GROUP BY p.product_id";

Но вылезла проблема с сортировкой, при ORDER BY p.sort_order например, вылезает ошибка Fatal error: Uncaught Exception: Error: Table 'p' from one of the SELECTs cannot be used in field list

Не получается объединить оба SELECT под одним ORDER BY.

Share this post


Link to post
Share on other sites

А последний?
 

Share this post


Link to post
Share on other sites
7 часов назад, chukcha сказал:

А последний?
 

Что последний?) Я конечно могу додумывать иногда, но в данном случае в замешательстве)

Share this post


Link to post
Share on other sites

SELECT
Union
SELECT
order by

SELECT * FROM (
SELECT
Union
SELECT

) t
order by
 

Share this post


Link to post
Share on other sites
Posted (edited)
2 часа назад, chukcha сказал:

SELECT
Union
SELECT
order by

SELECT * FROM (
SELECT
Union
SELECT

) t
order by
 

Да я пробовал так объединить под общим SELECT

$sql = "SELECT * FROM (SELECT ps.product_id, (
SELECT AVG(rating) FROM " . DB_PREFIX . "review r1 
WHERE r1.product_id = ps.product_id AND r1.status = '1' 
GROUP BY r1.product_id) AS rating 
FROM " . DB_PREFIX . "product_special ps 
LEFT JOIN " . DB_PREFIX . "product p ON (ps.product_id = p.product_id) 
LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) 
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE p.status = '1' 
AND p.date_available <= NOW() 
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' 
AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' 
AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) 
AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) 
GROUP BY ps.product_id
UNION
SELECT p.product_id, (
SELECT AVG(rating) FROM " . DB_PREFIX . "review r1 
WHERE r1.product_id = p.product_id AND r1.status = '1' 
GROUP BY r1.product_id) AS rating 
FROM " . DB_PREFIX . "product p
LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) 
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE p.status = '1' 
AND p.fake_special > '0'
AND p.date_available <= NOW() 
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' 
GROUP BY p.product_id) t
ORDER BY p.sort_order";

Fatal error: Uncaught Exception: Error: Unknown column 'p.sort_order' in 'order clause'

 

Edited by ILexus

Share this post


Link to post
Share on other sites

ORDER BY t.sort_order

Share this post


Link to post
Share on other sites
Только что, chukcha сказал:

ORDER BY t.sort_order

Fatal error: Uncaught Exception: Error: Unknown column 't.sort_order' in 'order clause'

Share this post


Link to post
Share on other sites

так его же нет в результирущем запросе

Share this post


Link to post
Share on other sites

Готовое решение

На стековерфлоу посоветовали достать данные через таблицу oc_product. Не знаю кому может пригодится, но все же оставлю здесь. Спасибо всем кто пытался помочь :)

 

Функция getProductSpecials

$sql = "SELECT DISTINCT p.product_id, (
SELECT AVG(rating) FROM " . DB_PREFIX . "review r1 
WHERE r1.product_id = ps.product_id AND r1.status = '1' 
GROUP BY r1.product_id) AS rating 
FROM " . DB_PREFIX . "product p
LEFT JOIN " . DB_PREFIX . "product_special ps ON (ps.product_id = p.product_id) 
LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) 
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE p.status = '1' 
AND p.date_available <= NOW() 
AND (ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' OR p.fake_special > '0')
AND (((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) 
AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) OR p.fake_special > '0') 
GROUP BY p.product_id";

Функция getTotalProductSpecials

$query = $this->db->query("
SELECT COUNT(DISTINCT p.product_id) AS total 
FROM " . DB_PREFIX . "product p 
LEFT JOIN " . DB_PREFIX . "product_special ps ON (ps.product_id = p.product_id) 
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) 
WHERE p.status = '1' 
AND p.date_available <= NOW() 
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' 
AND (ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' OR p.fake_special > '0')
AND (((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) 
AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) OR p.fake_special > '0')");

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.