| 1 |
<?php |
<?php |
| 2 |
// $Id: uc_discounts_product.module,v 1.1.2.3 2008/11/01 00:16:52 psynaptic Exp $ |
// $Id: uc_discounts_product.module,v 1.1.2.4 2008/11/01 00:18:59 psynaptic Exp $ |
| 3 |
|
|
| 4 |
/******************************************************************************* |
/******************************************************************************* |
| 5 |
* Hook Functions (Ubercart) |
* Hook Functions (Ubercart) |
| 133 |
|
|
| 134 |
function uc_discounts_product_apply($op, $action, $total_price, &$cart) { |
function uc_discounts_product_apply($op, $action, $total_price, &$cart) { |
| 135 |
// get conditions to make sure products can't be used more than once for |
// get conditions to make sure products can't be used more than once for |
|
// a discount |
|
|
|
|
|
//drupal_set_message("uc_discounts_product_apply: $op $action $total_price ". print_r($cart)); |
|
| 136 |
$sql = "SELECT * FROM {uc_discounts_conditions} WHERE discount_id=%d "; |
$sql = "SELECT * FROM {uc_discounts_conditions} WHERE discount_id=%d "; |
|
// removing the following lines per kiruva [http://www.ubercart.org/contrib/143#comment-8814] |
|
|
// $sql .= "AND property='product' "; |
|
|
// $sql .= "AND item_id='%s' "; |
|
| 137 |
$sql .= "ORDER BY condition_group, weight"; |
$sql .= "ORDER BY condition_group, weight"; |
| 138 |
$result = db_query($sql, $action->discount_id, $action->item_id); |
$result = db_query($sql, $action->discount_id, $action->item_id); |
| 139 |
|
|
|
//ob_start(); print_r($action); |
|
|
//drupal_set_message(" hit uc_discounts_product_apply: "); |
|
|
//drupal_set_message(ob_get_contents()); ob_end_clean(); |
|
|
//drupal_set_message(t("[$sql]",array('%d'=> $action->discount_id,'%s' => $action->item_id))); |
|
|
|
|
| 140 |
$condition_list = array(); |
$condition_list = array(); |
| 141 |
while ($row = db_fetch_object($result)) { |
while ($row = db_fetch_object($result)) { |
| 142 |
$condition_list[$row->id] = $row; |
$condition_list[$row->id] = $row; |
| 146 |
return 0; |
return 0; |
| 147 |
} |
} |
| 148 |
|
|
|
// make sure condition is satisfied and action is possible |
|
| 149 |
$condition_found = FALSE; |
$condition_found = FALSE; |
| 150 |
$product_price = 0; |
$product_price = 0; |
|
//drupal_set_message("stepping through cart items to see if condition is met and action is possible:"); |
|
| 151 |
foreach ($cart as $index => $product) { |
foreach ($cart as $index => $product) { |
|
//ob_start(); print_r($product); |
|
|
//drupal_set_message("cart product: ".ob_get_contents()); ob_end_clean(); |
|
| 152 |
// has the condition already been satisified |
// has the condition already been satisified |
| 153 |
if (empty($condition_found)) { |
if (empty($condition_found)) { |
|
//drupal_set_message(" stepping through condition_list:"); |
|
| 154 |
foreach ($condition_list as $condition) { |
foreach ($condition_list as $condition) { |
|
//ob_start(); print_r($condition); |
|
|
//drupal_set_message("condition: ".ob_get_contents()); ob_end_clean(); |
|
| 155 |
if ($product->nid == intval($condition->item_id)) { |
if ($product->nid == intval($condition->item_id)) { |
| 156 |
// product id (or type) matches condition item |
// product id (or type) matches condition item |
|
//drupal_set_message(" condition ".$condition->id."(".$condition->item_id.") matched ".$product->nid." (".$product->title.")"); |
|
| 157 |
$required_qty = $condition->value; |
$required_qty = $condition->value; |
| 158 |
if ($condition->item_id == intval($action->item_id)) { |
if ($condition->item_id == intval($action->item_id)) { |
| 159 |
// condition item and action item are the same |
// condition item and action item are the same |
| 160 |
$product_price = $product->price; |
$product_price = $product->price; |
|
// $product_found[$index] = $product->qty; |
|
|
// $product_count += $product->qty; |
|
| 161 |
} |
} |
|
|
|
|
// if ($product->qty >= $required_qty) { |
|
|
// save to apply discounts later |
|
|
// $condition_found = TRUE; |
|
|
// break; |
|
|
// } |
|
| 162 |
} |
} |
| 163 |
} |
} |
| 164 |
} |
} |
| 165 |
|
|
| 166 |
if ($product_price && $condition_found) { |
if ($product_price && $condition_found) { |
| 167 |
// condition and action have been found |
// condition and action have been found |
|
//drupal_set_message(" condition and action are satisfied.. applying discount:"); |
|
| 168 |
break; |
break; |
| 169 |
} |
} |
| 170 |
if ($product->nid == $action->item_id) { |
if ($product->nid == $action->item_id) { |
| 171 |
$qty = $product->qty; |
$qty = $product->qty; |
|
//drupal_set_message("product nid matches ACTION id"); |
|
|
//ob_start(); print_r($product); |
|
|
//drupal_set_message(ob_get_contents()); ob_end_clean(); |
|
|
//if ($product->qty >= $action->qty) { |
|
| 172 |
$product_found[$index] = $product->qty; |
$product_found[$index] = $product->qty; |
| 173 |
$product_count += $product->qty; |
$product_count += $product->qty; |
| 174 |
$product_price = $product->price; |
$product_price = $product->price; |
| 175 |
continue; |
continue; |
|
//} |
|
| 176 |
} |
} |
| 177 |
} |
} |
| 178 |
|
|
| 180 |
return 0; |
return 0; |
| 181 |
} |
} |
| 182 |
|
|
|
//drupal_set_message(" calculating discount amount.."); |
|
| 183 |
// calculate discount amount |
// calculate discount amount |
| 184 |
if (substr($action->amount, -1) == '%') { |
if (substr($action->amount, -1) == '%') { |
| 185 |
$discount_percent = substr($action->amount, 0, -1) / 100; |
$discount_percent = substr($action->amount, 0, -1) / 100; |
| 206 |
$discount_amount = $product_price; |
$discount_amount = $product_price; |
| 207 |
} |
} |
| 208 |
$discount = $discount_amount * $product_count; |
$discount = $discount_amount * $product_count; |
|
//drupal_set_message("discount_amount: $discount_amount * product_count: ".$product_count); |
|
| 209 |
} |
} |
| 210 |
// no refunds |
// no refunds |
| 211 |
if ($discount > $total_price) { |
if ($discount > $total_price) { |
| 212 |
$discount = $total_price; |
$discount = $total_price; |
| 213 |
} |
} |
| 214 |
|
|
|
// remove affected items from cart |
|
|
/*foreach ($product_found as $cart_index => $product_count) { |
|
|
$cart[$cart_index]->qty -= $product_count; |
|
|
}*/ |
|
|
|
|
| 215 |
return $discount; |
return $discount; |
| 216 |
} |
} |