Using ChatGPT AI to write cs-cart code

I have trained ChatGPT research release to answer the question

“please provide cs-cart code to list the product name, status and price for all products for my store”.

It wasn’t doing it at first, even though it knew which table the product name could be found in it didn’t join the tables, after telling it which table and the field to join on it successfully generated the code. I am wondering if I have trained it for me specificaly or if it works for others? If someone would be so kind to go there ask the same question.

I started off by asking it to write an add-on functions that generated most profitable products for each vendor, which it had a pretty good crack at but needed to much training to get it right so I started with something simpler (as above, which is simply a php file parked in the home directory). If a number of us spent time training this correctly we might be able to do some pretty cool things with it!

5 Likes

I got this answer;

Sure, here is an example of how you can list the product name, status, and price for all products in your CS-Cart store:

Copy code

{foreach from=$products item='product'}
  <h3>{$product.product}</h3>
  <p>Status: {if $product.status == 'A'}Active{else}Inactive{/if}</p>
  <p>Price: {$product.price}</p>
{/foreach}

This code uses a foreach loop to iterate over the $products array, which contains all of the products in your store. For each product, the code displays the product name, status (Active or Inactive), and price.

You can use this code in any CS-Cart template file, such as the product list template or the product details template. Just make sure to place the code in the appropriate location within the template file, and it should work as expected.

I hope this helps! Let me know if you have any questions or need further assistance.

1 Like

How and where to training it? it would be great if we can train chatGPT for cs-cart.

1 Like

Where do you go to use this ?
google ChatGPT … once you create an account you will end up with something like … OpenAI API

You train it by telling it what it has done wrong.

Also another trick is it doesn’t always give you all the code/steps, so you say continue from (last line it gave you, or from step x).

The (working) answer I got
This is the code that it produced in relation to my first question. It is simple standard alone php file that works just by quering the data base and not writing an add-on (place in a file with php extension and replace with your log in details).

<?php

// Connect to the database
$db = mysqli_connect("localhost", 'User_name, 'password', ‘database');

// Query the database to select the product name, status, and price for all products
$query = "SELECT pd.product, 
                 CASE p.status
                     WHEN 'A' THEN 'Active'
                     WHEN 'D' THEN 'Deleted'
                     WHEN 'H' THEN 'Hidden'
                 END AS status,
                 p.list_price
          FROM cscart_product_descriptions pd
          INNER JOIN cscart_products p ON pd.product_id = p.product_id";
$result = mysqli_query($db, $query);

// Loop through the result set and display the product name, status, and price
while ($row = mysqli_fetch_array($result)) {
    echo "<h3>" . $row['product'] . "</h3>";
    echo "<p>Status: " . $row['status'] . "</p>";
    echo "<p>Price: $" . $row['list_price'] . "</p>";
}

// Close the database connection
mysqli_close($db);

?>

How to train it to write an add-on?
After my first post (above) I almost got it to write the add-on, by saying stuff like, “aren’t you required to register the hooks” & “doesn’t cs-cart require a func.php” file and it would responsd. Yes you are right … then provide revised code/instructions. I had it writing five seperate files when it bombed out.

When I logged back in I got a completely different answer. Logged out and logged in again and got an answer close to where I started the first time. So I tried writing the question addressing many of the mistakes it made from my first attempt with the following question …

" Can you describe the steps and code required to display the last five vendors created and the number of products they own. Lets display them by appending a new data block to the TPL hook index in the admin panel dashboard template (design/backend/templates/views/index/index.tpl ). Lets call the add-on “last five vendors” and create the following files as part of the solution: addon.xml, init.php, func.php created in the app/addons/last_five_vendors directory, index.post.tpl created in the app/design/backend/templates/addons/last_five_vendors/hooks/index/ directory and index.post.php created in the app/addons/last_five_vendors/controllers/backend/ directory. The addon.xml file will need to create a table that holds the data to be displayed (drop table if it exists). The init.php file will need to register the needed hooks. The index.post.tpl file will display the data and the index.post.php file will collect the data to be displayed (from the table created within the addon.xml file)."

This addressed all mistakes it made the first time around that got me to a almost working add-on, but it still made mistakes (including not closing out the php code … ?> … and I gave up.

Unfortunetly I didn’t capture that code from my add-on attempt, but it was pretty close to working. I got a notification that it now remembers chat history but it didn’t when I tried so it is lost forever.

Why it repeats mistakes I don’t know … was it corrupted by other users (the first time they released after three days it thought it was god!) do the researchers deliberately make it make mistakes to see how different humans correct it (as part of it’s learning model) or is there multiple instances running?

1 Like

Just tried again to get it to write an add-on with the following question and it is the best answer I have (before trying to tell it correct it’s mistakes) … so maybe it is learning!

" Can you describe the steps and code required to display the last five vendors created and the number of products they own. Lets display them by appending a new data block to the TPL hook index in the admin panel dashboard template (design/backend/templates/views/index/index.tpl ). Lets call the add-on “last five vendors” and create the following files as part of the solution: addon.xml, init.php, func.php created in the app/addons/last_five_vendors directory, index.post.tpl created in the app/design/backend/templates/addons/last_five_vendors/hooks/index/ directory and index.post.php created in the app/addons/last_five_vendors/controllers/backend/ directory. The addon.xml file will need to create a table that holds the data to be displayed (drop table if it exists). The init.php file will need to register the needed hooks. The index.post.tpl file will display the data and the index.post.php file will collect the data to be displayed (from the table created within the addon.xml file)."

Here are the steps and code required to display the last five vendors created and the number of products they own in the TPL hook index in the admin panel dashboard template:

  1. Create the following files in the app/addons/last_five_vendors directory:
  • addon.xml: This file will define the add-on and create the table that holds the data to be displayed.
  • init.php: This file will register the needed hooks.
  • func.php: This file will contain any functions that you need to use in your add-on.
  1. In the addon.xml file, define the add-on and create the table that holds the data to be displayed. Here is an example of what the file might look like:
<?xml version="1.0" encoding="utf-8"?>
<addon>
  <id>last_five_vendors</id>
  <version>1.0</version>
  <priority>100</priority>
  <settings>
    <sections>
      <section>
        <id>last_five_vendors</id>
        <items>
          <item>
            <id>display_last_five_vendors</id>
            <type>checkbox</type>
            <variants>
              <variant>
                <id>Y</id>
                <value>Yes</value>
              </variant>
              <variant>
                <id>N</id>
                <value>No</value>
              </variant>
            </variants>
            <default_value>Y</default_value>
          </item>
        </items>
      </section>
    </sections>
  </settings>
  <schemas>
    <schema>
      <name>last_five_vendors</name>
      <table>
        <name>last_five_vendors</name>
        <columns>
          <column>
            <name>vendor_id</name>
            <type>int</type>
            <unsigned>true</unsigned>
            <notnull>true</notnull>
            <autoincrement>true</autoincrement>
            <primary_key>true</primary_key>
          </column>
          <column>
            <name>vendor_name</name>
            <type>char(255)</type>
            <notnull>true</notnull>
          </column>
          <column>
            <name>product_count</name>
            <type>int</type>
            <unsigned>true</unsigned>
            <notnull>true</notnull>
            <default_value>0</default_value>
          </column>
          <column>
            <name>timestamp</name>
            <type>timestamp</type>
            <notnull>true</notnull>
            <default_value>CURRENT_TIMESTAMP</default_value>
          </column>
        </columns>
      </table>
    </schema>
  </schemas>
</addon>


  1. In the init.php file, register the needed hooks. Here is an example of what the file might look like:
<?php

if ( !defined('BOOTSTRAP') ) { die('Access denied'); }

// Register the post-dispatch hook for the dashboard controller
Tygh::$app['view']->addHook('index', 'addons/last_five_vendors/hooks/index/index.post.tpl');

  1. In the func.php file, create a function to retrieve the data from the table created in the addon.xml file. Here is an example of what the function might look like:
<?php

function fn_get_last_five_vendors() {
  // Connect to the database
  $db = Tygh::$app['db'];

  // Select the last five vendors from the table, along with the number of products they own
  $vendors = $db->getArray("SELECT vendor_name, product_count FROM last_five_vendors ORDER BY timestamp DESC LIMIT 5");

  return $vendors;
}

  1. In the app/design/backend/templates/addons/last_five_vendors/hooks/index/ directory, create the index.post.tpl file. This file will display the data retrieved by the function in the func.php file. Here is an example of what the file might look like:
{*
  This template displays the last five vendors created and the number of products they own
*}

{if $display_last_five_vendors == 'Y'}
  <div class="last-five-vendors">
    <h2>Last Five Vendors</h2>
    <table>
      <thead>
        <tr>
          <th>Vendor</th>
          <th>Number of Products</th>
        </tr>
      </thead>
      <tbody>
        {foreach from=$vendors item=vendor}
          <tr>
            <td>{$vendor.vendor_name}</td>
            <td>{$vendor.product_count}</td>
          </tr>
        {/foreach}
      </tbody>
    </table>
  </div>
{/if}

  1. In the app/addons/last_five_vendors/controllers/backend/ directory, create the index.post.php file. This file will collect the data to be displayed and pass it to the index.post.tpl file. Here is an example of what the file might look like:
<?php

if ( !defined('BOOTSTRAP') ) { die('Access denied'); }

// Include the func.php file
require_once dirname(__FILE__) . '/../../func.php';

// Get the data from the table
$vendors = fn_get_last_five_vendors();

// Assign the data to the template
Tygh::$app['view']->assign('vendors', $vendors);

  1. In the TPL hook index in the admin panel dashboard template (design/backend/templates/views/index/index.tpl), include the add-on to display the data. Here is an example of how to do this:
{hook name="index:content_top"}
  {include file="addons/last_five_vendors/hooks/index/index.post.tpl"}
{/hook}

This is way off working, and it has repeated it’s mistake of not closing of php files. But again is a better starting point than my first attempt.

It also seems to know the cs-cart api but I haven’t asked it much about that.

1 Like

Well, it’s not a mistake, but rather, on the contrary, following the recommendations:

If a file contains only PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

So seems like it’s learning impressively fast :slight_smile:

1 Like

Well I didn’t know that … thanks.

Looking at the documentation it doesn’t learn, it remembers text from your chat (for the session at least), hence the improvements in one chat and starting again when I log back in. It seems the verability in answers I was getting was due to it ‘tempreture setting’ a setting of zero will result in no flexability in response, a setting of one maximum varability.

It’s data set was set in 2021 and it can’t take from other users or the internet. It is quite good with javascript, php and python and upto the character limit you can craft a question that it will code pretty well and it certainly has some cs-cart knowledge. Might be good for writing small chunks of code … not sure what the long term plan is!

3 Likes

Did anybody else tried the tool ?

I have tried. It really rocks.