Forums / All Other Contributions/Addons / Free Gift with purchase of $X or more - from Wiki - problems

Free Gift with purchase of $X or more - from Wiki - problems

Locked

Started by monkeytown on 30 Oct 2008, 6:34 PM · Views: 2,172

Results 1 to 12 of 12
This thread is locked. New replies are disabled.
30 Oct 2008, 6:34 PM
#1
monkeytown avatar

monkeytown

New Zenner

Join Date:
Nov 2007
Posts:
78
Plugin Contributions:
0

Free Gift with purchase of $X or more - from Wiki - problems

I have added the code from the Zen Wiki regarding getting a free gift with a large enough purchase. When I add the files to the proper places, it breaks the site whenever there is a change to the shopping cart. This includes logging in to an account with item in a saved cart. The first item will add/remove (such as setting all items to 0 and hitting update cart) but the page won't load (at all) and I have to reload the page and then remove/add another item.

One odd thing is that it will not right itself even when the files are removed or commented out (unless I restart). Also, it did seem to be working but I had my product_id wrong and when I changed it it broke the page again.

I copied the code exactly although I changed the file name slightly and the corresponding reference in the autoload file, and changing the breakpoint to 95 for some reason. Any help is appreciated. I will keep trying to sort through this in the mean time.

includes/auto_loaders/config.premiums.php

<?php

$autoLoadConfig[95][] = array('autoType'=>'class',
                              'loadFile'=>'observers/class.premiums.php');
$autoLoadConfig[95][] = array('autoType'=>'classInstantiate',
                              'className'=>'freeProduct',
                              'objectName'=>'freeProduct');
/**/
?>

includes/classes/observers/class.premiums.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 = 83133;
  /**
   * 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) {
    if ($_SESSION['cart']->show_total() >= $this->freeAmount && !$_SESSION['cart']->in_cart($this->freeProductID) ) {
      $_SESSION['cart']->add_cart($this->freeProductID);
    }
    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;
    }
  }
}
?>

Of course I went mad with power. Have you ever tried going mad without power? It's boring. No one listens to you.

30 Oct 2008, 7:07 PM
#2
monkeytown avatar

monkeytown

New Zenner

Join Date:
Nov 2007
Posts:
78
Plugin Contributions:
0

Re: Free Gift with purchase of $X or more - from Wiki - problems

The problem seemed to be the trailing ?> at the end of the file... is that possible? I remember reading around somewhere (here possibly) that that is left off for some reason... I didn't know it could potentially break the file.

Of course I went mad with power. Have you ever tried going mad without power? It's boring. No one listens to you.

30 Oct 2008, 8:31 PM
#3
ajeh avatar

ajeh

Oba-san

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

Re: Free Gift with purchase of $X or more - from Wiki - problems

You can leave off the last one ...

However, if included, make sure you do not leave any space(s) or blank line(s) after the end of the ?> or you will get errors ...

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!

31 Oct 2008, 12:50 AM
#4
drbyte avatar

drbyte

Sensei

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

Re: Free Gift with purchase of $X or more - from Wiki - problems

Have you tried the debug utility? step 2a: https://www.zen-cart.com/tutorials/index.php?article=82

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

31 Oct 2008, 8:03 PM
#5
monkeytown avatar

monkeytown

New Zenner

Join Date:
Nov 2007
Posts:
78
Plugin Contributions:
0

Re: Free Gift with purchase of $X or more - from Wiki - problems

@linda - That was likely the case with white space after the ?>

@Dr Byte - I hadn't used that, or even known of it's existence. I am on my dev cpu so I have access to the error logs but none were being produced in this case. Thanks for the tip :)

Of course I went mad with power. Have you ever tried going mad without power? It's boring. No one listens to you.

4 Nov 2008, 5:21 PM
#6
countrycharm avatar

countrycharm

Totally Zenned

Join Date:
Jul 2007
Posts:
2,179
Plugin Contributions:
2

Re: Free Gift with purchase of $X or more - from Wiki - problems

monkeytown:

I have added the code from the Zen Wiki regarding getting a free gift with a large enough purchase. When I add the files to the proper places, it breaks the site whenever there is a change to the shopping cart. This includes logging in to an account with item in a saved cart. The first item will add/remove (such as setting all items to 0 and hitting update cart) but the page won't load (at all) and I have to reload the page and then remove/add another item.

One odd thing is that it will not right itself even when the files are removed or commented out (unless I restart). Also, it did seem to be working but I had my product_id wrong and when I changed it it broke the page again.

I copied the code exactly although I changed the file name slightly and the corresponding reference in the autoload file, and changing the breakpoint to 95 for some reason. Any help is appreciated. I will keep trying to sort through this in the mean time.

includes/auto_loaders/config.premiums.php

<?php $autoLoadConfig[95][] = array('autoType'=>'class', 'loadFile'=>'observers/class.premiums.php'); $autoLoadConfig[95][] = array('autoType'=>'classInstantiate', 'className'=>'freeProduct', 'objectName'=>'freeProduct'); /**/ ?>
> 
> includes/classes/observers/class.premiums.php
> ```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 = 83133;
  /**
   * 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) {
    if ($_SESSION['cart']->show_total() >= $this->freeAmount && !$_SESSION['cart']->in_cart($this->freeProductID) ) {
      $_SESSION['cart']->add_cart($this->freeProductID);
    }
    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;
    }
  }
}
?>

My file names is class.get_it_free.php I uploaded them to where it says to and I can not get this to work. I have it set to give away a free product when they reach $75.00 but when I test it, it never shows up. What am I doing wrong. Thank you.

<?php

$autoLoadConfig[95][] = array('autoType'=>'class',
                              'loadFile'=>'observers/class.get_it_free.php');
$autoLoadConfig[95][] = array('autoType'=>'classInstantiate',
                              'className'=>'freeProduct',
                              'objectName'=>'freeProduct');
/**/
?>
<?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 = 75;
  /**
   * 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 = 38722;
  /**
   * 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) {
    if ($_SESSION['cart']->show_total() >= $this->freeAmount && !$_SESSION['cart']->in_cart($this->freeProductID) ) {
      $_SESSION['cart']->add_cart($this->freeProductID);
    }
    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;
    }
  }
}
?>

Is your site Upgraded to the current version 1.5.4 Yet?
zencart-upgrades-website-installation

5 Nov 2008, 9:30 PM
#7
countrycharm avatar

countrycharm

Totally Zenned

Join Date:
Jul 2007
Posts:
2,179
Plugin Contributions:
2

Re: Free Gift with purchase of $X or more - from Wiki - problems

I got it to work thanks anyway.

Is your site Upgraded to the current version 1.5.4 Yet?
zencart-upgrades-website-installation

11 Nov 2008, 12:26 AM
#8
aly22 avatar

aly22

Zen Follower

Join Date:
Feb 2008
Posts:
174
Plugin Contributions:
0

Re: Free Gift with purchase of $X or more - from Wiki - problems

Randy, can you please explain how you got it to work?
Thanks much :)

11 Nov 2008, 7:33 PM
#10
aly22 avatar

aly22

Zen Follower

Join Date:
Feb 2008
Posts:
174
Plugin Contributions:
0

Re: Free Gift with purchase of $X or more - from Wiki - problems

Thank you, Randy! That worked perfectly!!

11 Nov 2008, 8:50 PM
#11
countrycharm avatar

countrycharm

Totally Zenned

Join Date:
Jul 2007
Posts:
2,179
Plugin Contributions:
2

Re: Free Gift with purchase of $X or more - from Wiki - problems

No problem glad to help out.

Is your site Upgraded to the current version 1.5.4 Yet?
zencart-upgrades-website-installation

20 Jan 2009, 1:59 AM
#12
conmac863 avatar

conmac863

New Zenner

Join Date:
Jan 2009
Posts:
13
Plugin Contributions:
0

Re: Free Gift with purchase of $X or more - from Wiki - problems

Can you have multiple free items at different price breaks?
$25 Item A
$50 Item B

Etc.

Thanks
Sean

Sean - Zenner newbie
GreasedLightningDirect - Buy Direct from the manufacturer.