[Checkout/Carbon] Add to Shopping Cart link and Smarty string functions

sitarah

Joined: 2006-11-17
Posts: 1
Posted: Mon, 2006-11-20 20:40

Hi,

Several people have asked how to get the Add to Shopping Cart button as a standalone link when you are using the checkout module with paypal or email. As it is now, you can see this link only in Item Actions. After a few days, I found a solution. The below may be a good exercise for those who want to become more familiar with smarty string functions, too.

I could find no way to extract a particular link out of Core.ItemLinks, so I took an indirect approach; constructing the shopping cart link on my own in the smarty template. The link is constructed in three parts; the module name, the photo ID, and the return path.
For instance:

http://www.stealmydesktops.com/gallery2/main.php?g2_controller=checkout.AddToCart <---Module Name
&amp;g2_itemId=21&amp; <--Item ID
g2_return=/gallery2/v/Lakes/000_0545.JPG.html <--Return Path
&amp;g2_returnName=photo <-- Static String

The module name and the static string at the end are unchanging. The Item ID is accessible via $theme.item.id. The return path is difficult, though -- a template doesn't know what page it is serving. Any use of PHP_SELF or server variables returns main.php, and none of the arrays, like theme.params, or albumblocks, have the path, either. Therefore, we have to be indirect with this as well. We DO have access to the navigational buttons, which have links to the next and previous photo. They have the path. What you must do is get the link for the next or previous photo, cut off the image name of the previous photo, then add the new image name to it.

For instance:
/gallery2/v/Lakes/000_0545.JPG is the current picture

/gallery2/v/Lakes/000_0544.JPG is the previous picture.

I extract /gallery2/v/Lakes/ from the previous picture's url, then add 000_0545.JPG to get /gallery2/v/Lakes/000_0545.JPG, which is what we wanted.

The dependency on navigational links means that the code will NOT give you a link if there is no previous or next photo. In other words, if you have an album with one picture, you will not see the link.

The following code should go into navigator.tpl if you have one or photo.tpl.

//Capture will put whatever is *printed to the screen* into a variable.
//{g->url params=$theme.navigator.next.urlParams} must first execute to give me /gallery2/v/Lakes/000_0544.JPG, //so a normal variable assignment will not work. [Or so it seemed.]

	      {capture name=NextUrl}
	      	   {g->url params=$theme.navigator.next.urlParams}

	      {/capture}
	     
//if there IS no next picture, try to get the previous picture's url      
	      {if NextUrl == ""}
		    {capture name=NextUrl}
		    {g->url params=$theme.navigator.previous.urlParams}

			{/capture}
	      {/if}
//If nexturl is STILL blank, then there is no next or previous picture, and
// therefore, there is no way to construct the shopping cart link.
		{if NextUrl != ""}
			//put the url into variable theNextUrl.
                        //yes, those are TICK MARKS, NOT quotes!!
			  {assign var="theNextUrl" value=`$smarty.capture.NextUrl`}
			  
			 {assign var="theNeedle" value="/"}
			 
			 //Find the LAST slash [theNeedle] in the url. StrRpos is reverse string pos. You do 
                         //this so that you know where to chop off the image name.
			 //Ex: /gallery2/v/Lakes/000_0544.JPG 			 
			 {assign var="endUrl" value=$theNextUrl|strrpos:$theNeedle}

			 //Take a substring of the url, starting at 0 and ending at the last slash, but not
                         //including it.			 
			 {assign var="clippedUrl" value=$theNextUrl|substr:0:$endUrl}
			  //Add the slash back in*** because you want /gallery2/v/Lakes/ 
			 {assign var="clippedUrl" value=$clippedUrl|cat:'/'}
			 
			 //Now put the current picture on the end of the url. The name of the current 
                         //photo/image is stored in $theme.item.pathComponent 
			 //you now have  /gallery2/v/Lakes + / + 000_0545.JPG = gallery2/v/Lakes/000_0545.JPG 
			 
			 {assign var="NewUrl" value=$clippedUrl|cat:$theme.item.pathComponent }
			  
			  //Get any spaces out of NewUrl
			 {assign var="NewUrl" value=$NewUrl|trim }

			//now we've made the return path url.
			//we will now construct the actual shopping cart url, using the return path
			
			//now put your OWN domain path here! CHANGE THIS!
			//it MUST still go to main.php
			
	{assign var="FullLink" 
			 value="http://www.stealmydesktops.com/gallery2/main.php?g2_controller=checkout.AddToCart&amp;g2_itemId="}

			//add the item id onto the url
			 {assign var="FullLink"  value=$FullLink|cat:$theme.item.id}
			 //add the return path string
			 {assign var="FullLink"  value=$FullLink|cat:'&amp;g2_return='}
			 //add the return path URL you made above
			 {assign var="FullLink"  value=$FullLink|cat:$NewUrl}
			//add the final static string 
			 {assign var="FullLink"  value=$FullLink|cat:'.html&amp;g2_returnName=photo'}
		                 

			//Now just put in link code, with the href equal to the link url you just made.
			//Customize Add to Cart if you want.
			  <a href="
				 {$FullLink4}">Add to Cart</a>
			
                {/if}

***You may wonder why I didn't just include the slash, rather than taking it out and putting it back in. I'd have to increment $endUrl and smarty is finicky. It might let me do ($endUrl + 1) there, it might not, and I don't want to test it out.

So there you have. Paste that in to your navigator.tpl or photo.tpl, wherever you want the link, noting that you absolutely MUST change the domain name to your own.

For anyone reading this to get abreast of smarty string functions, you can do any php string function in smarty, using syntax like this:
{assign var="X" value="Y"|function:arg:arg}
In an if statement, you can use any php string function with normal php syntax.
{if substr("foo",strlen("foo")-1,strlen("foo")) == ".jpg"}
However, note that smarty IS finicky, and cat: 'foo' won't work. cat:'foo' will.

I hope this was helpful.

 
simtel

Joined: 2008-01-22
Posts: 4
Posted: Thu, 2008-01-31 06:26

Is it possible to use this code without the URL Rewrite plugin activated?
The code as it is now gives out a wrong return address which in turn throws a Security Violation (because the return address is not part of the gallery) when you try and add something to the cart.

 
simtel

Joined: 2008-01-22
Posts: 4
Posted: Thu, 2008-01-31 12:02

Ok, I worked out a solution myself. So here it is for anyone else interested:
This should work if you are not using the URL Rewrite plugin.
<a class="{g->linkId view="checkout.AddToCart"}" href="{g->url arg1="controller=checkout.AddToCart" arg2="itemId=`$theme.item.id`" arg3="return=/gallery2/main.php?g2itemId=`$theme.item.id`"}">{g->text text="Add to cart"}</a>
Just copy and past that where you want the link.
You may need to alter the path for the 'return' in arg3 to get it to return properly.

Hope that saves someone the hours it took me to write :)

 
thuthime

Joined: 2008-11-06
Posts: 1
Posted: Thu, 2008-11-06 08:46

hi,

may i know where i should paste this code? please

my problem is... when i click in "ADD TO CART" button. i see Security Violation ...
will i be able to solve this problem by adding this code.. please help im new to this

have a nice day,
thuthi,