forked from andersonfranco/woocommerce-installments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-installments.php
executable file
·270 lines (234 loc) · 9.86 KB
/
woocommerce-installments.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* Plugin Name: WooCommerce Installments
* Plugin URI: https://github.com/AndersonFranco/woocommerce-installments
* Description: This plugin appends installments into the product price.
* Author: Anderson Franco
* Author URI: http://www.francotecnologia.com/
* Version: 1.4.0
* License: GPLv2 or later
*/
class FrancoTecnologiaWooCommerceInstallments {
// ONLY PRODUCTS WITH PRICE GREATER THAN OR EQUAL TO:
protected static $priceGreaterThanOrEqualTo = 0;
// MINIMUM MONTHLY PAYMENT - MUST BE GREATER THAN ZERO:
protected static $minimumMonthlyPayment = 5;
// NUMBER OF THE COLUMNS OF THE TABLE:
protected static $numberOfTableColumns = 2;
// ADD TO CART - BUTTON POSITION: TOP = true, BOTTOM = false
protected static $addToCartButtonPosition = false;
// SHOW COLUMN TOTAL
protected static $showColumnTotal = false;
// TABLE HEADER MESSAGE
protected static $tableHeaderMessage = "";
// TABLE FOOTER MESSAGE
protected static $tableFooterMessage = "";
// USE COEFFICIENT TABLE / INTEREST RATES:
protected static $useCoefficientTable = false;
// COEFFICIENT TABLE - INTEREST RATES:
// http://andersonfranco.github.io/capital-recovery-factor/
protected static $coefficientTable = array(
1, 0.52255, 0.35347,
0.26898, 0.21830, 0.18453,
0.16044, 0.14240, 0.12838,
0.11717, 0.10802, 0.10040
);
// USE DICTIONARY LANGUAGE (DEFAULT: ENGLISH)
protected static $useDictLanguage = true;
// IF $useDictLanguage == false, USE THIS WORDS:
protected static $language = array(
'or' => 'ou',
'Installments' => 'Parcelas',
'Amount' => 'Valor',
'Total' => 'Total',
'InStock' => 'Em estoque',
'OutOfStock' => 'Indisponível',
'cartPageMessage' => 'Pague em até %d vezes'
);
public static function init() {
if (static::$useDictLanguage) {
// Default English words
static::$language = array(
'or' => __('or'),
'Installments' => __('Installments'),
'Amount' => __('Amount'),
'Total' => __('Total'),
'InStock' => __('InStock'),
'OutOfStock' => __('OutOfStock'),
'cartPageMessage' => __('NO interest for %d months')
);
}
add_action('plugins_loaded', array(get_called_class(), 'actions'));
}
public static function actions() {
// Product Page
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
add_action('woocommerce_single_product_summary', array(get_called_class(), 'actionSingleProduct'), ((static::$addToCartButtonPosition)?30:10));
// Catalog
add_action('woocommerce_after_shop_loop_item_title', array(get_called_class(), 'actionLoopItem'), 20);
// Cart
if (static::$language['cartPageMessage'] != '') {
add_action('woocommerce_cart_totals_after_order_total', array(get_called_class(), 'actionCart'), 20);
}
// CSS
add_action('wp_enqueue_scripts', array(get_called_class(), 'css'), 98);
// Javascript
add_action('wp_enqueue_scripts', array(get_called_class(), 'js'), 99);
}
protected static function calculateInstallment($price = 0.00, $installment = 0) {
$price = (float) $price;
$installment = (int) $installment;
$result = new stdClass();
if ($installment < 1 || $installment > 12) {
$result->price = 0;
$result->total = 0;
} else if (static::$useCoefficientTable) {
$result->price = sprintf("%0.2f", $price * static::$coefficientTable[$installment - 1]);
$result->total = sprintf("%0.2f", ($price * static::$coefficientTable[$installment - 1]) * $installment);
} else {
$result->price = sprintf("%0.2f", ($price / $installment));
$result->total = sprintf("%0.2f", ($price / $installment) * $installment);
}
return $result;
}
protected static function getPrice($price = null) {
if ($price === null) {
$product = get_product();
if ($product->get_price()) {
$price = $product->get_price();
}
}
return $price;
}
protected static function getInstallments($price = 0.00) {
$installments = round($price / static::$minimumMonthlyPayment);
if ($installments > 12) {
$installments = 12;
} else if ($installments < 1) {
$installments = 1;
}
return $installments;
}
protected static function getParceledValue($price = null) {
$price = static::getPrice($price);
if ($price > 0) {
$installments = static::getInstallments($price);
$calc = static::calculateInstallment($price, $installments);
return $installments . 'x ' . wc_price($calc->price);
} else {
return '';
}
}
protected static function getParceledTable($price = null, $variationId = null, $variationDisplay = null) {
$price = static::getPrice($price);
if ($price > 0) {
$installments = static::getInstallments($price);
$table = '<table class="francotecnologia_wc_parcpagseg_table ';
$table .= 'francotecnologia_wc_parcpagseg_table_with_variation_id_' . ($variationId > 0 ? $variationId : '0') . '" ';
$table .= ($variationDisplay === false ? 'style="display:none"' : '');
$table .= '><thead>';
$tableColspan = (2 + (static::$showColumnTotal?1:0)) * static::$numberOfTableColumns;
if (static::$tableHeaderMessage != '') {
$table .= '<tr><th class="francotecnologia_wc_parcpagseg_table_header_message_tr_th" colspan="'
. $tableColspan . '">' . static::$tableHeaderMessage . '</th></tr>';
}
$table .= '<tr class="francotecnologia_wc_parcpagseg_table_header_tr">';
$table .= str_repeat('<th>' . static::$language['Installments'] . '</th><th>' . static::$language['Amount'] . '</th>'
. (static::$showColumnTotal ? '<th>' . static::$language['Total'] . '</th>':''), static::$numberOfTableColumns);
$table .= '</tr>';
$table .= '</thead><tbody>';
$tdCounter = 0;
foreach (range(1, $installments) as $parcel) {
$calc = static::calculateInstallment($price, $parcel);
$tdCounter = 1 + $tdCounter % static::$numberOfTableColumns;
if ($tdCounter == 1) {
$table .= '<tr>';
}
$table .= '<th>' . $parcel . '</th><td>' . wc_price($calc->price) . '</td>' . (static::$showColumnTotal ? '<td>' . wc_price($calc->total) . '</td>' : '');
if ($tdCounter == static::$numberOfTableColumns) {
$table .= '</tr>';
}
}
if (substr( $table, -5 ) != '</tr>') {
$table .= '</tr>';
}
$table .= '</tbody>';
if (static::$tableFooterMessage != '') {
$table .= '<tfoot><tr><th class="francotecnologia_wc_parcpagseg_table_footer_message_tr_th" colspan="'
. $tableColspan . '">' . static::$tableFooterMessage . '</th></tr></tfoot>';
}
$table .= '</table>';
return $table;
} else {
return '';
}
}
public static function actionLoopItem() {
if (static::getPrice() >= static::$priceGreaterThanOrEqualTo) {
echo ' <span class="price francotecnologia_wc_parcpagseg_loop_item_price_span">'
. (static::getPrice() > 0 ? static::$language['or'] . ' ' : '')
. static::getParceledValue() . '</span>';
}
}
public static function actionSingleProduct() {
if (static::getPrice() < static::$priceGreaterThanOrEqualTo) {
woocommerce_template_single_price();
return;
}
$product = get_product();
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<p class="price"><?php echo $product->get_price_html(); ?>
<span class="francotecnologia_wc_parcpagseg_single_product_price_span">
<?php echo (static::getPrice() > 0 ? static::$language['or'] . ' ' : '') . static::getParceledValue(); ?>
</span>
</p>
<?php
if ($product->product_type != 'variable') {
echo static::getParceledTable();
} else {
$variationList = $product->get_available_variations();
foreach($variationList AS $variation) {
$productVariation = new WC_Product_Variation($variation['variation_id']);
$defaultVariation = array_diff($variation['attributes'], $product->get_variation_default_attributes());
echo static::getParceledTable($productVariation->get_price(), $variation['variation_id'], empty($defaultVariation));
}
}
?>
<meta itemprop="price" content="<?php echo $product->get_price(); ?>" />
<meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? static::$language['InStock'] : static::$language['OutOfStock']; ?>" />
</div>
<?php
}
public static function actionCart() {
global $woocommerce;
if ($woocommerce->cart->total < static::$priceGreaterThanOrEqualTo) {
return;
}
if ($woocommerce->cart->total) {
$installments = static::getInstallments($woocommerce->cart->total);
} else {
$installments = 0;
}
if (stripos(static::$language['cartPageMessage'],'%d') !== false) {
if ($installments > 0) {
$message = sprintf(static::$language['cartPageMessage'], $installments);
} else {
$message = '';
}
} else {
$message = static::$language['cartPageMessage'];
}
?>
<tr><th colspan="2" class="francotecnologia_wc_parcpagseg_cart_tr_th"><?php echo $message; ?></th></tr>
<?php
}
public static function css() {
wp_enqueue_style('woocommerce-installments', plugins_url('woocommerce-installments.css', __FILE__), array(), '1.0', 'all');
}
public static function js() {
wp_enqueue_script('woocommerce-installments', plugins_url('woocommerce-installments.js', __FILE__), array('jquery', 'wc-add-to-cart-variation'), '1.0', true);
}
}
FrancoTecnologiaWooCommerceInstallments::init();