Page 1 of 1
I am trying to use api to pull product price
Posted: Thu May 25, 2023 5:18 pm
by BatuBskn
I am working on an installment table, I used the following code in the product tab;
Code: Select all
<script>
var productPriceAPI = 'http://www.xxx.com/index.php?route=api/product&key=xxxx';
function fetchProductPrice() {
fetch(productPriceAPI)
.then(response => response.json())
.then(data => {
var productPrice = data.price;
var scriptElement = document.createElement('script');
scriptElement.src = 'https://www.xxx.com/api/amount=' + productPrice;
document.getElementById('divtable').appendChild(scriptElement);
})
.catch(error => {
console.error('error:', error);
});
}
window.addEventListener('DOMContentLoaded', fetchProductPrice);
</script>
test api:
Code: Select all
<?php
class ControllerApiProduct extends Controller {
public function index() {
$data = array(
'price' => 1000,
);
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($data));
}
}
I couldn't see an api where I can reach the product price, I have no idea how to do it. I will be glad if you help
Re: I am trying to use api to pull product price
Posted: Thu May 25, 2023 7:35 pm
by SohBH
Code: Select all
<script>
var productPriceAPI = 'http://www.xxx.com/index.php?route=api/product&key=xxxx';
function fetchProductPrice() {
fetch(productPriceAPI)
.then(response => response.json())
.then(data => {
var productPrice = data.price;
var scriptElement = document.createElement('script');
scriptElement.src = 'https://www.xxx.com/api/amount=' + productPrice;
document.getElementById('divtable').appendChild(scriptElement);
})
.catch(error => {
console.error('error:', error);
});
}
window.addEventListener('DOMContentLoaded', fetchProductPrice);
</script>
catalog/controller/api/product.php
Credit to webocreation
Code: Select all
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) {
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'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_product_description_length')) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'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));
}
}
Re: I am trying to use api to pull product price
Posted: Thu May 25, 2023 11:57 pm
by BatuBskn
This api is working but I am having trouble with my code in tab tab.
var productPrice = data.products[0].price
This way I can pull any data, but I couldn't figure out how to pull the price of the product.
Re: I am trying to use api to pull product price
Posted: Fri May 26, 2023 6:15 am
by softmonke
BatuBskn wrote: ↑Thu May 25, 2023 11:57 pm
This api is working but I am having trouble with my code in tab tab.
var productPrice = data.products[0].price
This way I can pull any data, but I couldn't figure out how to pull the price of the product.
What is this "tab tab" that you are referring to? Where exactly are you trying to retrieve the product price? Is it not possible to just get the product price in the controller file (PHP)?
Re: I am trying to use api to pull product price
Posted: Fri May 26, 2023 7:15 pm
by straightlight
$data['products'] is not required to be used since $json['products'] is taking place in this scenario.
Re: I am trying to use api to pull product price
Posted: Sat May 27, 2023 11:08 pm
by BatuBskn
Thank you for your help, I was talking about the tabs on the product page. I didn't need an api, instead I fixed it by adding a block.
Re: I am trying to use api to pull product price
Posted: Sun May 28, 2023 5:10 am
by straightlight
BatuBskn wrote: ↑Sat May 27, 2023 11:08 pm
Thank you for your help, I was talking about the tabs on the product page. I didn't need an api, instead I fixed it by adding a block.
Now that the issue has been resolved, please add: [SOLVED] at the beginning of the subject line on your first post.