Can Someone Please Explain Hooks?

Can someone please explain 2.x hooks in plain and simple language?

I will give this a shot.



"Hooks’ is a system for substituting (or amending) one block of code with another. The hooks occur in the standard code and check to see if another template exists to modify the existing code. The structure of the hook defines the subdirectory and TPL name to look for in the hooks directory.



As an example, I will use the /skins/YOURSKIN/customer/views/products/view.tpl. You will find the following block from lines 5 to 14:

```php [COLOR=“Red”]{hook name=“products:view_main_info”}[/COLOR]


[COLOR="Purple"]

{include file="views/products/components/product_images.tpl" product=$product show_detailed_link="Y"}
[/COLOR]
[COLOR="Blue"]

{include file="views/products/components/buy_now.tpl" product=$product but_role="action" show_qty=true show_sku=true obj_id=$product.product_id}
[/COLOR]

{/hook} ```

Based on the hook, the code will check to see if the file /products/view_main_info.ACTION.tpl exists. If it does, its contents will either replace or amend as below based on the ACTION portion of the filename:

overwrite = replace the code in the original hook with the code from the hooked file;
pre = process the contents of the hooked file before the processing the code in the hook block;
post = process the contents of the hooked file after the processing the code in the hook block.

For this example, we have a file called /products/view_main_info.overwrite.tpl which reverses the the divs in the original hook:
```php

[COLOR="Blue"]

{include file="views/products/components/buy_now.tpl" product=$product but_role="action" show_qty=true show_sku=true obj_id=$product.product_id}
[/COLOR]
[COLOR="Purple"]

{include file="views/products/components/product_images.tpl" product=$product show_detailed_link="Y"}
[/COLOR]

```
The substituted code will display the product block and tabs above the product image.

The problem remaining is where these hooked files should be located.

Using tbirnseth's Local Configuration add-on ([url]http://forum.cs-cart.com/showthread.php?t=11656)[/url], the hooked file would be located at /skins/YOURSKIN/customer/addons/local/hooks/products/view_main_info.overwrite.tpl.

Since we have no documentation on the "My changes" add-on from CS-Cart, we cannot be certain if it will work exactly the same way.

Hopefully, this explains this a bit clearer.

Bob

[quote name=‘jobosales’]

Hopefully, this explains this a bit clearer.



Bob[/QUOTE]

Thanks, and I’m sure it was a GREAT explanation . . . I’m just too stupid to understand it. Is there someone who can sum this up in a nutshell and explain it for the common man? My focus is more on selling products, not coding.

The executive summary:



Hooks provide a means to modify the way your cart works without altering the base files provided in the distribution. The advantage to this approach is that you will not need to resolve conflicts in the base files when upgrades are made available.



Bob

ok, but are hooks something I create or something already created inside of CS-Cart that I just tap into?

[quote name=‘knoxbury’]ok, but are hooks something I create or something already created inside of CS-Cart that I just tap into?[/QUOTE]



The hooks themselves already exist in the distributed code. You make use of them by creating your own separate files with your changes.



It took me a bit to wrap my head around it, too. It is really quite a powerful addition to CS-Cart and also means we should not be needing to constantly reconcile our mods to new releases.



Bob

Thanks jobo, Now this is cool.