Pre-filled product option through URL parameter

Hello everyone.



I’m about to sell a special version of a personalized product in my store and would like to add a feature where a text box or text area option in the product page takes its contents from a URL parameter.



Let’s say that I have a ‘Name’ text box option for my product. If the customers clicks on a link formatted as www.mystore.com/myproduct.html?name=John+Doe, then the ‘Name’ option would be pre-filled with ‘John Doe’.



This is extremely easy to do in PHP (via echo $_GET[‘name’]) if I had direct access to the form, however I have no idea where to hook this so it goes to the correct option field. Also I checked the page source and option fields take on numeric names such as “option_13_8”, so I feel that if I somehow reference those field names, things may break up in the future if that name ever changes.



Any ideas on how to accomplish the above?

You have to hook into the controller that builds the product information array, iterate through the product_fields until you find the one you need (compare labels, primary keys in the DB, whatever) and set it's value.

Okay, after a few hours fiddling with this I found a solution that does exactly what I wanted (and a little more) and it was really easy. I'm really amazed at how easy it is to modify CS-Cart, and this will be really useful for my store. If someone's interested in the solution, PM me.

Please post the solution :)

solesurvivor, i'll do it in a pinch, let me just refine some things. Where's your store? What do you need this functionality for?

Interesting, I sell personalised products too.



john

[quote name='denisgomes' timestamp='1340553518' post='139338']solesurvivor, i'll do it in a pinch, let me just refine some things. Where's your store? What do you need this functionality for?[/quote]



Store is not live but the feature could come in handy for a store I'm scheduled to work on that allows product customization. I would also like to explore if an item is available in multiple colors to show each color as an individual product on the category page but when the customer clicks the product it's one product with options and color they chose is already selected…

[quote name='solesurvivor' timestamp='1340557056' post='139341']

but when the customer clicks the product it's one product with options and color they chose is already selected…

[/quote]



Wouldn't it be easier if you just add/copy/clone the products instead of creating a mod to show these additional products?

[quote name='johnbol1' timestamp='1340554735' post='139339']

Interesting, I sell personalised products too.

[/quote]



What do you sell? Where's your store?

[quote name='denisgomes' timestamp='1340563058' post='139344']



Wouldn't it be easier if you just add/copy/clone the products instead of creating a mod to show these additional products?[/quote]



No. Our products are coming directly from our businesses existing erp and we never create a product color as a different item for sales reporting purposes we need to refer to the style and see all colors sold as well as many other reasons.

[quote name='denisgomes' timestamp='1340563145' post='139345']

What do you sell? Where's your store?

[/quote]



Store is UK, see avatar for web addy

solesurvivor, I haven't forgot about this, lemme just work out some more things and I'll add it.



BTW should I add it to a post in the bottom or edit the first post?

Yes please add to new post I would love to see this.

Any chance you could PM me the solution for this denisgomes or post it on the forum? This is just what I’ve been looking for but I just cant figure it out myself! Would be extremely grateful if you would be able to help me! :)

Hello simenkl. Here’s what I did, I totally forgot about this topic.



First I created a text input option for the product, and set its inner hint as ‘getparam|urltirinha’. getparam is a bogus command I made up, and urltirinha is the name of the URL parameter I’d like to grab.



Then I used the file editor to edit the /templates/views/products/components/product_options.tpl file. This controls the behavior of the product options. Near the line 94 you’ll see:


<br />
{elseif $po.option_type == "I"} {*Input*}<br />

```<br />
<br />
After that, I added the following code:<br />
<br />
```php
<br />
   	 {* Busca de parametros *}<br />
			{assign var="campo" value=$po.value|default:$po.inner_hint}<br />
			{if $campo|truncate:8:"":TRUE == "getparam"}<br />
			{assign var="parametro" value=$campo|substr:9}<br />
			{if $smarty.get.$parametro != ""}<br />
				{assign var="parametro_conteudo" value=$smarty.get.$parametro}<br />
			{/if}<br />
			<input id="option_{$obj_prefix}{$id}_{$po.option_id}" type="text" name="{$name}[{$id}][product_options][{$po.option_id}]" value="{$parametro_conteudo}" {if $product.exclude_from_calculate && !$product.aoc}disabled="disabled"{/if} class="ty-valign ty-input-text"{if $po.inner_hint} cm-hint{/if}{if $product.exclude_from_calculate && !$product.aoc} disabled{/if}" {if $po.inner_hint}title="{$po.inner_hint}"{/if} /><br />
			<br /><br /><br />
			{if $parametro_conteudo != ""}<br />
			<img src="{$parametro_conteudo}" style="width:400px; height:auto;" /><br />
			{/if}<br />
			<br /><br /><br />
		{else}<br />

```<br />
<br />
Note that this code should be ADDED to the existing code, that is, the product options will work just as before if there is no GETPARAM in the inner hint. The above code will simply check if we used the GETPARAM 'command' in the inner hint, and if so, it will grab the contents of the URL parameter named after the pipe (|) and paste into the input field. You can use any URL parameter name that you want.<br />
<br />
Also, I did this under CS-Cart 4.1.5 based on the store's default template. The <input> tag may have to be changed accordingly if you're using a newer version or a different template. In that case, you will find that same tag elsewhere in this file, so you'll just have to adapt it to this code.<br />
<br />
Besides filling in the URL parameter into the input field, this code will also display the captured URL parameter as an image below the input field, because I'm using it to sell products based on comic strips. If you don't need this, just remove the whole {if $parametro_conteudo != ""} part.<br />
<br />
If you'd like to see it live, please go to www.vidadesuporte.com.br and below each comic you'll see a 'Compre esta tirinha...' button underneath each comic. That button will redirect to my store and you should see the same comic in the product page. But please don't make any test orders <img src="upload://ssa1U17ndImgNZSdwFNmOF2yUgM.png" class="bbc_emoticon" alt=";)"><br />
<br />
BTW, since you are modifying a template file, this will have to be reapplied every time you change the store's template or upgrade the system. However, CS-Cart is smart enough to tell you if this file has changed during upgrade, so you can quickly reapply this modification.

thanks for your quick reply denisgomes! much appreciated! :grin:



I’ve tried to add the code you provided. I’m using version 4.1.3. and a custom template called buyshop. i found the specific line of text on line 89 in my file and added the section you provided after that without replacing anything.



the only part I adapted was the input code as I have a different version and another template as you instructed and I simply copied the code from that originally was directly below line 89 (94 for you):



<input id=“option_{$obj_prefix}{$id}_{$po.option_id}” type=“text” name=“{$name}[{$id}][product_options][{$po.option_id}]” value=“{$po.value|default:$po.inner_hint}” {if $product.exclude_from_calculate && !$product.aoc}disabled=“disabled”{/if} class=“valign input-text{if $po.inner_hint} cm-hint{/if}{if $product.exclude_from_calculate && !$product.aoc} disabled{/if}” {if $po.inner_hint}title=“{$po.inner_hint}”{/if} />



the whole code now looks like this:


{elseif $po.option_type == "I"} {*Input*}  <br />
		{* Busca de parametros *}<br />
						{assign var="campo" value=$po.value|default:$po.inner_hint}<br />
						{if $campo|truncate:8:"":TRUE == "getparam"}<br />
						{assign var="parametro" value=$campo|substr:9}<br />
						{if $smarty.get.$parametro != ""}<br />
								{assign var="parametro_conteudo" value=$smarty.get.$parametro}<br />
						{/if}<br />
		<input id="option_{$obj_prefix}{$id}_{$po.option_id}" type="text" name="{$name}[{$id}][product_options][{$po.option_id}]" value="{$po.value|default:$po.inner_hint}" {if $product.exclude_from_calculate && !$product.aoc}disabled="disabled"{/if} class="valign input-text{if $po.inner_hint} cm-hint{/if}{if $product.exclude_from_calculate && !$product.aoc} disabled{/if}" {if $po.inner_hint}title="{$po.inner_hint}"{/if} /><br />
						<br /><br /><br />
				{else}	  <br />
			<input id="option_{$obj_prefix}{$id}_{$po.option_id}" type="text" name="{$name}[{$id}][product_options][{$po.option_id}]" value="{$po.value|default:$po.inner_hint}" {if $product.exclude_from_calculate && !$product.aoc}disabled="disabled"{/if} class="valign input-text{if $po.inner_hint} cm-hint{/if}{if $product.exclude_from_calculate && !$product.aoc} disabled{/if}" {if $po.inner_hint}title="{$po.inner_hint}"{/if} />
```<br />
<br />
<br />
however when i try to open a product page it is blank and nothing comes up. Can you see any apparant mistakes?<br />
<br />
I have saved the inner hint in the product option text field named 'ticket' as [color=#282828][font=arial, verdana, tahoma, sans-serif]getparam|urltirinha like you said, and I have removed the[/font][/color][color=#282828][font=arial, verdana, tahoma, sans-serif]{if $parametro_conteudo != ""} part.[/font][/color]<br />
<br />
[color=#282828][font=arial, verdana, tahoma, sans-serif]Once again, Thank you for your help! <img src="upload://b6iczyK1ETUUqRUc4PAkX83GF2O.gif" class="bbc_emoticon" alt=":-)">[/font][/color]

The tag must be adapted instead of simply copied from your template:



value=“{$parametro_conteudo}” {if $product.exclude_from_calculate && !$product.aoc}disabled=“disabled”{/if} class=“ty-valign ty-input-text”{if $po.inner_hint} cm-hint{/if}{if $product.exclude_from_calculate && !$product.aoc} disabled{/if}" {if $po.inner_hint}title=“{$po.inner_hint}”{/if}



Also, if you'd like to use a different parameter name such as TICKET instead of URLTIRINHA, you must change it accordingly in the code.

{elseif $po.option_type == "I"} {*Input*}
{* Busca de parametros *}
{assign var="campo" value=$po.value|default:$po.inner_hint}
{if $campo|truncate:8:"":TRUE == "getparam"}
{assign var="parametro" value=$campo|substr:9}
{if $smarty.get.$parametro != ""}
{assign var="parametro_conteudo" value=$smarty.get.$parametro}
{/if}
{else}






Cheers for the quick reply again!



the above code is what I have added after line 89 (94 for you) now. I have adapted it now like you said.



The text field in product options i have named ticket, however the inner hint is set as [color=#282828][font=arial, verdana, tahoma, sans-serif]getparam|urltirinha as you instructed.[/font][/color]



My product page still just does not appear unfortunately. Any ideas or apparent mistakes? Sorry, I'm not experienced with this!

If your URL is mywebsite.com - This website is for sale! - mywebsite Resources and Information. then the inner hint should contain getparam|ticket, as I believe it was made clear befrore.

[quote name='denisgomes' timestamp='1405175542' post='187443']

If your URL is mywebsite.com - This website is for sale! - mywebsite Resources and Information. then the inner hint should contain getparam|ticket, as I believe it was made clear befrore.

[/quote]



Cheers for looking at it again! I have followed and understand those instructions. What I meant was that once I insert the code in the [color=#282828][font=arial, verdana, tahoma, sans-serif]/templates/views/products/components/product_options.tpl [/font][/color]none of my product pages load regardless of if they have any product options specified in them or not. The product page on my shop it just returns a blank page. If I remove the code from the file again it all works like normal again. [font=“arial, verdana, tahoma, sans-serif”][color=“#282828”]are there any other settings i need to apply or changes i need to do to get it to work? [/color][/font][color=#282828][font=arial, verdana, tahoma, sans-serif]thanks again![/font][/color]