Creating A Functional Button

Hello, I've recently started to create an addon called groupings. One problem I had was creating a button that collects information and stores it in the database. I am very green with CS-Cart and development. The advanced addon tutorial lacks information and documentation of the process.



I have a database named groups which is created on installation of the addon. In the function php part of the addon, I have a function that adds product id and user id to the database. I added a button to the product page but I do not know how to make it so that upon clicking the button, it calls the addon function that adds the user id and product id of the page it was called from.



this is the func.php:

```php

if(!defined('BOOTSTRAP')) { die('Access denied'); }
function fn_groupings_add_group()
{
$product_id = $smarty.request.product_id;
$user_id = $_SESSION['auth']['user_id'];

db_query('INSERT INTO ?:groups VALUES ($product_id, $user_id)');
}
?>

```



Other than the button, is this the correct way to get the product id and the user id?



Thank you very much in advance.

You should create a new controller (e.g. new_controller) where the function will be called and add the following URL to your custom button:



MY BUTTON

Could you guide me through how to make a new controller? Looking at the documentation, I am lost on how to create one for the addon and the content of a controller.



For example, I've created a main_info_title.post.php in the addonname/controllers/common/…

I am not sure what a controller is and what its function is and how to use it for my purpose.



Thank you very much, I am very new to cs-Cart and hope to use it for my website.

Please check this article:



http://docs.cs-cart.com/4.2.x/core/controllers/index.html

I appreciate your help by the way!



I've read it multiple times, but that documentation is a bit difficult to comprehend.

Here is what I tried:

I created a controller in the addons/*addonname/controllers/common/groupings.php

I added


BUTTON
[color=#000088][size=2][color=#222222][size=4]
[/size][/color][/size][/color]

Clicking on it gives: Error Oops something went wrong (Not Found). Please try again.

It seems like it cant find the controller. Is there something else I need to setup?



The controller content is this:

```php


if (!defined('BOOTSTRAP')) { die('Access denied'); }
use Tygh\Registry;
if($mode == 'button')
{

}
exit;
?>

```



I don't know what to put it in it yet.



How do I retrieve the data? $_Request['product_id'] does that return the product?



And finally, (lol sorry for so many questions) is it possible to make the button also return the quanity of the product too?

Oh oops, there is no edit function in the forum?



I'm guessing the data can just be retrieved from $product_id and $user_id since the values are being put in from the code in the button. Sorry about that!

Yes, you can use the $_REQUEST['user_id'] and $_REQUEST['product_id'] to receive the corresponding values.



To debug the code, please do the following:



Replace:



if($mode == 'button')
{
}




with



if($mode == 'button')
{
fn_print_r($_REQUEST);
}




And not that the correct add-on URL should be:



[color=#282828][font=arial, verdana, tahoma, sans-serif]app/addons/*addonname/controllers/common/groupings.php[/font][/color]



and call the URL from the browser address bar

Since I am running it locally, typing in



localhost/site/[color=#282828][font=arial, verdana, tahoma, sans-serif]app/addons/*addonname/controllers/common/groupings.php [/font][/color]



[color=#282828][font=arial, verdana, tahoma, sans-serif]gives me an “Access forbidden! You don't have the permissions to access the requested object. It is either read-protected or not readable by the server. Error 403”[/font][/color]



[color=#282828][font=arial, verdana, tahoma, sans-serif]I've tried many things to try to make it work but nothing had it working.[/font][/color]



[font=“arial, verdana, tahoma, sans-serif”][color=“#282828”]I tried removing [/color][/font]

[font=“arial, verdana, tahoma, sans-serif”][color=“#282828”]
if (!defined('BOOTSTRAP')) { die('Access denied'); }
use Tygh\Registry;
[/color][/font]



[font=“arial, verdana, tahoma, sans-serif”][color=“#282828”]Changing the Xampp config, etc[/color][/font]

I just keep getting the not found error. It doesn't give the error when I put it in apps/controllers/common. Why isn't cs-cart recognizing my controller in the addon?

Make sure that the module is installed and has the Active status

If by module you mean add-on, it is definitely installed and has the Active status. I can confirm it is working since I can see the button from the addon. I can't find any typing errors or anything.



Everything is in the right place. For example:

C:\xampp\htdocs\site\app\addons\myaddon\controllers\common\myaddon.php



calling


MY BUTTON


would return the not found addon. What is interesting is that even if I use different controllers such as watermarks.create, it also gives the same error. It does not return an error if I use a controller from app\controllers\common

Anybody have any idea?

Edit: Sorry for the triple post,



It seems that having both “frontend” and “common” with the same controller php file makes the error go away



Anyways, In the controller, I call this function which is in the func.php


function fn_addon_add_group($product_id, $user_id, $quantity)
{
db_query('CREATE TABLE `?:?s` (
`user_id` int(11) unsigned NOT NULL default `0`,
`amount` mediumint(8) unsigned NOT NULL default `1`,
PRIMARY KEY (`user_id`)
) Engine=MyISM DEFAULT CHARSET UTF8 IF NOT EXISTS ?:?s;', $product_id);
db_query('INSERT INTO ?:?s VALUES ($user_id, $quantity)', $product_id);
}




Does this code work? It is not creating any new tables according to phpmyadmin. It is supposed to create a table with the name cscart_productid if it doesn't exist for it already. Then inserts the data into the table.

SOrry fourth!



I fixed the code to in func.php:


function fn_addon_add_group($product_id, $user_id, $quantity)
{
db_query('CREATE TABLE IF NOT EXISTS `?:'.$product_id.'` (
`user_id` int(11) unsigned NOT NULL default `0`,
`amount` mediumint(8) unsigned NOT NULL default `1`,
PRIMARY KEY (`user_id`)
) Engine=MyISM DEFAULT CHARSET UTF8;');
db_query('INSERT INTO ?:'.$product_id.' VALUES($user_id, $quantity)');
}


and i call this function in the controller with this:


if($mode == 'button')
{
fn_multiless_add_group($_REQUEST['product_id'], $_REQUEST['user_id'], 0);
}




At the end, when I press the button, I don't see any database named: cscart_30 or others like it.

Are the functions from func.php available to the controller file? I don' understand how everything connects.



Sorry for this long thread…

Yes, the database functions are available in the custom controllers. If you provide us with the temporary FTP access, we can look into the issue.

I'm developing this addon on a local server sorry.



I'll clarify my questions:

  1. How do I check if the controller is being called?
  2. Why do I need both frontend and common folder for the controllers for cscart to find them?
  3. How do I check if any of my files are not working or returns an error because of wrong syntax and such?



    Thank you again.

Also, What does


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


check for? The init.php, func.php, and controllers have them

[quote name='wjdwndud' timestamp='1435601912' post='220894']

  1. How do I check if the controller is being called?

    [/quote]



    Add the following line of code to the controller:



fn_print_die($_REQUEST);




And run the following URL in the browser



http://you_domian.com/index.php?dispatch=myaddon.button&product_id=111&user_id=222



[quote name='wjdwndud' timestamp='1435601912' post='220894']

2. Why do I need both frontend and common folder for the controllers for cscart to find them?

[/quote]



It is not necessary condition. Looks like permissions issue.


[quote name='wjdwndud' timestamp='1435601912' post='220894']

3. How do I check if any of my files are not working or returns an error because of wrong syntax and such?

[/quote]



In the config.local.php file replace:



error_reporting(E_ALL);
ini_set('display_errors', 0);




with



error_reporting(E_ALL);
ini_set('display_errors', 1);
define('DEVELOPMENT', true);



[quote name='wjdwndud' timestamp='1435611845' post='220903']

Also, What does


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


check for? The init.php, func.php, and controllers have them

[/quote]



This string is required to restrict direct access to the file. E.g.



http://domain.com/app/addons/myaddon/controllers/frontend/myyaddon.php

Thank you very much for your help! I appreciate it. This community is very nice and useful.

I won't hesitate to use eComLab's services!