Creating own addon help

Hello,



This is for the latest cs-cart version.



I’ve been working through the structure of cs-cart and have gotten so far. The way I understand it there are 3 ways to make changes the first being editing template files (bad for upgrades), the second is to override template files in a generic addon (usually referred to as my_changes) and the 3rd is to create your own addon for each piece of functionality.



It is the 3rd method I am interested in for my own knowledge but of course it is also the hardest.



In my case all as I want to do is add a “Request a Quote” button underneath the “Add to cart” button on the product pages and the product listing pages.



There is no PHP coding really required for this except in the URL I’ll need add “?item=[the product id]” but I can handle that part in the template so it’s still not coding per se.



What I am unsure of is the template file itself so if someone could show me how to set it up I would appreciate it.



Here is what I have so far:



Under addons/request_quote/ I have addon.xml. Of that the only questions I have are:

Request a quote
0
0
active


Request a Quote



Do I need to do anything with or ? I’m guessing “no” because there is no code to process which is why I set them to zero. In the file I got them from they much larger numbers.



I am assuming that “request_a_quote” will be automatically accessible in the .tpl files via “$lang.request_a_quote” - please let me know if this not is correct.



Under addons/request_quote/controllers/customer/ I have: init.post.php which has:


```php


if ( !defined(‘AREA’) ) { die(‘Access denied’); }


Registry::get(‘view’)->assign(‘request_quote’, array( ‘allowed’ => 1));


?>
```
As you can see it doesn't do anything at the moment except the first line which seems to be universal and I assume is a line of security code. The 2nd line is just commented out. I don't think this file is needed but I am keeping there just as placeholder (as a reminder) in case I find I do need some coding.

The last file I'm working on and haven't figured out at all is under: skins/basic/customer/addons/request_quote/ and all as I got is: view.tpl.

Of this I just copied over "add_to_wish_list.tpl" which is a single button like this:

```php {include file="buttons/button.tpl" but_id=$but_id but_name=$but_name but_text=$lang.add_to_wishlist but_role="text" but_onclick=$but_onclick but_href=$but_href}
```

For readability here I'll split that line up (and change the button text to mine) and in doing so we get:

```php {include file="buttons/button.tpl"
but_id=$but_id
but_name=$but_name
but_text=$lang.request_a_quote
but_role="text"
but_onclick=$but_onclick
but_href=$but_href
}
```
And here is where I am stuck because I don't have a clue where $but_id, $but_name etc. is coming from and therefore if I need to do anything with them. Also, perhaps I've used the wrong template to get a button and there is a better way. And, I probably have to setup some hooks to put this in the right place once the template is coded correctly... so as you can see I'm a bit lost at the moment.

Can anyone give me a direction on this? Help appreciated!

I understand that setting up a whole addon by itself for just a button is almost certainly overkill but it's as much an exercise in understanding creating new addons as it is creating the functionality.

Reg

Anyone?

[quote]

Do I need to do anything with or ? I’m guessing “no” because there is no code to process which is why I set them to zero. In the file I got them from they much larger numbers.

[/quote]

No

[quote]

I am assuming that “request_a_quote” will be automatically accessible in the .tpl files via “$lang.request_a_quote” - please let me know if this not is correct.

[/quote]

correct



Why would you add code to the init controller rather than the products controller if it is the products you want to deal with. Why wouldn’t you just reference this in smarty code where you need it as {if $addons.request_quote.status == ‘A’} which would say the addon is enabled or not.



The ‘views’ of the skin pages are called following a call to the addon controller. I.e. when ?dispatch=products.view&product_id=123 is called, the products controller sets the variables to be used by the view and then processes the appropriate template for the “mode” of the controller. I.e. skins/basic/customer/views/products/view.tpl



For a custom controller like yours There’s no need to do any of this since you’re really not managing anything.



So in whatever template you want to do your thing, you should just do:


{if $addons.request_quote.status == 'A'}
{include file="addons/request_quote/includes/my_button_file.tpl"}
{/if}




Then in skins/basic/customer/addons/request_quote_includes/my_button_file.tpl add the include of the button code. The ‘but_id’ is the ID of the span tag or whatever the container is for the button. ‘but_name’ should be something like:


dispatch[controller.mode]


where controller is whatever controller you want to redirect them to and mode is the mode of the controller.



If all you want to do is add a link to a product page or something similar, look at the template for the product listing you want to use and look for a hook in the place you want to add your button or link. But you’re going to need to develop the page where the customer is to be redirected to.



I think you are overcomplicating for what you really want to achieve. But since I don’t know what the button is supposed to do (go to a form I assume) hard to advise how to do it.



I think your concept of how things are modified in cs-cart is off somewhat. Addons are completely different than template modifications. Hooks are used in both templates and PHP code and enable the “core” to be extended or customized in both cases. Addons should have a distinct functionality and are used when some degree of “management” is needed in either the admin or customer areas.

[quote name=‘tbirnseth’]Why would you add code to the init controller rather than the products controller if it is the products you want to deal with. Why wouldn’t you just reference this in smarty code where you need it as {if $addons.request_quote.status == ‘A’} which would say the addon is enabled or not.

[/QUOTE]



I never thought of init as a controller so I just learned something. I just thought of it as a place where you do initialization (i.e.: before other stuff) so that’s something I learned right there.



As an overview of where my ideas have come from so far… my methodology is primarily based on the wishlist addon because it does what I want to do which is add a button near the “add to cart” button and in a good way.



That is, it does it as far as I can see totally within itself and without overriding another template. That is, it just adds it’s piece in where it needs to go but in a more sophisticated way than just copying over a template, hacking it, and overriding the original template with the hacked one (as is suggested as a technique with the my_changes addon at times). So I want to learn exactly how it’s achieving what it’s doing so I can use that technique as and when needed.



I’m going through everything you wrote now so if I have any more questions I’ll let you know soon enough. Hopefully this is enough for me to make it work.



Thanks!

Thanks, it’s working now. I didn’t do exactly as you showed but pretty close. The only differences are really I was borrowing heavily from wishlist.



So for anyone else it came down to needing only two files.



The first is the mandatory “addon.xml” file and the second was “buy_now.pre.tpl” in the dir.: skins/basic/customer/addons/request_quote/hooks/products



And the code is:

{if !$hide_request_quote_button}
{if $auth.user_id != "0"}
{if $controller != "categories"}
{include file="buttons/button.tpl" but_id="button_request_quote_`$obj_prefix``$product.product_id`" but_name="button_request_quote_`$obj_prefix``$product.product_id`" but_text=$lang.request_a_quote but_role="text" but_onclick=$but_onclick but_href="http://www.my-website.com/quote_request.html?part=`$product.product_id`"}
{/if}
{/if}
{/if}




However, there is one question I still have, in the wishlist file there was this code:


{if !$hide_wishlist_button}
{/if}




A search through all the code for “hide_wishlist_button” did not show anywhere that it gets defined. This leads me to believe either there was something wrong in my search (which I doubt but it’s possible) or it get defined automatically through code.



If the latter is the case then I would assume that “$hide_request_quote_button” would also get automatically defined but I have no proof of that. Can someone tell me if my assumption about this variable is correct or not?



A few thing threw me and most of it came from preconceived ideas based on other complex systems I work on. Also I thought I had the addon installed and active but it wasn’t. I don’t know if it didn’t like something I did and uninstalled itself or if through the learning curve I just missed installing it.



One nice thing I learned which you can see from the above, you don’t need any files of PHP code at all if all you as you are doing is hooking into a template file. I thought you would need at least an init.php or such for some basics.

I found out one more thing for the newbie. Uninstall (of an addon) will literally remove your files from the active skin so don’t do it until you copy your code to var/skins_repository/base as it will install from there.

One more… it can be really stubborn about PHP sessions so if all things being equal you, let’s say turned off something and then it won’t come back on or in my case I think it was the uninstall I was testing. Log out of everything and then log back it and it might magically work. Perhaps somewhere on the interface there is a “clear cache” or something that fixes that but I hadn’t found it yet.

You don’t need an addon for what you’re doing. It can all be done in the existing my_changes addon. Why have the overhead fo an addon for what you’re doing. It’s kind of a waste. If you want to do a real addon then your template files should be created in var/skins_repository/base/customer/addons so that an uninstall will remove your files and an install will install them.



Regarding the hide_wishlist. Remember that cs-cart adds code to the cart to support their custom development team. So you will many times find things in the core with no reference to other things but when they deliver the addon to the client, they don’t have to provide modified core files to support it. It’s a luxury they have in being the authors.



You need to spend some time in the code learning how things work. Session info is critical to many things. Logging out destroys the current session so yes, things like unsaved carts will be lost.

The product inquiry addon can easily be modified and used as a request a quote addon, or you can go all the way and get the quote / invoicing addon - Sno

@sno - I’m guessing this is really more of a training project for @reg

[quote name=‘tbirnseth’]You don’t need an addon for what you’re doing. It can all be done in the existing my_changes addon. Why have the overhead fo an addon for what you’re doing. It’s kind of a waste.[/QUOTE]



Thanks but I made it clear that this was as much an exercise in making an addon as anything else.



Also, if you put more than one thing in “my_changes” how do then turn one off without the other? And, what is going to look better to the client, a “my_changes” you turn off and on or a “Request a quote” addon you can turn on and off? So, in the long run I consider it necessary to have an addon per independent functionality even if it is only one file.

[quote name=‘snorocket’]The product inquiry addon can easily be modified and used as a request a quote addon, or you can go all the way and get the quote / invoicing addon - Sno[/QUOTE]



Cool, thanks. It’s done now but I’ll probably be doing more addons so this is really good to know.

[quote name=‘tbirnseth’]@sno - I’m guessing this is really more of a training project for @reg…[/QUOTE]



Spot on!

If you are planning on doing more than one of these tiny changes, then why not just have a checkbox in my_changes/addon.xml that says whether it’s on or off. You can then easily check at run-time in the templates. Otherwise you’re going to add 10 addons over time that basically do nothing other than be loaded in every page load and processed individually.



Odd that you seem to ask for help, but most of your responses seem to be more about justifying what you have done versus really reviewing the advise by others who have gone before you.



i’m done.