PluginProbe
MyRewards / trunk
MyRewards vtrunk
5.7.8 5.7.7 5.7.6 trunk 1.2.2 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.1 2.2.0 2.3.0 2.4.0 2.4.1 2.5.0 2.6.4 2.6.6 3.0.0.0 3.1.0 3.1.2 3.1.2.1 3.10.4 3.10.9 All 165 releases
woorewards / include / ui / blocks / extension.php

extension.php in MyRewards trunk, at include/ui/blocks/extension.php

265 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace LWS\WOOREWARDS\Ui\Blocks;
3 if (!defined('ABSPATH')) exit();
4
5 /** Exchange data with client-side cart.
6 * @see https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce-blocks/docs/third-party-developers/extensibility/rest-api/extend-rest-api-update-cart.md
7 * @see https://github.com/woocommerce/woocommerce/tree/trunk/plugins/woocommerce-blocks/packages/checkout/utils
8 */
9 class Extension {
10 const API = 'lws_woorewards';
11
12 public static function install()
13 {
14 $me = new self();
15 \woocommerce_store_api_register_endpoint_data([
16 'endpoint' => \Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema::IDENTIFIER,
17 'namespace' => self::API,
18 'data_callback' => [$me, 'apiData'],
19 'schema_callback' => [$me, 'apiSchema'],
20 'schema_type' => ARRAY_A,
21 ]);
22 \woocommerce_store_api_register_endpoint_data([
23 'endpoint' => \Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema::IDENTIFIER,
24 'namespace' => self::API,
25 'data_callback' => [$me, 'apiData'],
26 'schema_callback' => [$me, 'apiSchema'],
27 'schema_type' => ARRAY_A,
28 ]);
29 \woocommerce_store_api_register_update_callback([
30 'namespace' => self::API,
31 'callback' => [$me, 'apiUpdate'],
32 ]);
33 return $me;
34 }
35
36 /** @see https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce-blocks/docs/third-party-developers/extensibility/rest-api/available-endpoints-to-extend.md */
37 public function apiData()
38 {
39 $systems = [];
40 foreach ($this->getSystems() as $slug => $pool) {
41 /** @var \LWS\WOOREWARDS\PRO\Core\Pool $pool */
42 $info = $this->getInfo($pool);
43 if (!$info) continue;
44 $info['used_formated'] = $pool->formatPoints($info['used']);
45 $info['max_formated'] = $pool->formatPoints($info['max']);
46 $info['amount_formated'] = $pool->formatPoints($info['amount']);
47 $systems[$slug] = $info;
48 }
49 $data = ['systems' => $systems];
50 return \apply_filters('lws_woorewards_bloc_cart_data', $data, $this);
51 }
52
53 static function getSystems()
54 {
55 static $systems = null;
56 if (null === $systems) {
57 $systems = [];
58 $pools = \apply_filters('lws_woorewards_get_pools_by_args', false, ['showall' => true], \get_current_user_id());
59 if ($pools) {
60 $pools = $pools->filter([__CLASS__, 'filterPools']);
61 $systems = \array_combine($pools->map(function($pool){
62 return $pool->getName();
63 }), $pools->asArray());
64 }
65 }
66 return $systems;
67 }
68
69 function getInfo($pool)
70 {
71 $userId = \get_current_user_id();
72 if( !$userId )
73 return false;
74 if( !\WC()->cart )
75 return false;
76 if( !$pool )
77 return false;
78
79 $stackId = $pool->getStackId();
80 $max = $this->getMaxPoints($pool, \WC()->cart, $userId);
81 $min = \intval($pool->getOption('direct_reward_min_points_on_cart'));
82 $used = \intval(\get_user_meta($userId, 'lws_wr_points_on_cart_'.$stackId, true));
83
84 if ($min > 0 && $min > $max) {
85 \update_user_meta($userId, 'lws_wr_points_on_cart_'.$stackId, 0);
86 $used = 0;
87 } elseif ($used > $max) {
88 \update_user_meta($userId, 'lws_wr_points_on_cart_'.$stackId, $max);
89 $used = $max;
90 } elseif ($used > 0 && $min > 0 && $min > $used) {
91 \update_user_meta($userId, 'lws_wr_points_on_cart_'.$stackId, $min);
92 $used = $min;
93 }
94
95 return array(
96 'amount' => $pool->getPoints($userId),
97 'min' => $min,
98 'max' => $max,
99 'used' => $used,
100 'stack' => $stackId,
101 'name' => $pool->getName(),
102 );
103 }
104
105 static function filterPools($pool)
106 {
107 if( !$pool->getOption('direct_reward_mode') )
108 return false;
109 if( !$pool->userCan() )
110 return false;
111 if (!$pool->isBuyable())
112 return false;
113 return true;
114 }
115
116 protected function getMaxPoints(&$pool, &$cart, $userId)
117 {
118 $rate = $pool->getOption('direct_reward_point_rate');
119 if( $rate == 0.0 )
120 return 0;
121 $points = $pool->getPoints($userId);
122
123 $total = $cart->get_subtotal();
124 if( 'yes' === get_option('woocommerce_prices_include_tax') )
125 $total += $cart->get_subtotal_tax();
126
127 foreach($cart->get_applied_coupons() as $otherCode)
128 {
129 if (strpos($otherCode, 'wr_points_on_cart') === false) {
130 $total -= $cart->get_coupon_discount_amount($otherCode);
131 }
132 }
133
134 $currencyRate = \LWS\Adminpanel\Tools\Conveniences::getCurrencyPrice(1, false, false);
135 if (0 != $currencyRate)
136 $total = $total / $currencyRate;
137
138 $max = (int)\ceil($total / $rate);
139 $points = \min($max, $points);
140 $points = \apply_filters('lws_woorewards_pointdiscount_max_points', $points, $rate, $pool, $userId, $cart);
141 return $points;
142 }
143
144 /** @see https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce-blocks/docs/third-party-developers/extensibility/rest-api/available-endpoints-to-extend.md */
145 public function apiSchema()
146 {
147 $schema = [
148 'systems' => [
149 'description' => __('List of Loyalty systems.', 'woorewards-lite'),
150 'context' => ['view', 'edit'],
151 'readonly' => false,
152 'type' => 'array',
153 'items' => \array_fill_keys(\array_keys($this->getSystems()), [
154 'description' => __('System description.', 'woorewards-lite'),
155 'context' => ['view', 'edit'],
156 'readonly' => false,
157 'type' => 'array',
158 'items' => [
159 'used' => [
160 'description' => __('Points used in Cart.', 'woorewards-lite'),
161 'type' => ['integer', 'null'],
162 'context' => ['view', 'edit'],
163 'readonly' => false,
164 ],
165 'used_formated' => [
166 'description' => __('Points used in Cart for display.', 'woorewards-lite'),
167 'type' => ['string', 'null'],
168 'context' => ['view', 'edit'],
169 'readonly' => false,
170 ],
171 'amount' => [
172 'description' => __('Available Points.', 'woorewards-lite'),
173 'type' => ['integer', 'null'],
174 'context' => ['view', 'edit'],
175 'readonly' => false,
176 ],
177 'amount_formated' => [
178 'description' => __('Available Points for display.', 'woorewards-lite'),
179 'type' => ['string', 'null'],
180 'context' => ['view', 'edit'],
181 'readonly' => false,
182 ],
183 'min' => [
184 'description' => __('Minimum Points to use in Cart.', 'woorewards-lite'),
185 'type' => ['integer', 'null'],
186 'context' => ['view', 'edit'],
187 'readonly' => false,
188 ],
189 'max' => [
190 'description' => __('Maximum Points to use in Cart.', 'woorewards-lite'),
191 'type' => ['integer', 'null'],
192 'context' => ['view', 'edit'],
193 'readonly' => false,
194 ],
195 'max_formated' => [
196 'description' => __('Maximum Points to use in Cart for display.', 'woorewards-lite'),
197 'type' => ['string', 'null'],
198 'context' => ['view', 'edit'],
199 'readonly' => false,
200 ],
201 'stack' => [
202 'description' => __('Points reserve.', 'woorewards-lite'),
203 'type' => ['string', 'null'],
204 'context' => ['view', 'edit'],
205 'readonly' => false,
206 ],
207 'name' => [
208 'description' => __('Loyalty system.', 'woorewards-lite'),
209 'type' => ['string', 'null'],
210 'context' => ['view', 'edit'],
211 'readonly' => false,
212 ],
213 ]
214 ]),
215 ]
216 ];
217 return \apply_filters('lws_woorewards_bloc_cart_schema', $schema);
218 }
219
220 /** @see https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce-blocks/docs/third-party-developers/extensibility/rest-api/extend-rest-api-update-cart.md */
221 public function apiUpdate($data)
222 {
223 if (\apply_filters('lws_woorewards_bloc_cart_update_is', true, $data)) {
224 if ('use_points' === $data['action']) {
225 $userId = \get_current_user_id();
226 if ($userId) {
227 $points = \sanitize_text_field($data['value']);
228 $system = \apply_filters('lws_woorewards_get_pools_by_args', false, array(
229 'system' => \sanitize_key($data['system'])
230 ), $userId)->last();
231 if ($system) {
232 $points = (int)$system->reversePointsFormat($points);
233 $this->applyPointsOnCart($system, $points, $userId);
234 }
235 }
236 }
237 }
238 \do_action('lws_woorewards_bloc_cart_updated', $data);
239 }
240
241 protected function applyPointsOnCart($system, $points, $userId)
242 {
243 $stackId = $system->getStackId();
244 if (\WC()->cart) {
245 $points = \min($points, $this->getMaxPoints($system, \WC()->cart, $userId));
246 } else {
247 $max = $system->getPoints($userId);
248 $points = \max(0, \min($points, $max));
249 }
250 \update_user_meta($userId, 'lws_wr_points_on_cart_' . $stackId, $points);
251
252 if (\WC()->cart) {
253 $code = 'wr_points_on_cart-' . $system->getName();
254 if ($points) {
255 // add coupon if not exists
256 if( !\WC()->cart->has_discount($code) )
257 \WC()->cart->apply_coupon($code);
258 } else {
259 // silently remove coupon if exists
260 if( \WC()->cart->has_discount($code) )
261 \WC()->cart->remove_coupon($code);
262 }
263 }
264 }
265 }