X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Cart modification: how to hide option if product already in cart? (https://forum.x-cart.com/showthread.php?t=34524)

ben@lrb.co.uk 10-11-2007 03:25 AM

Cart modification: how to hide option if product already in cart?
 
I've created a snippet of code to add a giftwrap option to the shopping cart (a hidden product, click to opt-in).

However, I want this NOT to show up if there is already giftwrap in the cart.

Obviously the basic structure I'm after is something like:

{if [cart contents] [does not contain] [product 3491]}

{include file="customer/main/gift_wrap.tpl"}

{/if}

but I'm not sure what variables to use in the if statement, or indeed, what expression Smarty uses for a "does not contain" match (so far I've just seen equals, not equals, greater than, less than etc. etc.)

Can anyone advise here?

Many thanks,

Rachael

ben@lrb.co.uk 10-15-2007 06:54 AM

Re: Cart modification: how to hide option if product already in cart?
 
Still hoping that someone out there has the answer to this... There must be some of this logic used somewhere in X-Cart because of the "give products for free" if a certain product is added to the card special-offers thing.

Cheers,

Rachael

geckoday 10-15-2007 08:14 AM

Re: Cart modification: how to hide option if product already in cart?
 
If you want the include after the list of products you can check in the loop that lists the products. Change this:
Code:

{section name=product loop=$products}
To this:
Code:

{assign var="gwfound" value="n"}
{section name=product loop=$products}
{if $products[product].productid eq 3491}
  {assign var="gwfound" value="y"}
{/if}


Then after the {/section} wherever you want it add:
Code:

{if $gwfound eq "n"}
  {include file="customer/main/gift_wrap.tpl"}
{/if}


If you want it before the list of products you'll probably have to add a second {section} {/section} block to loop through the array just to check for it.

ben@lrb.co.uk 10-18-2007 04:16 AM

Re: Cart modification: how to hide option if product already in cart?
 
Hi Ralph,

Thanks so much - that works beautifully.

This is a very elementary question, but I am really new to this:

Why does the check need to be assigned to a separate variable for the evaluation to work?

Previously, I had:

Code:

{if $products[product].productid ne "3491"}
{include file="customer/main/gift_wrap.tpl"}


but although it was including the file fine, it was doing it regardless of the existence of product 3491. Though I guess that could be (a) the double quotes round the product ID (b) the fact that I had the evaluation happening outside the products section??

It would be useful to know why this works for future ref...

Cheers,

Rachael

geckoday 10-18-2007 06:18 AM

Re: Cart modification: how to hide option if product already in cart?
 
Quote:

Originally Posted by ben@lrb.co.uk
Previously, I had:

Code:

{if $products[product].productid ne "3491"}
{include file="customer/main/gift_wrap.tpl"}


but although it was including the file fine, it was doing it regardless of the existence of product 3491. Though I guess that could be (a) the double quotes round the product ID (b) the fact that I had the evaluation happening outside the products section??

It would be useful to know why this works for future ref...

Cheers,

Rachael

The double quotes are OK but not necessary - smarty/PHP is very forgiving about converting between strings and numbers. $products is an array containing all of the products in the cart. The {section} {/section} loop repeats whats inbetween them changing $product each time to point to the next product within $products. Outside of the {section} {/section} loop the value of $product isn't defined so your test of $products[product].productid won't work. You could have moved it inside the {section}{/section} but then it would include the file once for each product that isn't 3491 in the cart - not what you want. There is no simple function to just ask if productid 3491 exists in the $products array so you have to loop through the array and check every item in the cart. Hence the need for the assign within the {section}{/section} loop to set a flag if product 3491 is found.


All times are GMT -8. The time now is 01:30 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.