Post by scottyboyyy » Sat Jan 14, 2023 4:45 am

Hello!

I'm trying to create a function that will select products that have the same model and then sort / order by price . The result I am getting though is finding the products with the same model but isn't ordering by price and I am struggling to work out why. Any help for an SQL noob would be much appreciated! ;D .

Current result:

Code: Select all

Array ( [0] => Array ( [product_id] => 30 [name] => Canon EOS 5D [price] => $146.00 ) [1] => Array ( [product_id] => 34 [name] => iPod Shuffle [price] => $26.00 ) [2] => Array ( [product_id] => 42 [name] => Apple Cinema 30" [price] => $122.00 ) [3] => Array ( [product_id] => 47 [name] => HP LP3065 [price] => $218.00 ) )
Desired result:

Code: Select all

Array ( [0] => Array ( [product_id] => 34 [name] => iPod Shuffle [price] => $26.00 ) [1] => Array ( [product_id] => 42 [name] => Apple Cinema 30" [price] => $122.00 ) [2] => Array ( [product_id] => 30 [name] => Canon EOS 5D [price] => $146.00 ) [3] => Array ( [product_id] => 47 [name] => HP LP3065 [price] => $218.00 ) )
Function:

Code: Select all

public function getProductsAndComparePrice($model) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE p.model = '" . $model . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)'");
		
		$product_data = array();

		foreach ($query->rows as $result) {
			$product_data[] = $this->getProduct($result['product_id']);
		}
		return $product_data;
	}

Active Member

Posts

Joined
Fri Apr 07, 2017 2:36 am

Post by by mona » Sat Jan 14, 2023 4:27 pm

You have a syntax error in your query and neither special nor discount are in the product table.

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by paulfeakins » Mon Jan 16, 2023 6:26 pm

scottyboyyy wrote:
Sat Jan 14, 2023 4:45 am
I'm trying to create a function that will select products that have the same model and then sort / order by price . The result I am getting though is finding the products with the same model but isn't ordering by price and I am struggling to work out why. Any help for an SQL noob would be much appreciated! ;D .
You should really tell us why you're trying to do this if you want a good answer.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by paulfeakins » Mon Jan 16, 2023 6:26 pm

by mona wrote:
Sat Jan 14, 2023 4:27 pm
You have a syntax error in your query and neither special nor discount are in the product table.
And that could be fixed by: https://chat.openai.com/

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by SohBH » Sat Jan 21, 2023 4:10 pm

Code: Select all

public function getProductsAndComparePrice($model) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "product_special ps ON (p.product_id = ps.product_id) LEFT JOIN " . DB_PREFIX . "product_discount pd ON (p.product_id = pd.product_id) WHERE p.model = '" . $model . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . " ORDER BY (CASE WHEN ps.price IS NOT NULL THEN ps.price WHEN ps.discount IS NOT NULL THEN ps.discount ELSE p.price END)'");
		
		$product_data = array();

		foreach ($query->rows as $result) {
			$product_data[] = $this->getProduct($result['product_id']);
		}
		return $product_data;
	}

Business Web Development | Content Creation | Analytics and Reporting | SEO


User avatar
Active Member

Posts

Joined
Mon Nov 02, 2020 12:01 am
Location - Malaysia

Post by straightlight » Sun Jan 22, 2023 5:51 am

SohBH wrote:
Sat Jan 21, 2023 4:10 pm

Code: Select all

public function getProductsAndComparePrice($model) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "product_special ps ON (p.product_id = ps.product_id) LEFT JOIN " . DB_PREFIX . "product_discount pd ON (p.product_id = pd.product_id) WHERE p.model = '" . $model . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . " ORDER BY (CASE WHEN ps.price IS NOT NULL THEN ps.price WHEN ps.discount IS NOT NULL THEN ps.discount ELSE p.price END)'");
		
		$product_data = array();

		foreach ($query->rows as $result) {
			$product_data[] = $this->getProduct($result['product_id']);
		}
		return $product_data;
	}

Code: Select all

public function getProductsAndComparePrice($model) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "product_special ps ON (p.product_id = ps.product_id) LEFT JOIN " . DB_PREFIX . "product_discount pd ON (p.product_id = pd.product_id) WHERE p.model = '" . $this->db->escape($model) . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . " ORDER BY (CASE WHEN ps.price IS NOT NULL THEN ps.price WHEN ps.discount IS NOT NULL THEN ps.discount ELSE p.price END)'");
		
		$product_data = array();

		foreach ($query->rows as $result) {
			$product_data[] = $this->getProduct($result['product_id']);
		}
		
		return $product_data;
Added as an event as well since $this->getProduct() model may also need to be recalled.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 415 guests