Forums / General Questions / Determine if a certain product is in the cart

Determine if a certain product is in the cart

Locked

Views: 1,933

Results 1 to 11 of 11
This thread is locked. New replies are disabled.
11 Dec 2007, 4:25 PM
#1
mygreygoose avatar

mygreygoose

New Zenner

Join Date:
Jun 2007
Posts:
91
Plugin Contributions:
0

Determine if a certain product is in the cart

I want to include a message flag when certain items are in the shopping cart on tpl_shopping_cart_default.php.

The logic is basically, if the cart contains product id 142, then show some html code. I played with the Zen Cart functions, but I just couldn't figure out how to create this statement.

11 Dec 2007, 5:20 PM
#2
hem avatar

hem

Totally Zenned

Join Date:
Sep 2007
Location:
Dublin, Ireland
Posts:
1,281
Plugin Contributions:
5

Re: Determine if a certain product is in the cart

Do you want to hardwire it or write it as an SQL query??? Where do you want the message to appear??? On the main page, underneath the shopping cart, in its own side box????

Andrew

Andrew Moore
Omnicia Agile Development
www.omnicia.com

11 Dec 2007, 5:25 PM
#3
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Determine if a certain product is in the cart

The cart content is store in $_SESSION, you can retrieve it anywhere.
Try print_r($_SESSION) to learn the structure of the data.

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

11 Dec 2007, 6:52 PM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Determine if a certain product is in the cart

Just use the built-in function:```
$_SESSION['cart']->in_cart($products_id)

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

12 Dec 2007, 12:37 PM
#5
hem avatar

hem

Totally Zenned

Join Date:
Sep 2007
Location:
Dublin, Ireland
Posts:
1,281
Plugin Contributions:
5

Re: Determine if a certain product is in the cart

DrByte:

Just use the built-in function:```
$_SESSION['cart']->in_cart($products_id)


So you can just add:

```php
  $alert_id=142;
  if($_SESSION['cart']->in_cart($alert_id))
  {
	$content.="<strong>Insert your custom HTML here for display when products_id 142 is found in your shopping cart</strong>";
  }

to your tpl_shopping_cart.php, near the end of the file, right before $content .= '</div>'; (see attached).

Andrew

Andrew Moore
Omnicia Agile Development
www.omnicia.com

12 Dec 2007, 3:49 PM
#6
stuff4toys avatar

stuff4toys

Totally Zenned

Join Date:
May 2007
Location:
SW Florida
Posts:
1,062
Plugin Contributions:
1

Re: Determine if a certain product is in the cart

Why not use one of the "Cross Sell Mods" in the dl section? Then you can set up your products from the admin.

???
JOhn ><>

Newest Site: ChargerPros - Stuff4Toys

14 Dec 2007, 11:33 PM
#7
mygreygoose avatar

mygreygoose

New Zenner

Join Date:
Jun 2007
Posts:
91
Plugin Contributions:
0

Re: Determine if a certain product is in the cart

Sorry for the delay in responding... been having a terrible time getting it to work. It's not working with the particular product ID I'm looking for, and I'm wondering if it has anything to do with it having attributes. When I do a var_dump, I get the below.

For some reason, the product_ID is being appended with what looks like a session token (186:da00eb308c52a23d5e6d9c1ce7270e44).

object(shoppingCart)#4 (10) { ["contents"]=> array(1) { ["186:da00eb308c52a23d5e6d9c1ce7270e44"]=> array(2) { ["qty"]=> float(1) ["attributes"]=> array(2) { [18]=> string(2) "70" [2]=> string(2) "19" } } } ["total"]=> float(39.99) ["weight"]=> float(4) ["cartID"]=> string(5) "11075" ["content_type"]=> string(8) "physical" ["free_shipping_item"]=> int(0) ["free_shipping_weight"]=> int(0) ["free_shipping_price"]=> int(0) ["customers_group"]=> NULL ["observers"]=> array(0) { } }

Thanks everyone for your help with this... you've been so kind!

15 Dec 2007, 12:23 AM
#8
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Determine if a certain product is in the cart

MyGreyGoose:

For some reason, the product_ID is being appended with what looks like a session token (186:da00eb308c52a23d5e6d9c1ce7270e44).That's not a session token. It's a unique identifier for that particular product in that particular cart with that particular combination of attributes assigned to it.

To detect just the product, strip off the colon and the hash before passing it to the function.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

15 Dec 2007, 7:35 PM
#9
mygreygoose avatar

mygreygoose

New Zenner

Join Date:
Jun 2007
Posts:
91
Plugin Contributions:
0

Re: Determine if a certain product is in the cart

How do I strip off the colon and hash before passing it to the function? I've tried everything, but I don't understand how to truncate something from an array.

This is the only code I'm using:

  $alert_id=180;
    
  if($_SESSION['cart']->in_cart($alert_id))
  {
    $content .= "custom HTML here";
  }  
15 Dec 2007, 8:02 PM
#10
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,515
Plugin Contributions:
176

Re: Determine if a certain product is in the cart

hmmm ... your case is backwards to how I had read it the first time round ... oops

Try in_cart_mixed() instead of in_cart()

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

15 Dec 2007, 8:15 PM
#11
mygreygoose avatar

mygreygoose

New Zenner

Join Date:
Jun 2007
Posts:
91
Plugin Contributions:
0

Re: Determine if a certain product is in the cart

That worked... thanks!!!