Skip to content

Instantly share code, notes, and snippets.

@DevWael
Last active August 15, 2021 19:29
Show Gist options
  • Save DevWael/122ce99fbe8d7df8a0398962839fb689 to your computer and use it in GitHub Desktop.
Save DevWael/122ce99fbe8d7df8a0398962839fb689 to your computer and use it in GitHub Desktop.
get all products in opencart 3.0 api
<?php
class ControllerApiProduct extends Controller {
public function index() {
$this->load->language( 'api/cart' );
$this->load->model( 'catalog/product' );
$this->load->model( 'tool/image' );
$json = array();
$json['products'] = array();
$filter_data = array();
$results = $this->model_catalog_product->getProducts( $filter_data );
foreach ( $results as $result ) {
$product_images = $this->model_catalog_product->getProductImages( $result['product_id'] );
$thumbs = array();
if ( $product_images ) {
foreach ( $product_images as $product_image ) {
$thumbs[] = $this->model_tool_image->resize( $product_image['image'], $this->config->get( 'theme_' . $this->config->get( 'config_theme' ) . '_image_product_width' ), $this->config->get( 'theme_' . $this->config->get( 'config_theme' ) . '_image_product_height' ) );
}
}
if ( $thumbs ) {
$thumbs = implode( ',', $thumbs );
} else {
$thumbs = '';
}
if ( $result['image'] ) {
$image = $this->model_tool_image->resize( $result['image'], $this->config->get( 'theme_' . $this->config->get( 'config_theme' ) . '_image_product_width' ), $this->config->get( 'theme_' . $this->config->get( 'config_theme' ) . '_image_product_height' ) );
} else {
$image = $this->model_tool_image->resize( 'placeholder.png', $this->config->get( 'theme_' . $this->config->get( 'config_theme' ) . '_image_product_width' ), $this->config->get( 'theme_' . $this->config->get( 'config_theme' ) . '_image_product_height' ) );
}
if ( $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' ) ), $this->session->data['currency'] );
} 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' ) ), $this->session->data['currency'] );
} else {
$special = false;
}
if ( $this->config->get( 'config_tax' ) ) {
$tax = $this->currency->format( (float) $result['special'] ? $result['special'] : $result['price'], $this->session->data['currency'] );
} else {
$tax = false;
}
if ( $this->config->get( 'config_review_status' ) ) {
$rating = (int) $result['rating'];
} else {
$rating = false;
}
$data['products'][] = array(
'product_id' => $result['product_id'],
'name' => $result['name'],
'description' => trim( strip_tags( html_entity_decode( $result['description'], ENT_QUOTES, 'UTF-8' ) ) ),
'thumb' => $image,
'images' => $thumbs,
'price' => $price,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'quantity' => $result['quantity'],
'sku' => $result['sku'],
'model' => $result['model'],
'rating' => $result['rating'],
'href' => $this->url->link( 'product/product', 'product_id=' . $result['product_id'] ),
);
}
$json['products'] = $data['products'];
$this->response->addHeader( 'Content-Type: application/json' );
$this->response->setOutput( json_encode( $json ) );
}
}
@hiahmad30
Copy link

thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment