New Zenner
- Join Date:
- Jun 2011
- Posts:
- 8
- Plugin Contributions:
- 0
Free shipping on product when certain quantity is bought from a manufacturer.
Hi all.
I am trying to add a condition to my store, whenever a customer buys certain amount from one selected manufacturer products from that manufacturer have free shipping.
The shipping module I use is ground shipping from Fedex web services. But I want this change to be shipping method agnostic.
I am not a Zencart expert, this is me second attempt for code modification. So I build an observer. Here is the code:
<?php
/**
* Observer class used to add a free shipping to a product product to the cart
* if the user spends more than $x on products from an specific manufacturer
*/
class freeShippingOnManufacturer extends base {
var $freeManufacturers = array();
var $cartManufacturers = array();
var $productsTotalInCart = array();
/**
* constructor method
*
* Attaches our class to the $_SESSION['cart'] class and watches for 2 notifier events.
*/
function __construct() {
$_SESSION['cart']->attach($this, array('NOTIFIER_CART_ADD_CART_END', 'NOTIFIER_CART_REMOVE_END', 'NOTIFIER_CART_UPDATE_QUANTITY_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()) {
global $db;
//get manufacturers with free shipping option
$getfreeshippingmanufacturerssql = "SELECT manufacturers_id, min_value FROM manufacturers_free_shipping WHERE active = '1'";
$resultm = $db->Execute($getfreeshippingmanufacturerssql);
$freeManIds = array();
if ($resultm->RecordCount() > 0) {
while (!$resultm->EOF) {
$this->freeManufacturers[$resultm->fields['manufacturers_id']] = $resultm->fields['min_value'];
$freeManIds[] = $resultm->fields['manufacturers_id'];
$resultm->MoveNext();
}
}
foreach($_SESSION['cart']->contents as $key => $value){
if(strstr($key,':')){
$expl = explode(':',$key);
$pid = $expl[0];
}else{
$pid = $key;
}
$manufacturersql = "SELECT manufacturers_id FROM " . TABLE_PRODUCTS ." WHERE products_id = '".$pid."'";
$result = $db->Execute($manufacturersql);
if ($result->RecordCount() > 0) {
if(in_array($result->fields['manufacturers_id'],$freeManIds)){
$this->cartManufacturers[$result->fields['manufacturers_id']] += $_SESSION['cart']->in_cart_product_total_price($pid);
$free_shipping_products_price += $_SESSION['cart']->in_cart_product_total_price($pid);
$weight_to_reduce += $_SESSION['cart']->in_cart_product_total_weight($key);
$items_to_free += $value['qty'];
}
}
}
if($this->freeManufacturers[$result->fields['manufacturers_id']] <= $this->cartManufacturers[$result->fields['manufacturers_id']]){
$_SESSION['cart']->free_shipping_item = $_SESSION['cart']->free_shipping_item + $items_to_free;
$_SESSION['cart']->weight = $_SESSION['cart']->weight - $weight_to_reduce;
$_SESSION['cart']->free_shipping_weight = $_SESSION['cart']->free_shipping_weight + $weight_to_reduce;
$_SESSION['cart']->free_shipping_price = $_SESSION['cart']->free_shipping_price + $free_shipping_products_price;
}
}
}
I don't what else should be done other than modify the cart object to reflect the new free shipping values:
$_SESSION['cart']->free_shipping_item = $_SESSION['cart']->free_shipping_item + $items_to_free;
$_SESSION['cart']->weight = $_SESSION['cart']->weight - $weight_to_reduce;
$_SESSION['cart']->free_shipping_weight = $_SESSION['cart']->free_shipping_weight + $weight_to_reduce;
$_SESSION['cart']->free_shipping_price = $_SESSION['cart']->free_shipping_price + $free_shipping_products_price;
The table manufacturers_free_shipping has the following structure:
CREATE TABLE IF NOT EXISTS `manufacturers_free_shipping` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`manufacturers_id` int(11) NOT NULL,
`min_value` float NOT NULL,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
I don't know what else should be done in order for those products to be free shipping.
Any help will be really appreciated :)
Thanks