Post by BatuBskn » Thu May 25, 2023 5:18 pm

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

Newbie

Posts

Joined
Tue Nov 01, 2022 5:26 am

Post by SohBH » Thu May 25, 2023 7:35 pm

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));
    }
}

Business Web Development | Content Creation | Analytics and Reporting | SEO


User avatar
Active Member

Posts

Joined
Mon Nov 02, 2020 12:01 am
Location - Malaysia

Post by BatuBskn » 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.

Newbie

Posts

Joined
Tue Nov 01, 2022 5:26 am

Post by softmonke » Fri May 26, 2023 6:15 am

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)?

Reach out to us at hello@softmonke.com for your OpenCart web development needs.


User avatar
Active Member

Posts

Joined
Tue May 23, 2023 4:42 am


Post by straightlight » Fri May 26, 2023 7:15 pm

$data['products'] is not required to be used since $json['products'] is taking place in this scenario.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by BatuBskn » 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.

Newbie

Posts

Joined
Tue Nov 01, 2022 5:26 am

Post by straightlight » Sun May 28, 2023 5:10 am

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 264 guests