-
Notifications
You must be signed in to change notification settings - Fork 0
/
uc_promotions.module
139 lines (118 loc) · 3.65 KB
/
uc_promotions.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* @file Ubercart Promotions main module
*/
/**
* Implements hook_perm
* Valid permissions for this module
*/
function uc_promotions_perm() {
return array('access ubercart promotions');
}
/**
* Implements hook_menu
*
*/
function uc_promotions_menu(){
$items['admin/store/uc_promotions'] = array(
'title' => t('Promotions'),
'description' => t('Active promotions'),
'access arguments' => array('access ubercart promotions'),
'page callback' => 'uc_promotions_get_promos',
'type' => MENU_NORMAL_ITEM,
'file' => 'uc_promotions.inc',
);
$items['admin/store/uc_promotions/view'] = array(
'title' => t('View promotions'),
'description' => t('Active promotions'),
'access arguments' => array('access ubercart promotions'),
'page callback' => 'uc_promotions_get_promos',
'type' => MENU_NORMAL_ITEM,
'file' => 'uc_promotions.inc',
);
$items['admin/store/uc_promotions/create'] = array(
'title' => t('Create a promotion'),
'description' => t('In this area you can add a promotion'),
'access arguments' => array('access ubercart promotions'),
'page callback' => 'uc_promotions_create_promo',
'type' => MENU_NORMAL_ITEM,
'file' => 'uc_promotions.inc',
);
$items['admin/store/uc_promotions/%uc_promotions_id/edit'] = array(
'title' => t('Modifica promozione'),
'description' => t('In this area you can edit a promotion'),
'access arguments' => array('access ubercart promotions'),
'page callback' => 'uc_promotions_edit_promo',
'page arguments' => array(3),
'type' => MENU_NORMAL_ITEM,
'file' => 'uc_promotions.inc',
);
return $items;
}
/*
* Load a promo, called from wildcard menu item
*/
function uc_promotions_id_load($promo_id){
$sql = "SELECT * FROM {uc_promotionstions} WHERE promoid = %d";
$result = db_query($sql, $promo_id);
$results = array();
while($data = db_fetch_object($result)){
$results['promoid'] = $data->promoid;
$results['nid'] = $data->nid;
$results['price_limit'] = $data->price_limit;
$results['domain'] = $data->domain;
$results['status'] = $data->status;
}
return $results;
}
/**
* Implements hook_add_to_cart
* Inject a promotion inside the cart when a specific condition is satisfied
*/
function uc_promotions_uc_cart_alter(&$items) {
//loading active promos
$sql = "SELECT * FROM {uc_promotionstions} WHERE status=%d";
$result = db_query($sql, 1);
$retuls = array();
while($data = db_fetch_object($result)){
//applying the promotion
uc_promotions_handler($data->nid, $data->price_limit, $data->domain, $items);
}
}
/**
* Promotion handler
*/
function uc_promotions_handler($nid, $price_limit, $domain, &$items){
$domain = isset($domain) ? $domain : $_SERVER['HTTP_HOST'];
if($_SERVER['HTTP_HOST'] != $domain){
return;
}
// Offer
$promo_free_id = $nid;
$promo_data = array("module" => "uc_product");
//Check the current cart total and determine the total price
$price = 0;
foreach($items as $item){
$price += ($item->qty * $item->price);
}
//Check for promo products inside list
$item_check = 0;
foreach($items as $item){
if($item->nid == $promo_free_id) {
//Populate $item_check
$item_check = 1;
//Force the promotion qty to 1
if($item->qty > 1){
$item->qty = 1;
}
}
}
//Add the promotion
if($price >= $price_limit && $item_check == 0){
uc_cart_add_item($promo_free_id, $qty = 1, $data = NULL, $cid = NULL, $msg = "", $check_redirect = TRUE, $rebuild = TRUE);
}
//Remove the promotion
if($price < $price_limit){
uc_cart_remove_item($promo_free_id, null, $promo_data);
}
}