dimanche 5 juillet 2015

Increasing loop by 1 for a foreach data array

I am working on a website where I am trying to have the front page skip showing products that are out of stock.

The code for showing the products is the following:

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


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

    foreach ($results as $result) {
    if ($result['quantity'] <= 0) { 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']),
        );
}

For this foreach loop, the limit in this case is set to 6, so this code runs 6 times to show 6 items on the front page.

In order to skip Out Of Stock items I have added the line:

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

Now this code does its job but when it detects an item with 0 stock, it leaves an empty space (so rather than showing 6 product spaces it shows 5)

What I would like to accomplish is when this code detects a 0 stock item is to increment the foreach loop so it runs 7 times rather than 6.

Thanks!

Aucun commentaire:

Enregistrer un commentaire