Forums / All Other Contributions/Addons / question about in_cart() function,

question about in_cart() function,

Views: 1,435

Results 1 to 13 of 13
5 Feb 2013, 2:49 AM
#1
tcarden avatar

tcarden

Zen Follower

Join Date:
Oct 2012
Posts:
282
Plugin Contributions:
0

question about in_cart() function,

I'm trying to trouble shoot some code in my new observer class. this is my first time out using the observer class and i've gone thru the 2 tuts in the wiki.

i'm trying to check for the presence of certain products (hazmat) in the cart. and if there, create a ```php
$_SESSION['hazMatOneInCart'] = TRUE


orig i have a var based on a specific product id -
var $hazMatProdOne = 115;

and then ran like below - 

```php
function update(&$class, $eventID, $paramsArray = array()) {
	 if($_SESSION['cart']->in_cart($this->hazMatProdOne)) {
		//echo 'has HazMat One';
		$_SESSION['hazMatOneInCart'] = TRUE;
	} 
}

didn't seem to be working, and then, for testing purposes when i switched out the var $hazMatProdOne to an integer, 115 (a product id) and got an error -

function update(&$class, $eventID, $paramsArray = array()) {
	 if($_SESSION['cart']->in_cart($this->115)) {
		$_SESSION['hazMatOneInCart'] = TRUE;
	} 
	
}
5 Feb 2013, 4:32 AM
#2
tcarden avatar

tcarden

Zen Follower

Join Date:
Oct 2012
Posts:
282
Plugin Contributions:
0

Re: question about in_cart() function,

never mind, i see the problem

$this->155
``` is wrong, 
instead, 
```php
$this->shouldBeAVar
5 Feb 2013, 5:02 AM
#3
drbyte avatar

drbyte

Sensei

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

Re: question about in_cart() function,

Or just 115 instead of $this->115

.
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.

5 Feb 2013, 5:21 AM
#4
tcarden avatar

tcarden

Zen Follower

Join Date:
Oct 2012
Posts:
282
Plugin Contributions:
0

Re: question about in_cart() function,

hey Dr.Byte, yes i caught after i posted, thanks

for some reason i cannot get this in_cart() function to return true even though i have product 115 in the cart. not sure what to do?

function update(&$class, $eventID, $paramsArray = array()) {
if($_SESSION['cart']->in_cart(115)) {
$_SESSION['hazMatOneInCart'] = TRUE;
//echo 'has HazMat One';
}
} //doesn't return true

however, below does work. meaning the other parts of the code not listed here are working. how can i trouble shoot this?

function update(&$class, $eventID, $paramsArray = array()) {
if(true) {
$_SESSION['hazMatOneInCart'] = TRUE;
//echo 'has HazMat One';
}
}

5 Feb 2013, 4:00 PM
#5
tcarden avatar

tcarden

Zen Follower

Join Date:
Oct 2012
Posts:
282
Plugin Contributions:
0

Re: question about in_cart() function,

getting discouraged here. not sure how to get that to test as true,
i'll include more code here in case that helps to clarify. the autoloaders are working.

function hazMatProduct() {
    $_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END'));
}

function update(&$class, $eventID, $paramsArray = array()) {
global $cart;
if($_SESSION['cart']->in_cart(115)) {
//if(TRUE) {   //this works
        $_SESSION['hazMatOneInCart'] = TRUE;
    }
}
}

are there any more tuts available on using the observer class? i've done the two in the Developers WIKI -
the prod id 115 is just for testing, will be changed to a variable once i figure out problem here

5 Feb 2013, 7:50 PM
#6
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,110
Plugin Contributions:
56

Re: question about in_cart() function,

tcarden:

thanks, unfortunately it doesn't do anything.

getting discouraged here. not sure how to get that to test as true,
i'll include more code here in case that helps to clarify. the autoloaders are working.

function hazMatProduct() {
$_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END'));
}

function update(&$class, $eventID, $paramsArray = array()) {
global $cart;
if($_SESSION['cart']->in_cart(115)) {
//if(TRUE) { //this works
$_SESSION['hazMatOneInCart'] = TRUE;
}
}
}

> 
> are there any more tuts available on using the observer class? i've done the two in the Developers WIKI -
> the prod id 115 is just for testing, will be changed to a variable once i figure out problem here
How about this:

function hazMatProduct() {
$this->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END', 'NOTIFIER_CART_UPDATE_QUANTITY_END'));
}

function update(&$class, $eventID, $paramsArray = array()) {
$_SESSION['hazMatOneInCart'] = ($class->in_cart(115)) ? true : false;
}

6 Feb 2013, 6:24 AM
#7
drbyte avatar

drbyte

Sensei

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

Re: question about in_cart() function,

in_cart() works on the complete product-id value. And if you've got attributes on the product, the hash code after the main product number is also part of the in_cart() search.

So, if the product-id assigned by the cart after selecting attribute combinations is 115:58203828a348f838e8f8d8c then testing in_cart(115) will fail.

Instead, you'll need to get $_SESSION['cart']->get_products() into a variable, and then loop through the array of products dumped into that variable, and find a match on the number you're looking for, perhaps using (int) to force the removal of the hash when doing your comparison. (Can't use (int) on the parameters to in_cart() calls because that's going the opposite direction to what you're seeking.)

.
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.

7 Feb 2013, 2:18 AM
#8
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: question about in_cart() function,

You could also try:

[B]$_SESSION['cart']->in_cart_check('products_id','115')[/B]

which will do all the extra code of looping through the results of the $_SESSION['cart']->get_products() without the check of attributes ...

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

7 Feb 2013, 3:52 AM
#9
tcarden avatar

tcarden

Zen Follower

Join Date:
Oct 2012
Posts:
282
Plugin Contributions:
0

Re: question about in_cart() function,

excellent, thanks!,
extremely helpful..

just call me Mr. HazMat

7 Feb 2013, 5:13 AM
#10
drbyte avatar

drbyte

Sensei

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

Re: question about in_cart() function,

Ajeh:

You could also try:

[B]$_SESSION['cart']->in_cart_check('products_id','115')[/B]

> which will do all the extra code of looping through the results of the $_SESSION['cart']->get_products() without the check of attributes ...
Yup, that'll work too.

.
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.

7 Feb 2013, 1:17 PM
#11
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,110
Plugin Contributions:
56

Re: question about in_cart() function,

lat9:

How about this:

function hazMatProduct() {
$this->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END', 'NOTIFIER_CART_UPDATE_QUANTITY_END'));
}

function update(&$class, $eventID, $paramsArray = array()) {
$_SESSION['hazMatOneInCart'] = ($class->in_cart(115)) ? true : false;
}

There's one more place where a hazMat product could "sneak" into the cart .. when a customer's cart is restored from a previous login; you'll need to add 'NOTIFIER_CART_RESTORE_CONTENTS_END' to the list of notifiers that you attach to. Combining that with Ajeh's suggestion yields:

function hazMatProduct() {
$this->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END', 'NOTIFIER_CART_UPDATE_QUANTITY_END', 'NOTIFIER_CART_RESTORE_CONTENTS_END'));
}

function update(&$class, $eventID, $paramsArray = array()) {
$_SESSION['hazMatOneInCart'] = ($class->in_cart_check('products_id', '115')) ? true : false;
}

8 Feb 2013, 4:19 AM
#12
tcarden avatar

tcarden

Zen Follower

Join Date:
Oct 2012
Posts:
282
Plugin Contributions:
0

Re: question about in_cart() function,

thanks lat9, am adding your code now.

maybe i'll have a decent plugin or add-on to contribute when this is all done.

regards,
Mr. HazMat

11 Jul 2013, 12:42 PM
#13
bobthemolder avatar

bobthemolder

Zen Follower

Join Date:
Nov 2008
Posts:
164
Plugin Contributions:
0

Re: question about in_cart() function,

Ajeh:

You could also try:

[B]$_SESSION['cart']->in_cart_check('products_id','115')[/B]

> which will do all the extra code of looping through the results of the $_SESSION['cart']->get_products() without the check of attributes ...
Bump/+1 Thanks Ajeh I was running into this issue as well.

Perhaps the core functionality of the $_SESSION['cart']->in_cart() should be changed to work as per Ajeh's suggestions? It seems to me a pretty big gap to not include products with attributes in the in_cart() function.

For Google Indexing: (I searched for these)
How do I figure out if a product has been added to cart
zen cart detect add to cart function
zen-cart check if product add to cart
zen-cart.com detect added to cart

There's also a closed thread from 2010 that's more visible in the Search Engines that should be linked to this solution: <http://www.zen-cart.com/showthread.php?146750-Already-in-Cart-code-sometimes-works>