Zen Cart Logo
Forums / General Questions / Whats wrong with my add_cart code?!

Whats wrong with my add_cart code?!

Locked

Views: 1,340

Results 1 to 4 of 4
This thread is locked. New replies are disabled.
30 Jan 2008, 9:30 PM
#1
enigmabomb avatar

enigmabomb

New Zenner

Join Date:
Jan 2008
Posts:
49
Plugin Contributions:
0

Whats wrong with my add_cart code?!

Hi,

I'm trying to write an observer, and it fails whenever I try to add an item to the cart. See below:

<?php
class platinum extends base 
{
	var $freeProductID = 75837;

	function platinum()
	{
		$_SESSION['cart']->attach($this,array('NOTIFIER_CART_ADD_CART_END'));
    }
    
    function update(&$callingClass, $notifier, $paramsArray) 
    {
		global $db;
		//die($_SESSION['customer_id']);
		$statement = "SELECT `customers_group_pricing` FROM `ch_customers` WHERE `customers_id` = ".(int)$_SESSION['customer_id'];
		$check = $db->Execute($statement);
		if($check->RecordCount() == 1)
			if ($check->fields["customers_group_pricing"]==2)
			{
				//$_SESSION['cart']->add_cart($this->freeProductID,1);
				$_SESSION['cart']->add_cart("75837");			
			}
    }
}
?>
1 Feb 2008, 1:51 PM
#2
wilt avatar

wilt

Oji-san

Join Date:
Jun 2003
Location:
Newcastle UK
Posts:
1,850
Plugin Contributions:
3

Re: Whats wrong with my add_cart code?!

HI,

did you create an autoload entry in includes/autoloaders
and how did you define that.

Also when testing this, are you testing as a logged in customer, One thing you should probably do is create another notify watcher for a person being logged in, as, based on your code, if a guest adds an item to their cart they will not get the free item, even when they do login.

1 Feb 2008, 7:50 PM
#3
enigmabomb avatar

enigmabomb

New Zenner

Join Date:
Jan 2008
Posts:
49
Plugin Contributions:
0

Re: Whats wrong with my add_cart code?!

Hi Wilt,

Here is my include autoloader file named, config.platinum.php

<?php
$autoLoadConfig[90][] = array('autoType'=>'class',
                              'loadFile'=>'observers/class.platinum.php');
$autoLoadConfig[90][] = array('autoType'=>'classInstantiate',
                              'className'=>'platinum',
                              'objectName'=>'platinum');

?>

My code is attempting the following:

If the person is in a certain group, whenever they add something to their cart, it checks to see if the cart contains a certain item. If not, add it. Then, whenever they try to remove something from their cart, it checks to see if they're removing the autmoatically loaded item. If they are, add it again.

I don't want someone to be able to check out without buying item X. Item X is NOT free.

Thanks for your help,

Joshua

1 Feb 2008, 10:58 PM
#4
wilt avatar

wilt

Oji-san

Join Date:
Jun 2003
Location:
Newcastle UK
Posts:
1,850
Plugin Contributions:
3

Re: Whats wrong with my add_cart code?!

k,

Unforunately that is not what the code posted is doing.

What your code is doing, is checking if the current customer is in a certain group, and if they have just added something to the cart then add the special cart item(75837) to the cart.

Unfortunately this will not work for a couple of reasons.

  1. it only works once the customer is logged in (it will ignore anything the customer does before logging in. So if they add something as a guest, then login, and then checkout, they will not get the special item.

  2. it is not checking if the special item is already in the cart. so could add the special item multiple times.

U would be better off choosing a different notifier, from the checkout stage.

Its difficult to tell u which one to use, as this depends on how the special item would affect shipping costs. etc.

For example u could use this modifier - NOTIFY_HEADER_START_CHECKOUT_SHIPPING

U would then check the customers group status and if it matched what u want then check if the special item was in the cart
using $_SESSION['cart']->in_cart(75837) and if it is not in the cart, add it to the cart.

HTH