Jump to content

if product id in cart


dvp

Recommended Posts

Hello WHMCS community,

 

I am currently looking for a template syntax or a method to add specific images to my cart table (viewcart.tpl) based upon the ID of the specific product. Since I just have a few basic products available i would like to do this manually e.g. if product xy show img a, else if product xx show image b etc...

 

However I have searched the forum and other resources and could not find a proper method to get this done. Some expert advise would be truly appreciated.

 

Thank you very much and best regards,

dvp

Link to comment
Share on other sites

the basic way would be to do this...

 

{if $product.pid eq "46"}<img src="images/hosting1.png">{elseif $product.pid eq "42"}<img src="images/hosting2.png">{/if}

remember that this will only work for products - for domains, you would need to play with the $domain array.

 

the more advanced way, would first check if an image exists for the product in the cart, and if it does display it - if no product image exists, it won't attempt to display any image.

 

the advantage of this method is that if you add new products which have images, you won't need to modify the template each time...

 

{assign var="prodimage" value="images/"|cat:$product.pid|cat:'.png'}{if "$prodimage"|file_exists}<img src="{$prodimage}">{/if}

the images should be in the same directory - in the above example, the "images" folder within your WHMCS installation.

each image should be given the same filename as the product ID - so if your productID (PID) = 34, then name the image "34.png" - if you would prefer to use .jpg, then alter the above code and replace .png with .jpg

 

the above code should go in the {foreach key=num item=product from=$products} loop of viewcart.tpl

Link to comment
Share on other sites

  • 4 years later...
 
 
 
 
On 3/12/2015 at 6:44 PM, brian! said:

the basic way would be to do this...

 


{if $product.pid eq "46"}<img src="images/hosting1.png">{elseif $product.pid eq "42"}<img src="images/hosting2.png">{/if}

 

Hi brian!

I'm trying to do something like op in viewcart but $product.pid is not a variable  there

I use in configureproductdomain.tpl this "{if $templatefile == 'configureproductdomain' && in_array($productinfo.pid, $prodID)}" and is working because is a simple array, but in viewcard is a multidimensional array and in_array is not working here.

How can I get the product pid from viewcart so I cand create an if condition?

thanks

 

Link to comment
Share on other sites

Hi @mihai666g

19 hours ago, mihai666g said:

I'm trying to do something like op in viewcart but $product.pid is not a variable  there

are you sure ?

19 hours ago, mihai666g said:

use in configureproductdomain.tpl this "{if $templatefile == 'configureproductdomain' && in_array($productinfo.pid, $prodID)}" and is working because is a simple array, but in viewcard is a multidimensional array and in_array is not working here.

then if you want to do this in the template, the simple solution is to loop though the array...

19 hours ago, mihai666g said:

How can I get the product pid from viewcart so I cand create an if condition?

would help to know what you wanted to output and where, but i'm assuming either it's something you want to do multiple times (if there are multiple conditions net) or you only want to do it once (regardless of how many times you find x in viewcart)...

if it's the first, then you do it within the $products loop (assuming that's where the output is going to be)...

{foreach $products as $num => $product}
	{if in_array($product.pid, array('123'))}it's in there!!{/if}
{/foreach}

if it's the second and you only want it displayed once, then you would assign a counter as per the code in the thread below...

 

Link to comment
Share on other sites

 
 
 
 
50 minutes ago, brian! said:

if it's the first, then you do it within the $products loop (assuming that's where the output is going to be)...


{foreach $products as $num => $product}
	{if in_array($product.pid, array('23,32,45'))}it's in there!!{/if}
{/foreach}

 

I tried and it breaks my page, css + js gone.

Link to comment
Share on other sites

46 minutes ago, brian! said:

did you try it ?

the way you had your array list was wrong - either each value can be in quotes, or if you know they're numbers, you won't need any single quotes.

Yes, I tried and is not working.

Link to comment
Share on other sites

@mihai666g I think you misunderstood @brian!.

Your code was wrong. By wrapping all of your numbers in one single quote, it's treated as one number - either wrap each number in their own single quote (array('23','32','45')) or don't do it at all (array(23,32,45)).

Did you try using the code he pasted? It's not the same as the code you posted.

Link to comment
Share on other sites

 
 
 
 
 
 
 
✌
6
12 hours ago, DennisHermannsen said:

@mihai666g I think you misunderstood @brian!.

Your code was wrong. By wrapping all of your numbers in one single quote, it's treated as one number - either wrap each number in their own single quote (array('23','32','45')) or don't do it at all (array(23,32,45)).

Did you try using the code he pasted? It's not the same as the code you posted.

I tried every scenario, the code is not working and is breaking the page.

Normal page, https://prnt.sc/repaxo, added the code https://prnt.sc/repbgb and this is the output https://prnt.sc/repbs7

Link to comment
Share on other sites

7 minutes ago, brian! said:

this is what i'm seeing in Six & Standard cart

xcneu2T.png

Dennis is absolutely correct - the activity log should give you a clue to what it doesn't like.

Thi is the error:

Smarty Error: Syntax error in template "file:/home/xxxxxxxxxxx/public_html/mi.xxxxxxxx.com/templates/lagom/core/pages/viewcart/xxxxxx/viewcart.tpl" on line 2 "{foreach $products as $num => $product}" missing 'item' attribute

Edited by mihai666g
Link to comment
Share on other sites

8 minutes ago, DennisHermannsen said:

Could you paste your entire viewcart.tpl?

I've just inserted this:


{foreach $products as $num => $product}
	{if in_array($product.pid, array(1,2))}it's in there!!{/if}
{/foreach}

And I get the exact same as @brian!

.... somehow now is working, I just deactivated my custom theme file and reactivated it.... my brain hurts

Thanks guys for patience.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated