PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / classes / model / ShippingPriceFormula.php

ShippingPriceFormula.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/classes/model/ShippingPriceFormula.php

168 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Description of ShippingPriceFormula
5 *
6 * @author Ali2Woo Team
7 */
8
9 namespace AliNext_Lite;;
10
11 class ShippingPriceFormula {
12
13 public $id = 0;
14 public $sign = '*';
15 public $value = '';
16
17 public function __construct($data = 0) {
18 if (is_int($data) && $data) {
19 $this->id = $data;
20 $this->load($this->id);
21 } else if (is_array($data)) {
22 foreach ($data as $field => $value) {
23 if (property_exists(get_class($this), $field)) {
24 $this->$field = esc_attr($value);
25 }
26 }
27 }
28 }
29
30 public function load($id = false) {
31 $load_id = $id ? $id : ($this->id ? $this->id : 0);
32 if ($load_id) {
33 $formula_list = ShippingPriceFormula::load_formulas_list(false);
34 foreach ($formula_list as $formula) {
35 if (intval($formula['id']) === intval($load_id)) {
36 foreach ($formula as $field => $value) {
37 if (property_exists(get_class($this), $field)) {
38 $this->$field = esc_attr($value);
39 }
40 }
41 break;
42 }
43 }
44 }
45 return $this;
46 }
47
48 public function save() {
49 $formula_list = ShippingPriceFormula::load_formulas_list(false);
50
51 if (!intval($this->id)) {
52 $this->id = 1;
53 foreach ($formula_list as $key => $formula) {
54 if (intval($formula['id']) >= $this->id) {
55 $this->id = intval($formula['id']) + 1;
56 }
57 }
58 $formula_list[] = get_object_vars($this);
59 } else {
60 $boolean = false;
61 foreach ($formula_list as $key => $formula) {
62 if (intval($formula['id']) === intval($this->id)) {
63 $formula_list[$key] = get_object_vars($this);
64 $boolean = true;
65 }
66 }
67 if (!$boolean) {
68 $formula_list[] = get_object_vars($this);
69 }
70 }
71
72 set_setting('shipping_formula_list', array_values($formula_list));
73 return $this;
74 }
75
76 public function delete() {
77 $formula_list = ShippingPriceFormula::load_formulas_list(false);
78 foreach ($formula_list as $key => $formula) {
79 if (intval($formula['id']) === intval($this->id)) {
80 unset($formula_list[$key]);
81 set_setting('shipping_formula_list', array_values($formula_list));
82 }
83 }
84 }
85
86 public static function deleteAll() {
87 del_setting('shipping_formula_list');
88 }
89
90
91 public static function load_formulas() {
92 return ShippingPriceFormula::load_formulas_list(true);
93 }
94
95 private static function load_formulas_list($asObject = true) {
96 $result = array();
97 $formula_list = get_setting('shipping_formula_list');
98 $formula_list = $formula_list && is_array($formula_list) ? $formula_list : array();
99 if ($asObject) {
100 foreach ($formula_list as $formula) {
101 $fo = new ShippingPriceFormula();
102 foreach ($formula as $name => $value) {
103 if (property_exists(get_class($fo), $name)) {
104 $fo->$name = $value;
105 }
106 }
107 $result[] = $fo;
108 }
109 } else {
110 $result = $formula_list;
111 }
112
113 return $result;
114 }
115
116 public static function get_default_formula() {
117 $formula = get_setting('shipping_default_formula');
118 return new ShippingPriceFormula($formula && is_array($formula) && $formula['value'] !== "" ? $formula : array('value' => 1, 'sign' => '*'));
119 }
120
121 public static function set_default_formula($formula) {
122
123 set_setting('shipping_default_formula', get_object_vars($formula));
124 }
125
126 public static function normalize_shipping_price($shipping) {
127 $price = 0;
128
129 if (isset($shipping['previewFreightAmount']) && isset($shipping['previewFreightAmount']['value']))
130 $price = floatval($shipping['previewFreightAmount']['value']);
131
132 else if (isset($shipping['freightAmount']) && isset($shipping['freightAmount']['value']))
133 $price = floatval($shipping['freightAmount']['value']);
134
135 return $price;
136
137 }
138
139 public static function apply_formula($shipping, $local_options, $round = 2){
140
141 $shipping_price = self::normalize_shipping_price($shipping);
142
143 if ($local_options['use_price_rule']) {
144 $formula = self::get_default_formula();
145 if ($formula && $formula->value){
146 if ($formula->sign == "=") {
147 $shipping_price = floatval($formula->value);
148 } else if ($formula->sign == "*") {
149 $shipping_price = floatval($shipping_price) * floatval($formula->value);
150 } else if ($formula->sign == "+") {
151 $shipping_price = floatval($shipping_price) + floatval($formula->value);
152 }
153 }
154 }
155 return round($shipping_price, $round);
156 }
157
158 public static function allow_price_rule(){
159 return get_setting(Settings::SETTING_ALLOW_SHIPPING_FRONTEND);
160 }
161
162 public static function allow_post_price_rule($post_id){
163 $use_price_rule = get_post_meta($post_id, 'a2wl_use_price_rule', true);
164 return $use_price_rule==="" || $use_price_rule === "1" ? true : false;
165 }
166
167 }
168