Post by Redcomet » Mon Jul 06, 2015 11:07 am

Hello,

I am trying to customize latest.php to make out of stock items hidden.

I have added the following line

Code: Select all

if ($result['quantity'] <= 0) { continue; }
under

Code: Select all

foreach ($results as $result) {
Now this code does its job but the issue is when I have lets say 6 products enabled on the latest module (based on $data['limit']), only 5 show up.

Code: Select all

$data = array(
        'sort'  => 'p.date_added',
        'order' => 'DESC',
        'start' => 0,
        'limit' => $setting['limit']
    );
My thought was to somehow increment the foreach loop when quantity = 0 but every time I try, I get a syntax error.

How can I play around with latest.php to make sure 6 products show even when one is out of stock?

Thanks
Last edited by Redcomet on Thu Jul 23, 2015 4:04 am, edited 1 time in total.

Newbie

Posts

Joined
Mon Jul 06, 2015 10:54 am

Post by Redcomet » Tue Jul 07, 2015 12:47 pm

I have figured it out for all those who are wondering how to skip out of stock items without losing a blank space.

I used a simple counter system with a break function. Here is the code I used

Code: Select all

$data = array(
			'sort'  => 'p.date_added',
			'order' => 'DESC',
			'start' => 0,
			'limit' => 20,
		);

		$count = 0;
		$results = $this->model_catalog_product->getProducts($data);

		foreach ($results as $result) {
		if ($count == 6) break;
		$count++;
		if ($result['quantity'] <= 0) { $count--; continue; }
			if ($result['image']) {
				$image = $this->model_tool_image->resize($result['image'], $setting['image_width'], $setting['image_height']);
			} else {
				$image = false;
			}
						
			if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
				$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$price = false;
			}
					
			if ((float)$result['special']) {
				$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$special = false;
			}
			
			if ($this->config->get('config_review_status')) {
				$rating = $result['rating'];
			} else {
				$rating = false;
			}
			
			$this->data['products'][] = array(
				'product_id' => $result['product_id'],
				'thumb'   	 => $image,
				'name'    	 => $result['name'],
				'price'   	 => $price,
				'special' 	 => $special,
				'rating'     => $rating,
				'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
				'href'    	 => $this->url->link('product/product', 'product_id=' . $result['product_id']),
			);
		}
If you want to customize it to the amount of items you want displayed for your front page, simply change

Code: Select all

if ($count == 6) break;
to your desired numbered.

Hope that helps everyone out there!

Newbie

Posts

Joined
Mon Jul 06, 2015 10:54 am

Post by ianhaney50 » Mon Aug 05, 2024 6:08 pm

Hi

I have tried this code for the latest module on the homepage and it's still showing a empty space for a out of stock product. I'm using opencart 2.3.0.2.

Should I increase the number 6 to 7 on this line

Code: Select all

if ($count == 6) break;

New member

Posts

Joined
Fri Apr 29, 2016 4:21 am

Post by ADD Creative » Mon Aug 05, 2024 7:32 pm

ianhaney50 wrote:
Mon Aug 05, 2024 6:08 pm
Hi

I have tried this code for the latest module on the homepage and it's still showing a empty space for a out of stock product. I'm using opencart 2.3.0.2.

Should I increase the number 6 to 7 on this line

Code: Select all

if ($count == 6) break;
That would depend on what the module limit is set to.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by ianhaney50 » Mon Aug 05, 2024 8:10 pm

ADD Creative wrote:
Mon Aug 05, 2024 7:32 pm
ianhaney50 wrote:
Mon Aug 05, 2024 6:08 pm
Hi

I have tried this code for the latest module on the homepage and it's still showing a empty space for a out of stock product. I'm using opencart 2.3.0.2.

Should I increase the number 6 to 7 on this line

Code: Select all

if ($count == 6) break;
That would depend on what the module limit is set to.
It's set to 9

New member

Posts

Joined
Fri Apr 29, 2016 4:21 am

Post by ADD Creative » Tue Aug 06, 2024 1:12 am

ianhaney50 wrote:
Mon Aug 05, 2024 8:10 pm
It's set to 9
Should probably check count == 9.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom
Who is online

Users browsing this forum: No registered users and 2 guests