Displaying api output

Hi,

I am at a begginer level with cs-cart coding. I have the output I want in raw format (outputs some strings and links), but are stuck on the next step

Q1. How do I now display my output within a cs-cart frame/page?

This is how my solution works so far

  1. I have created a page (using the admin panel menu) with a input box
  2. I have a php file in the root of the store that takes the $Pist input box, makes some API calls and returns an array of results
  3. using basic php/html tags I have a set of vairables I want to display

Q2. Also is it wrong to go about a solution this way, I know the solution is personalised to my store, and it isn’t an add-on but I don’t want to commercialise it so I think that is ok? i.e. as long as I prevent sql injection there isn’t a security risk and if I am not doing PUTs I am not going to break anything?

Cheers :slight_smile:

Hello!

  1. It’s better to output to the Smarty template. This way you can separate the logic of the code from the display of its contents, and use all the features that Smarty has to offer.

  2. It’s up to you decide how to make you add-on, but in case you wish to offer it to other people, you need to rework it to comply with the coding standards:
    Coding Standards — CS-Cart 4.16.x documentation
    DOs and DON’Ts for CS-Cart Devs — CS-Cart 4.16.x documentation
    Controllers — CS-Cart 4.16.x documentation

Hi,
Thanks for the input. I don’t think I have broken any of the listed standards. I am simply using API calls to get data and then manipulating it (but not modifying/PUTing it), what suggests I might have not followed any of those links (and I will go back and have another look, not that I am selling it, but good to learn right from the start!).

My PHP file is called from a submit action from a form using the inbuilt menu/capabilities … so don’t think anything wrong their either?

So how do I follow your advice and use a smarty to display the content (I know how to add a html/smarty block to layout) … could you give me an example based on if I had created a vairable $my_test_variable in my php file how would I display that? does it have to be on the same page where the form called the php file? … or is this where the standards start to become an issue i.e. you can’t get back into cs-cart ‘legitimately’ without an add-on?

Speaking of add-ons, for example it won’t be possible to provide such a solution at the CS-Cart Add-on Market, since your solution (if it is not a service) must have a package with your add-on.

In this case, I recommend starting with these tutorials, which will introduce you to the wonderful world of CS-Cart add-ons. :slight_smile:
https://docs.cs-cart.com/latest/developer_guide/addons/tutorials/index.html

You still haven’t answered either question and twice addressed a question I didn’t ask (I have said I don’t want ot sell it). But thanks for having a crack at it.

Oh, sorry, I read it in the wrong way. Instead of

I have read

I want to commercialise it so I think that is ok?

Forgive me for my blindness :smiling_face_with_tear:

In case you will use the separate script, you need to do the following:

  1. Make sure that your script connects to the installation itself. This can be achieved by including the following code in the beginning of your script:
<?php
define('AREA', 'A');
define('ACCOUNT_TYPE', 'admin');

require(dirname(__FILE__) . '/init.php');
  1. Once all the necessary data is processed and is ready to be displayed, you can now to put it into the Smarty variable:
Tygh::$app['view']->assign('variable_name', $data);

where variable_name will be the name of the variable that can be used in the Smarty template.

  1. Than, since all the code is located within one separate script (not as separate controller within an add-on), you can manually display the template:
Tygh::$app['view']->display('views/auth/popup_login_form.tpl');

where views/auth/popup_login_form.tpl is the relative path to the template from the design/themes/[active_theme_name]/templates/ folder.

  1. Create the template, which path you have specified in the 3rd step. There you will be able to use all the variables you have assigned in the 2nd step. Information on Smarty syntax can be found here:
    Smarty Documentation
1 Like

MANY thanks for this @CS-Cart_team!

1 Like

I have tried to apply this and when it wasn’t working I dumbed it down to a simple test but I get a 404 not found error. I believe I have followed @CS-Cart_team instructions but I musn’t have?

1. Created custom_script.php in root directy

<?php
define('AREA', 'A');
define('ACCOUNT_TYPE', 'admin');

require(dirname(__FILE__) . '/init.php');

use Tygh\Registry;

// Sample data to be assigned
$data = array(
    'message' => 'Hello, this is a custom message test!',
    'timestamp' => date('Y-m-d H:i:s')
);

// Assign the data to a Smarty variable
Tygh::$app['view']->assign('custom_data', $data);

// Display the template
Tygh::$app['view']->display('views/auth/custom_template.tpl');
?>

2. **Create Template File: custom_template.tpl

<div class="custom-message">
    <p>{$custom_data.message}</p>
    <p>Timestamp: {$custom_data.timestamp}</p>
</div>

3. Theme - I tried placing template file in

design/themes/**responsive**/templates/views/auth/custom_template.tpl

&

design/themes/**abt__unitheme2**/templates/views/auth/custom_template.tpl

4. Test

https://www.thefarmerslot.com.au/custom_script.php.

5. Fail!

=404 error (nothing in error_log

I see that previously, I wrote that you need to define AREA as A, and recommended to create a template in the theme’s folder. That’s a mistake.

In case you need to display the template from the frontend (theme’s folder), the AREA should be defined as C:

define('AREA', 'C');

In other way, the template must be located within the design/backend/templates folder.

As for the current situation, 404 error in response, I can only assume, that this script has incorrect permissions and/or ownership. Check that everything is correct there.

1 Like

Thankyou my test is now working. The 404 error was a typo.

Next to get it displaying in the storefront I get a empty result when I try and display vairable in a html/smarty

<p>Here is my data in a cs-cart layout smarty block: {$custom_data.message}</p>

I’ve also tried

Registry::get('view')->assign('custom_data', $data)

in place of
Tygh::$app['view']->display('views/auth/custom_template.tpl')

1 Like

No, it’s definitely should look like you’ve made if before:

// Assign the data to a Smarty variable
Tygh::$app['view']->assign('custom_data', $data);

// Display the template
Tygh::$app['view']->display('views/auth/custom_template.tpl');

Try to print out the contents of the variable in full:

{fn_print_r($custom_data)}

since this time I don’t know what might get wrong :melting_face:

Many thanks.

I ended up getting it working by putting it into the my_changes add-on. That defeats the original point, but at least it takes me closer to my own add-on. Until now I haven’t been too successful with the add-ons. Php, sql, vba, chatgpt no worries, but just can’t get my head around working with cs-cart (API is easy enough, but limited options). Heaps appreciate the help though.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.