souptical souptical - 1 month ago 9
PHP Question

Get ID from product before and after

I am trying to get IDs from products before and after the product I am currently looking and so I can get the info from those products and make a next product and previous product buttons

I am working on Opencart version 2.3.0.2

This is all in product.php controller all of this is around line 277, after the data['product_id'] I added my code

$data['product_id'] = (int)$this->request->get['product_id'];

$results = $this->model_catalog_product->getProduct($data['product_id']);

foreach ($results as $result) {
if ($result['product_id'] < $data['product_id']){
$data['product_before_id'] = $result['product_id'];
} else {
$data['product_before_id'] = 0;
}

if ($result['product_id'] > $data['product_id']){
$data['product_after_id'] = $result['product_id'];
} else {
$data['product_after_id'] = 0;
}

}


I was trying to get it but got illegal string offset

The altered answer

$next_product = $this->model_catalog_product->getProduct($product_id + 1);
while($next_product === false){
$product_id = $product_id + 1;
$next_product = $this->model_catalog_product->getProduct($product_id);
}

$previous_product = $this->model_catalog_product->getProduct($product_id - 1);
while($previous_product === false){
$product_id = $product_id - 1;
$previous_product = $this->model_catalog_product->getProduct($product_id);
}


if ($previous_product !== false) {
if ($previous_product['product_id'] == $data['product_id']) {
$data['product_before_id'] = 0;
} else {
$data['product_before_id'] = $previous_product['product_id'];
}
} else {
$data['product_before_id'] = 0;
}

if ($next_product !== false) {
if ($next_product['product_id'] == $data['product_id']) {
$data['product_after_id'] = 0;
} else {
$data['product_after_id'] = $next_product['product_id'];
}
} else {
$data['product_after_id'] = 0;
}

Answer

The code you wrote is flawed to begin with. The getProduct function retrieves the information about a single product, not all products.

Based on what you would like to do I would simply get the current page's product ID and then check the ID's on either side of it. So if the product ID is 4 check if both product ID 3 and 5 exist.

This can be achieved by simply running the getProduct function for both of them because if the product ID argument does not exist in the database the function will just return false.

Example:

$product_id = (int)$this->request->get['product_id'];

$previous_product_id = $this->model_catalog_product->getProduct($product_id - 1);
$next_product_id = $this->model_catalog_product->getProduct($product_id + 1);

if ($previous_product_id !== false) {
    $data['product_before_id'] = $previous_product_id;
} else {
    $data['product_before_id'] = 0;
}

if ($next_product_id !== false) {
    $data['product_after_id'] = $next_product_id;
} else {
    $data['product_after_id'] = 0;
}

Make sure that you use the !== operator and not the != operator as !== checks that the type is actually a boolean and therefore a value of false indicating that a product does not exist for the supplied product ID.

This answer applies only to retrieving the previous and next product. To retrieve a full set of all products ordered by their ID's would require a completely different answer.