How to programmatically add a product from a addon ?

I am writing an add on for Cs-cart and I need to dynamically add products to the catalog.

Now what I see is that fn_update_product function is in the controller admin/products.php instead of being in core function files or a separate file.

This means I cannot directly call this function from my addon or include a products.php file because its a controller.

Do I need to write my own function for this simple and basic operation ?

NB : Why functions are in controllers first of all, shouldn't it be in a separate file.

Isn't this a huge design flaw / bad design ?

Most people simply check if the product exists and then do an INSERT or UPDATE depending. It's not too complicated unless you want to handle the image pairs, options, features, etc. Then you'll have to do several queries or use functions in fn.catalog.php.

The fn_update_product() is ONLY used by the controller so no reason it shouldn't reside there. Granted it would make addon development easier if it were not imbedded in the controller, but I doubt that is a design goal of the cart. You won't find any kind of API for cs-cart either. I believe these are conscious decisions to generate more custom development by cs-cart team and to limit the support issues of foreign imports.

Depending on how and where you need to call the fn_update_product function, you may be able to do it by including the controller like this:

ob_start(); //Hook output buffer
include(DIR_ROOT . "/controllers/admin/products.php");
ob_end_clean(); //Clear output buffer

This will defined the functions in products.php for you. Beware, if the file has already been included by fn_dispatch() or will be included later (i.e. in cases where your code is called in a hook that runs before dispatch), then you will get “cannot redeclare function” errors. You can solve the first problem (where the file has already been included) by checking for the function before including the file. The second case is more complicated.

If you choose to use this method, it would be safest to unset $mode before you include the controller (and reset it back afterward) so that the controller doesn't actually do anything.

Thanks for the replies tbirnseth and iviable,

for now I have copied the whole function as a different name and have been using it without any issues.

@iviable : including a controller is a big “NO” “NO”

[quote name=‘shikhar’ timestamp=‘1320325036’ post=‘125240’]

@iviable : including a controller is a big “NO” “NO”


I completely agree that including a controller is bad practice. It’s also bad practice to include functions for a controller procedurally in the same file (as CS-Cart does) precisely because of the problem that you face :)

From both a style and technical standpoint, in normal circumstances I would never use the approach I suggested. In most systems it wouldn’t be necessary. In the case of CS-Cart, however, I think it’s the lesser of two evils.

When I code an addon, I think about the potential affect of future core code changes.


[]The structure of controllers may change and code may be executed independent of the $mode variable (which would be run every time you include the file).

]The fn_update_product function may change materially.


In either case, the addon could have undesired affects. Judging from changes in previous versions, I think that the second case is more likely, so I would personally choose to include the controller. I can totally understand the choice to duplicate the function instead - especially if you’re not looking to future-proof the addon.

Rest assured, I don’t normally code with like this! In fact, I only discovered this method when I had the exact same issue as you - I needed to execute a function that was defined in a controller. I do wish the code was more addon friendly…