Hooks Question

I am reading about hooks in the knowledge base here: [url]http://docs.cs-cart.com/common.php?dispatch=docs/view&node_name=addon-connection[/url]



It says in the first step


[quote]In order to create a hook it is necessary to do the following:


  1. Create the hooks directory (if it isn’t there) in the addon directory.

    [/quote]

    Just so I understand, say I am creating an addon called “bubbles”. I put the directory “bubbles” in www/addons/… Then I create a directory called “hooks” inside of “bubbles” like so:



    www/addons/bubbles/hooks/



    Right? Or do I put the hooks directory here:



    www/addons/hooks/



    Or somewhere else?



    In the seconds step it says:

[quote]In the hooks directory create a directory and give it the name of the controller where the hook will be called.[/quote]



Does this mean if my hook is going to be located in the products.php directory then I need to make the directory named /products/



Thanks for any help.

I use the hooks with the My Changes addon. I thought about trying to type out my layout, but I feel a screen shot might be better.







Once the directorys are made you can then put your files into them.



So if you wanted to change the bottom you would put the file:



bottom.override.tpl



In:



skins/your-skin/customer/addons/my_changes/hooks/index/



I hope this helps. If anything isn’t clear let me know and I’ll try to help out more.



Brandon

Ok, now I am a little bit confused. On this page [url]http://docs.cs-cart.com/common.php?dispatch=docs/view&node_name=cscart-architecture[/url] it says there are TH and CH.



What if my change will require more than just a .tpl file change? My change will mostly involve .php code. Is there a my_changes for actual functionality changes? What would be my approach for creating php changes so that they won’t be overwritten when I do an update?



I am guessing I want a CH – code hook, but then, where exactly do I put this code so it isn’t overwritten when I do an update. For instance, say I want to put a CH inside of the products.php file to hook to my bubbles add-on and then later transfer control back to products.php. Say I put it on line 2500 of the products.php file. How do I know this line won’t be overwritten by some new code from CS-Cart when I do an update later?

So I am trying to follow CS-Cart’s instructions here by following their use of the fn_set_hook function. I am noticing that they seem to be loosely followed. Unless of course, I am not understanding, which is possible.



At the bottom of the fn_update_product function inside of /www/controllers/admin/products.php you see the line:


fn_set_hook('update_product', $product_data, $product_id);



Now from their explaination:


[quote]

“Knowing the hook name, any addon can intercept execution of the program code, perform necessary manipulations and then return the execution process to the Main program (MP). When calling a certain hook, all variables directly defined in this hook become available for the addon.



Call of the hook within the addon is carried out by defining the function which name consists of the prefix “fn_”, addon name, underscore and the full name of the called hook.”[/quote]



From this, I guess I am looking for something like fn_xxx_update_product() in the /www/addons/ directory right?? How do I know which one it is? When I do a search I see



fn_bestsellers_update_product

fn_seo_update_product

fn_tags_update_product

fn_discussion_update_product



also if it matters



fn_update_product_price

fn_update_product_amount



How am I, or cs-cart supposed to know which of these functions are being called by the code hook?

woah woah woah… is this calling all hooks in the addons directory that have a function declared in this standard … fn_xxx_update_product() ???

Are you trying to make your own hooks or use the ones already built into CS-Cart? I’ve only ever used the built in ones so I couldn’t really help you with making new ones.



If you are trying to use the built in ones than you are seriously overcomplicating things.



To see how I changed my header using hooks you can check out:



[url]http://forum.cs-cart.com/showthread.php?t=14070[/url]



I hope this helps. If you are trying to do something different than you can just ignore my post.



Brandon

[quote name=‘brandonvd’]Are you trying to make your own hooks or use the ones already built into CS-Cart? I’ve only ever used the built in ones so I couldn’t really help you with making new ones.



If you are trying to use the built in ones than you are seriously overcomplicating things.[/QUOTE]



Brandon, thanks for your reply.



I am trying to make a CH – Code Hook, not a TH – Template Hook. It seems a TH is what you are describing … please correct me if I am wrong…It also seems that Template Hooks are all that most people on the forums have needed to complete any add-ons. But alas, I do not need to modify any of the front end layout, or any .tpl file, or any other presentation layer files.



I need to modify the way CS-Cart actually works with the data. I need to modify the php execution. To put it simply, I need to intercept (or hook) the (MP) – Main Program code and then do some changeroos to the data, and then return the control to the MP. Basically, when a product is added from the admin back end, I have a bunch of PHP code that has to be run on the data the user puts in before it is eventually inserted into the database.



I have already written the various functions to accomplish what I need, and I put the code in the products.php file in the /controllers/ directory. However, I am not interested in accomplishing my task like this because eventually I will want to update my cs-cart site to 2.0.10 and then other updates after that. So I was hoping I could use what CS-Cart calls CH – code hooks, not TH – template hooks because according to CS-Cart, these CH won’t get overwritten by and update of the cart. I hope this makes sense, but yeah… hoping someone has done something like this…

Oh, ok. Yeah I won’t be any help, sorry. Maybe Sno will chime in on this one. I would be willing to bet that he can help.



Brandon

Ok, thanks again Brandon, you’ve definitely been a help in getting me here. Must respect.



Anyone else wanna chime in? … Jobosales… Sno… I know you guys are out there! Haha, just kidding. I hope you guys can help, I’m on the verge of making my idea a legit add-on I think. Just need this lil’ bit of info so I can move forward.



Thanks for any help.

I was looking for the same thing, and came across your thread.

Just figured it out with the help of the links here, so I thought I’d share:


  • Make a new directory in the addons dir with the name of your addon, eg. /addons/fixed_shipping/
  • Create an init.php file with an array of the hooks you want to access:

    ```php
    if ( !defined('AREA') ) { die('Access denied'); }

    fn_register_hooks(
    'calculate_cart'
    );

    ?> ```
  • Create a func.php file with a function named fn_youraddon_[I]yourhook/I :

    ```php
    function fn_fixed_shipping_calculate_cart(&$cart, $cart_products, $auth, $calculate_shipping, $calculate_taxes, $apply_cart_promotions)
    {
    $cart['display_shipping_cost'] = $cart['shipping_cost'] = $20;
    }

    ?> ```

    -Create an addon.xml file with the name of your addon, and id/directory name:



fixed_shipping
Fixed Shipping
1
0
active




  • Go to your CS-Cart Administration → Addons, and hit ‘install’ next to your addon name
  • That’s it!



    This addon is just an example - I am aware that you can get fixed shipping without an addon :slight_smile:

Hi,



I see you have common_templates, my_files and css folders under my_changes. How do you use that? Thanks!






[quote name=‘brandonvd’]I use the hooks with the My Changes addon. I thought about trying to type out my layout, but I feel a screen shot might be better.







Once the directorys are made you can then put your files into them.



So if you wanted to change the bottom you would put the file:



bottom.override.tpl



In:



skins/your-skin/customer/addons/my_changes/hooks/index/



I hope this helps. If anything isn’t clear let me know and I’ll try to help out more.



Brandon[/QUOTE]

I just have my folders like this to help keep me organized. You can set them up differently, but I don’t want to have to hunt around looking for my files so I did this.



I use the system in a couple of ways. First for my local_styles.css it works like:



In skins/basic/customer/addons/my_changes/hooks/index



I have a file called styles.post.tpl This file comes after my other style sheets are pulled up. In that file I have:






So above, you can see how my local_styles.css is called. Now because I use images in my style sheet I also have to have a folder for the images so that is why I have the folder:



skins/basic/customer/addons/my_changes/css/images



Now another example of why I have a folder is with my skins/basic/customer/addon/my_changes/common_templates



I have a file called:



skins/basic/customer/addons/my_changes/hooks/index/bottom.override.tpl



In this file I have:


```php {* $Id: bottom.override.tpl 7497 2009-11-20 10:41:21Z Brandon $ *}















{$lang.quick_help} {$lang.my_account} {$lang.information}
{include file="addons/my_changes/common_templates/quick_help_bottombox.tpl"} {include file="addons/my_changes/common_templates/my_account_bottombox.tpl"} {include file="addons/my_changes/common_templates/info_bottombox.tpl"}


{$lang.copyright} © {if $smarty.const.TIME|date_format:"%Y" != $settings.Company.company_start_year}{$settings.Company.company_start_year}-{/if}{$smarty.const.TIME|date_format:"%Y"} {$settings.Company.company_name}. {$lang.powered_by} J and K Online - Saltwater To Go is UpFront



```

As you can see there are three files called up from my common templates folder. Originally I just put these in my skins/basic/customer/common_templates folder, but since there are quite a few files in that folder it was hard for me to find them if I wanted to make changes so I made this new folder.

Like I said, it is basically about organization and doing what makes it easiest for me to find and edit my files.

I hope all this answers your questions.

Brandon

Perfect, thank you.

maybe I’m missing the point, but where can I find a list of what functions are available to pass to fn_register_hooks? As I understand it, fn_register_hooks takes a list of pre-defined internal functions in cs-cart and passes control to my addon. If that’s true, I need a list of those functions and the parameters they take, but I can’t seem to find it.

[quote name=‘frednurk’]maybe I’m missing the point, but where can I find a list of what functions are available to pass to fn_register_hooks? As I understand it, fn_register_hooks takes a list of pre-defined internal functions in cs-cart and passes control to my addon. If that’s true, I need a list of those functions and the parameters they take, but I can’t seem to find it.[/QUOTE]



[url]http://docs.cs-cart.com/common.php?dispatch=docs/view&node_name=cscart-architecture[/url]



Does that help you?

thanks, but I’ve already read that, and the relevant part seems to be:


[QUOTE]Knowing the hook name, any addon can intercept execution of the program code, perform necessary manipulations and then return the execution process to the Main program (MP). When calling a certain hook, all variables directly defined in this hook become available for the addon.[/QUOTE]



The problem is, I don’t know the hook names, or where I can find them and the parameters they take.

Of course, I could be totally misunderstanding this, but I’ve read the docs 5 times and I can’t figure it out.

Ok, maybe I am now misunderstanding what you are trying to do. Let me explain what I understand about fn_register_hooks



I think you can pass any function with any parameters to it because it essentially generates a breakpoint in the code and then allows whatever function with however many parameters that you specific to be generated as well. And that function can be used at that time for purposes of “intercepting” what is going on.



So basically you register some function that you have already written through fn_register_hooks and then later, you will know the function because you already set it, so you can call it when the fn_set_hook is called.



I can’t be exactly sure of all of thise, because a while back I was really digging into this and got sidetracked with the new updates and new features, and I have become a little obsessive about learning about how the Database works. But I assume one day I will get back to this.

I don’t think that’s true. I’m trying to learn the ropes by making an addon called ‘category_news’ that can attach a single news item to a category. I call fn_register_hooks(‘update_category’) and it gets called when a category is updated. My called function is then named ‘fn_category_news_update_category’ as per the limited documentation.

It seems that the function “update_category” has to exist somewhere in the cscart core, or I can’t hook into it. To figure out the args for this, I looked in the core source code, and I found where fn_update_category was defined and what args it took. That seems like a very hard way to find out the list of possible hooks and args. If what I’m saying is true, it is necessary to have a list of all the possible hooks, but there is no such list.

If I was just defining random code hooks and then calling them, it wouldn’t work, because the whole point of ‘hooks’ in general in any api is to allow one to hook into certain actions that have already been built. If I don’t have a list of the actions that can happen in cs-cart, how do I build that list?

I inserted some code into the core that outputs all the hook functions that addons ask to be attached to, and here is the list. Is it complete? What are the args for those functions? What am I missing?


[QUOTE]pre_add_to_cart

generate_cart_id

get_cart_product_data

calculate_cart

delete_cart_product

reorder

delete_product

clone_product

update_product

update_page

delete_page

clone_page

get_additional_product_data

get_page_data

get_pages

get_products

get_users

seo_is_indexed_page

is_accessible_discussion

get_discussion_object_data

get_discussion_objects

get_block_locations

localization_objects

search_init

save_log

sitemap_item

sitemap_link_object

seo_is_indexed_page

get_seo_vars

seo_link

convert_tpl_url

convert_php_url

validate_sef_object

revisions_publish

revisions_delete_objects

revisions_create_objects

revisions_clone

revisions_get_data

create_revision_tables

revisions_delete

place_order

delete_product

get_products

place_order

get_order_info

change_order_status

order_notification

delete_order

delete_cart_product

generate_cart_id

calculate_cart

exclude_products_from_calculation

form_cart

allow_place_order

save_cart

extract_cart

get_cart_item_types

is_cart_empty

exclude_from_shipping_calculation

get_orders

pre_add_to_cart

delete_cart_product

init_secure_controllers

get_status_params_definition

get_manifest_definition

get_google_codes

apply_google_codes

form_google_codes_response

google_coupons_calculation

get_google_add_items

reorder

init_secure_controllers

get_banners

delete_banners

search_by_objects

init_templater

get_additional_product_data

get_product_data_more

pre_add_to_cart

delete_cart_product

get_products

calculate_cart_items

place_order

update_profile

update_product_amount

change_order_status

delete_product

get_products

products_sorting

update_product

get_product_data

delete_page

update_page

get_page_data

page_object_by_type

clone_page

delete_page

update_page

get_page_data

clone_page

page_object_by_type

user_init

convert_tpl_url

localization_objects

get_products

get_user_info

get_categories

update_profile

update_product

delete_product

update_category

delete_category

delete_order

update_news

delete_news

update_page

delete_page

update_event

delete_event

clone_product

get_product_data

get_products

get_categories

get_pages

fill_user_fields

get_gift_certificate_info

user_init

init_user_session_data

pre_add_to_cart

get_carts

get_products

get_additional_product_data_before_discounts

pre_add_to_cart

generate_cart_id

get_cart_product_data

place_order

pre_place_order

get_status_data

buy_together_pre_add_to_cart

buy_together_restricted_product

pre_add_to_wishlist[/QUOTE]

I am a little behind you still on this… Let me ask you two questions.



This may sound odd to you, because you are farther ahead than me, but how would you know any function’s args without looking for where it is defined in the code?



I think I am understanding where you are coming from a little. So you are saying you want to stop the execution of the fn_update_category function and you need to know what args/parameters are passed to this function.



I am quite interested in this now, so I did a search of the core directory and foudn what you found, two instances of ‘update_category’ , both located in fn.catalog.php.


function fn_update_category($category_data, $category_id = 0, $lang_code = CART_LANGUAGE)

and a little later inside the actual fn_update_category function…

fn_set_hook('update_category', $category_data, $category_id, $lang_code);



This kind of makes sense… I am going to keep looking.

you’ve hit on the problem precisely! How would I know the functions args without looking in the code? How do I even know the function exists? If this is indeed how the addon system works, where is the documentation?

Either I’m completely misunderstanding this process or the documentation is completely useless. I’m totally fine if it’s me being an idiot, but I’m pretty confused.

I’ve written a number of smarty-like template languages, and I’ve built a few APIs and used a few more that I haven’t written, and this ‘hook’ thing is a pretty common concept. In order for it to work, you need to have a list of places in the code that you can hook into though. Where’s that list? The documentation for developers seems awfully skimpy.