30 Jan 2014, 2:11 AM
New Zenner
- Join Date:
- Dec 2012
- Posts:
- 5
- Plugin Contributions:
- 0
Free gift with purchase for certain category only
Hi,
I am using the "real world example" in the wiki https://www.zen-cart.com/wiki/index.php/Developers_API_Tutorials#A_Real_World_Example
to add a free gift to the customers shopping cart when spending over a certain amount
I want it to apply only to purchases within a certain category
is that possible?
this is my code
<?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 = 10; /** * 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 = 422; /** * 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; } } } ?>