Forums / Templates, Stylesheets, Page Layout / Notifier/Observer problem editing Real World Example 1

Notifier/Observer problem editing Real World Example 1

Locked

Views: 3,998

Results 1 to 20 of 24
This thread is locked. New replies are disabled.
19 Feb 2010, 3:40 AM
#1
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Notifier/Observer problem editing Real World Example 1

Hi, i am having problem understanding the wiki developer's api tutorial. i followed the first real world example and it works perfectly fine. what i don't understand is "$this->attach($this, array('NOTIFIER_CART_ADD_CART_END'))" why is that the "$this" is attached to the event and in the real world example 1 is attached in a $_SESSION['class']. can anyone explain to me why. and also i have experimented in the real world example 1. i removed the condition "if ($_SESSION['cart']->show_total() >= $this->freeAmount && !$_SESSION['cart']->in_cart($this->freeProductID) )
{

}" and when i tried to add a product in the cart an error occur. the error is INTERNAL SERVER ERROR 500. can anyone tell me why is that happening it is just a statement why is an error comes up.

19 Feb 2010, 10:41 AM
#2
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

Anyone i haven't found a solution to this problem for over a month now

20 Feb 2010, 2:18 AM
#3
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

any help would be appreciated

20 Feb 2010, 2:48 PM
#4
lat9 avatar

lat9

Administrator

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

Re: Notifier/Observer problem editing Real World Example 1

I've seen 500-errors when my changes caused an infinite loop.:blush:

21 Feb 2010, 12:44 AM
#5
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

i don't think its a looping issue cause i can't find any infinite loop in the code

21 Feb 2010, 2:55 AM
#6
ajeh avatar

ajeh

Oba-san

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

Re: Notifier/Observer problem editing Real World Example 1

Best I can tell you is the line in the real world is calling the other one ...

You could be addressing a class at the wrong point or something ...

There is not enough information on what you are trying to do here ...

Perhaps later, when wilt or DrByte are free and handy they can help you further on that ...

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!

21 Feb 2010, 6:37 AM
#7
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

<?php
/**
 * Observer class used to add a free product to the cart if the user spends more than $x
 *
 */
class freeProduct extends base {
  /**
   * The threshold amount the customer needs to spend.
   * 
   * Note this is defined in the shops base currency, and so works with multi currency shops
   *
   * @var decimal
   */
  var $freeAmount = 50;
  /**
   * The id of the free product.
   * 
   * Note. This must be a true free product. e.g. price = 0 Also make sure that if you don't want the customer
   * to be charged shipping on this, that you have it set correctly.
   *
   * @var integer
   */
  var $freeProductID = 57;
  /**
   * constructor method
   * 
   * Attaches our class to the $_SESSION['cart'] class and watches for 2 notifier events.
   */
  function freeProduct() {
    $_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END'));
  }
  /**
   * Update Method
   * 
   * Called by observed class when any of our notifiable events occur
   *
   * @param object $class
   * @param string $eventID
   */
  function update(&$class, $eventID, $paramsArray = array()) {
  
    if ($eventID == 'NOTIFIER_CART_REMOVE_END' && (isset($_SESSION['freeProductInCart']) && $_SESSION['freeProductInCart'] == TRUE ))
  {
    if (!$_SESSION['cart']->in_cart($this->freeProductID))
    {
      $_SESSION['userRemovedFreeProduct'] = TRUE;
    }
  }

  if (!isset($_SESSION['userRemovedFreeProduct']) || $_SESSION['userRemovedFreeProduct'] != TRUE) 
  {
    if ($_SESSION['cart']->show_total() >= $this->freeAmount && !$_SESSION['cart']->in_cart($this->freeProductID) ) // when this statement is removed i get an error  
    {	
	  
      $_SESSION['cart']->add_cart($this->freeProductID);
      $_SESSION['freeProductInCart'] = TRUE;  
    }
  }
 
  if ($_SESSION['cart']->show_total() < $this->freeAmount && $_SESSION['cart']->in_cart($this->freeProductID) ) 
  {
    $_SESSION['cart']->remove($this->freeProductID);
  }

 
  if ($_SESSION['cart']->in_cart($this->freeProductID)) 
  {
    $_SESSION['cart']->contents[$this->freeProductID]['qty'] = 1;
  }
   
  }  
}

?>

thanks man. that is the code on wiki. "if ($_SESSION['cart']->show_total() >= $this->freeAmount && !$_SESSION['cart']->in_cart($this->freeProductID) ) " when that statement is remove i get an error the page wont load properly.

21 Feb 2010, 9:06 AM
#8
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

can you tell me what does it mean when I attach an event to a class just like $_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END'));. Is it like I will include that script to that function?? that is why I can't easily use an add_cart method because other function is using that method?

21 Feb 2010, 4:05 PM
#9
wilt avatar

wilt

Oji-san

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

Re: Notifier/Observer problem editing Real World Example 1

Hi,

When you do

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

you are telling the observer/notifier system(ONS) that you want to watch for 2 events 'NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END'

Whenever those events happen, the ONS will call the update method of the class you passed a reference for($this, which in the example is the freeProduct class)

the update method for the freeProduct class will be called twice. Once when the NOTIFIER_CART_ADD_CART_END happens and again when the 'NOTIFIER_CART_REMOVE_END event happens.

When the update method is called it receives a copy of the class that called it(In the example the $_SESSION['cart'] class, and details of the notifier that caused the event to happen. (ie either NOTIFIER_CART_ADD_CART_END or NOTIFIER_CART_REMOVE_END

HTH

Ian

22 Feb 2010, 12:26 AM
#10
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

Thank you very much for the explanation, i kind of get it now how it works.

4 Mar 2010, 2:37 AM
#12
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

I really want to know how to customize the code and make modules in zen cart but there are few support for this. i can't continue learning if i can't figure out why there is an error when removing just a simple statement

4 Mar 2010, 2:45 AM
#13
drbyte avatar

drbyte

Sensei

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

Re: Notifier/Observer problem editing Real World Example 1

dale88:

Has anyone have an idea why there is an error when that statement is removed. This is the error on my error log "File does not exist: /home/proje/public_html/500.shtml, referer: http://www.projects.info/2010/zencart/index.php?main_page=product_info&cPath=3_13&products_id=8"
The "500.shtml" file is called when the server encounters a "500" error, and the 500.shtml file is used to display that error message to the browser. Evidently you don't have such a file, so the error cannot be displayed. Hence the "file does not exist" message you're seeing.

A "500" error is a "Internal Server Error", which is triggered by the webserver, and not by Zen Cart. The reasons for triggering the error are documented in the server's apache errorlog. You may have to ask your hosting company for access to that information.

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

4 Mar 2010, 2:49 AM
#14
drbyte avatar

drbyte

Sensei

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

Re: Notifier/Observer problem editing Real World Example 1

As to why removing a line of code is causing you some errors, it's very difficult to troubleshoot what syntax problems you may have introduced by making changes.

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

4 Mar 2010, 3:10 AM
#15
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

I know the problem is an internal server error. my problem is what is causing that ```php

<?php /** * Observer class used to add a free product to the cart if the user spends more than $x * */ class freeProduct extends base { /** * The threshold amount the customer needs to spend. * * Note this is defined in the shops base currency, and so works with multi currency shops * * @var decimal */ var $freeAmount = 50; /** * The id of the free product. * * Note. This must be a true free product. e.g. price = 0 Also make sure that if you don't want the customer * to be charged shipping on this, that you have it set correctly. * * @var integer */ var $freeProductID = 57; /** * constructor method * * Attaches our class to the $_SESSION['cart'] class and watches for 2 notifier events. */ function freeProduct() { $_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END')); } /** * Update Method * * Called by observed class when any of our notifiable events occur * * @param object $class * @param string $eventID */ function update(&$class, $eventID, $paramsArray = array()) { if ($eventID == 'NOTIFIER_CART_REMOVE_END' && (isset($_SESSION['freeProductInCart']) && $_SESSION['freeProductInCart'] == TRUE )) { if (!$_SESSION['cart']->in_cart($this->freeProductID)) { $_SESSION['userRemovedFreeProduct'] = TRUE; } } if (!isset($_SESSION['userRemovedFreeProduct']) || $_SESSION['userRemovedFreeProduct'] != TRUE) { if ($_SESSION['cart']->show_total() >= $this->freeAmount && !$_SESSION['cart']->in_cart($this->freeProductID) ) // when this statement is removed i get an error { $_SESSION['cart']->add_cart($this->freeProductID); $_SESSION['freeProductInCart'] = TRUE; } } if ($_SESSION['cart']->show_total() < $this->freeAmount && $_SESSION['cart']->in_cart($this->freeProductID) ) { $_SESSION['cart']->remove($this->freeProductID); } if ($_SESSION['cart']->in_cart($this->freeProductID)) { $_SESSION['cart']->contents[$this->freeProductID]['qty'] = 1; } } } ?>

I was experimenting on this code on WIKI API tutorial. i tried to remove this condition statement "  if ($_SESSION['cart']->show_total() < $this->freeAmount && $_SESSION['cart']->in_cart($this->freeProductID) ) 
  {" that is when the error comes up when i'm trying to add a product in the cart. i scanned shopping cart class and still haven't figured why it has to have that statement for the class to work
4 Mar 2010, 3:17 AM
#16
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

i hope you don't mind testing this on your zen cart test site

4 Mar 2010, 5:04 AM
#17
drbyte avatar

drbyte

Sensei

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

Re: Notifier/Observer problem editing Real World Example 1

dale88:

I was experimenting on this code on WIKI API tutorial. i tried to remove this condition statement " if ($_SESSION['cart']->show_total() < $this->freeAmount && $_SESSION['cart']->in_cart($this->freeProductID) )
{
" that is when the error comes up when i'm trying to add a product in the cart. i scanned shopping cart class and still haven't figured why it has to have that statement for the class to work
But ... an "if" clause which contains an opening brace { must also include a closing brace }
And, if you only removed the "if" condition and its opening brace, then you've created an ugly PHP error situation.
If you really want to remove that particular "if" clause, you need to remove the whole thing: ```
if ($_SESSION['cart']->show_total() < $this->freeAmount && $_SESSION['cart']->in_cart($this->freeProductID) )
{
$_SESSION['cart']->remove($this->freeProductID);
}

I recommend going back to the PHP website and studying the code syntax before adjusting further PHP code.

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

4 Mar 2010, 5:16 AM
#18
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

Yes i know it will cause php errors if you don't remove the closing bracket. i removed the condition including the closing bracket.

so the script looks like this

<?php
/**
 * Observer class used to add a free product to the cart if the user spends more than $x
 *
 */
class freeProduct extends base {
  /**
   * The threshold amount the customer needs to spend.
   * 
   * Note this is defined in the shops base currency, and so works with multi currency shops
   *
   * @var decimal
   */
  var $freeAmount = 100;
  /**
   * The id of the free product.
   * 
   * Note. This must be a true free product. e.g. price = 0 Also make sure that if you don't want the customer
   * to be charged shipping on this, that you have it set correctly.
   *
   * @var integer
   */
  var $freeProductID = 15;
  /**
   * constructor method
   * 
   * Attaches our class to the $_SESSION['cart'] class and watches for 2 notifier events.
   */
  function freeProduct() {
    $_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END'));
  }
  /**
   * Update Method
   * 
   * Called by observed class when any of our notifiable events occur
   *
   * @param object $class
   * @param string $eventID
   */
  function update(&$class, $eventID, $paramsArray = array()) {

  if ($eventID == 'NOTIFIER_CART_REMOVE_END' && (isset($_SESSION['freeProductInCart']) && $_SESSION['freeProductInCart'] == TRUE ))
  {
    if (!$_SESSION['cart']->in_cart($this->freeProductID))
    {
      $_SESSION['userRemovedFreeProduct'] = TRUE;
    }
  }

  if (!isset($_SESSION['userRemovedFreeProduct']) || $_SESSION['userRemovedFreeProduct'] != TRUE) 
  {

      $_SESSION['cart']->add_cart($this->freeProductID);
      $_SESSION['freeProductInCart'] = TRUE;  
    }
  
 
  if ($_SESSION['cart']->show_total() < $this->freeAmount && $_SESSION['cart']->in_cart($this->freeProductID) ) 
  {
    $_SESSION['cart']->remove($this->freeProductID);
  }

  if ($_SESSION['cart']->in_cart($this->freeProductID)) 
  {
    $_SESSION['cart']->contents[$this->freeProductID]['qty'] = 1;
  }
   
  }  
}
?>
4 Mar 2010, 5:53 AM
#19
drbyte avatar

drbyte

Sensei

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

Re: Notifier/Observer problem editing Real World Example 1

Maybe because you're removing some critical logic which protects against re-adding the item to the cart if it's already there? perhaps thus causing an endless loop?

I'm not sure why you would choose to remove that part of the logic in the first place.

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

4 Mar 2010, 6:24 AM
#20
dale88 avatar

dale88

New Zenner

Join Date:
Feb 2010
Posts:
23
Plugin Contributions:
0

Re: Notifier/Observer problem editing Real World Example 1

I got you. i wanna know the details why is that statement important on the logic. I'm just experimenting the code for learning reasons. Can't seem to find out why is that statement caused errors when removed. all its do is check if amount is more than the free amount. for me, when that statement is removed it should automatically adds the free product on the cart regardless of the amount in the cart. that's what i think the program flow is.