PluginProbe
Pay with Vipps and MobilePay for WooCommerce / 6.2.0
Pay with Vipps and MobilePay for WooCommerce v6.2.0
6.2.1 6.2.0 6.1.10 6.1.9 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1.0 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 5.4.3 5.4.2 5.4.1 5.4.0 5.3.4 trunk All 183 releases
woo-vipps / payment / woo-vipps-compatibility.php

woo-vipps-compatibility.php in Pay with Vipps and MobilePay for WooCommerce 6.2.0, at payment/woo-vipps-compatibility.php

321 lines 15.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 This file is part of the plugin Pay with Vipps and MobilePay for WooCommerce
4 Copyright (c) 2019 WP-Hosting AS
5
6 MIT License
7
8 Copyright (c) 2019 WP-Hosting AS
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 */
28 if ( ! defined( 'ABSPATH' ) ) {
29 exit; // Exit if accessed directly
30 }
31
32 // This file collects actions, hooks and filters that are specific to third-party plugins that need extra support.
33
34 // Support Yith WooCommerce Name your price. We need to load the front-end filters when doing express checkout - otherwise price will be zero.
35 // Unfortunately, we can't do this before priority 10 for plugins loaded to support their 'premium' stuff. IOK 2021-09-29
36 add_action('after_setup_theme', function () {
37 if (function_exists('YITH_Name_Your_Price_Frontend')) {
38 if (is_admin() && defined('DOING_AJAX') && DOING_AJAX && isset($_REQUEST['action']) && $_REQUEST['action'] == 'do_express_checkout') {
39 YITH_Name_Your_Price_Frontend();
40 }
41 }
42
43 // Support Tutor LMS in express checkout/checkout. IOK 2026-02-18
44 if (class_exists('Tutor\WooCommerce')) {
45 // IOK 2026-02-18 Pretend express/checout orders are gifts for the purposes of tutor. This will stop the system from trying to enrollment when the order is still partial.
46 add_action('woo_vipps_before_create_express_checkout_order', function ($cart) {
47 add_filter('tutor_is_gift_item', function ($yesno, $item) {
48 return true;
49 }, 99,2 );
50 }, 10);
51 // After we have the customer, proceed with the tutor logic. IOK 2026-02-18
52 add_action('woo_vipps_set_order_shipping_details', function ($order, $shipping, $user) {
53 if (empty($order)) return;
54 $order_id = $order->get_id();
55 $enrolled = false;
56 // This logic is from the Admin Create Order branch of tutors' enrollment logic. IOK 2026-02-18
57 try {
58 foreach ( $order->get_items() as $item ) {
59 $product_id = $item->get_product_id();
60 $if_has_course = tutor_utils()->product_belongs_with_course( $product_id );
61 if ( $if_has_course ) {
62 $course_id = $if_has_course->post_id;
63 $customer_id = $order->get_customer_id();
64 tutor_utils()->do_enroll( $course_id, $order_id, $customer_id );
65 $enrolled = true;
66 }
67 }
68 if ($enrolled) {
69 $order->save();
70 }
71 } catch (Exception $e) {
72 Vipps::instance()->log("An error occured while trying to enroll user: " . $e->getMessage(), 'debug');
73 }
74
75 }, 10, 3);
76 }
77
78 // Snap Pixel for WooCommerce adds javascript to ajax functions if the do add-to-cart. We do that for single product express checkout,
79 // so better remove that action. IOK 2022-05-13
80 if (class_exists('snap_pixel_functions')) {
81 add_action('woocommerce_add_to_cart', function ($args) {
82 $doing_express_checkout = (did_action('wp_ajax_nopriv_do_single_product_express_checkout') || did_action('wp_ajax_do_single_product_express_checkout'));
83 if (!$doing_express_checkout) return;
84 global $wp_filter;
85 $carthooks = @$wp_filter['woocommerce_add_to_cart'];
86 if ($carthooks && $carthooks[10]) {
87 foreach($carthooks[10] as $callback) {
88 $f = $callback['function'];
89 if (is_array($f) && $f[1] == 'snap_pixel_code_add_to_cart') {
90 remove_action('woocommerce_add_to_cart', array($f[0], $f[1]), 10);
91 }
92 }
93 }
94 },1, 2);
95
96 }
97
98 // Default setup of NGINX Helper purges on comment insertion, which includes order notes.
99 // We'll remove this action in the very last filter before wp_insert_comment is called,
100 // when still adding order notes. IOK 2026-08-25
101 if (class_exists('FastCGI_Purger')) {
102 add_filter('woocommerce_new_order_note_data', function ($data) {
103 global $wp_filter;
104 if (empty($wp_filter['wp_insert_comment'])) return $data;
105 $callbacks = $wp_filter['wp_insert_comment']->callbacks[200] ?? [];
106 $f = null;
107 foreach($callbacks as $callback) {
108 $fun = $callback['function'];
109 if (is_array($fun) && isset( $fun[0], $fun[1]) && $fun[1] == 'purge_post_on_comment') {
110 $f = $fun; break;
111 }
112 }
113 if ($f) {
114 remove_action('wp_insert_comment', $f, 200);
115 }
116 return $data;
117 });
118 }
119
120 }, 20);
121
122 // IOK 2020-03-17: Klarna Checkout now supports external payment methods, such as Vipps. This is great, but we need first to check
123 // that any user hasn't already installed the free plugin for this created by Krokedil. If they have, this filter will be present:
124
125 add_action('after_setup_theme', function () {
126 if (class_exists('KCO') && defined('KCO_WC_VERSION') && version_compare(KCO_WC_VERSION, '2.0.0', '>=') && Vipps::instance()->gateway()->enabled == 'yes') {
127 if (has_filter('kco_wc_api_request_args', 'kcoepm_create_order_vipps')) {
128 // Vipps external payment support is already present - do nothing. IOK 2021-09-29
129 } else {
130 require_once(dirname(__FILE__) . "/VippsKCSupport.class.php");
131 VippsKCSupport::init();
132 }
133 }
134
135
136 // IOK 2022-06-28 This plugin erroneously tries to create an order on the order-received page when reached via the
137 // Vipps Express Checkout route. Reported, but avoiding the issue by disabling it on this page.
138 if (class_exists('DIBS_Easy')) {
139 add_action('template_redirect', function () {
140 if (is_order_received_page()) {
141 global $wp;
142 if (!isset($wp->query_vars['order-received'])) return;
143 $order_id = absint($wp->query_vars['order-received']);
144 $order = wc_get_order($order_id);
145 if (!$order || is_wp_error($order)) {
146 return;
147 }
148 if (! Vipps::is_vipps_order($order)) {
149 return;
150 }
151 add_filter('option_woocommerce_dibs_easy_settings', function ($value, $option) {
152 if (!empty($value)) {
153 $value['enabled'] = 'no';
154 $value['description'] = '#yolo';
155 $value['test_mode'] = 'no';
156 return $value;
157 } else {
158 }
159 return $value;
160 }, 10, 2);
161 }
162 });
163 }
164
165 // IOK 2022-11-24 support adwords-tracking with Monster Insights
166 if (class_exists('MonsterInsights_eCommerce_WooCommerce_Integration')) {
167 add_action('woo_vipps_express_checkout_order_created', function ($orderid) {
168 $mi = MonsterInsights_eCommerce_WooCommerce_Integration::get_instance();
169 $vipps = Vipps::instance();
170 if (method_exists($mi, 'save_user_cid')) {
171 $vipps->log("Saving monster insights tracking info to order $orderid", 'debug');
172 $mi->save_user_cid($orderid);
173 } else {
174 $vipps->log("Tried adding monster insights session to order $orderid, but save_user_cid does not exist in object", 'debug');
175 }
176 });
177 }
178
179
180 // For WooCommerce Smart Coupons, which at 6.9.0 thought that doing_ajax and WP_ADMIN => this was an admin call
181 // IOK 2022-12-19 reported, but many older versions are likely to exist
182 if (class_exists('WC_SC_Purchase_Credit')) {
183 function woo_vipps_pretend_not_doing_ajax ($doingit) {
184 if ($doingit && isset($_REQUEST['action']) &&
185 ($_REQUEST['action'] == 'do_single_product_express_checkout' ||
186 $_REQUEST['action'] == 'do_express_checkout')) {
187 return false;
188 }
189 return $doingit;
190 }
191 function woo_vipps_add_no_ajax () {
192 add_filter('wp_doing_ajax', 'woo_vipps_pretend_not_doing_ajax');
193 }
194 function woo_vipps_remove_no_ajax() {
195 remove_filter('wp_doing_ajax', 'woo_vipps_pretend_not_doing_ajax');
196 }
197 add_action('woo_vipps_before_calculate_totals_partial_order', function ($order) {
198 add_action('woocommerce_new_order_item', 'woo_vipps_add_no_ajax' , 9);
199 add_action('woocommerce_new_order_item', 'woo_vipps_remove_no_ajax' , 11);
200 });
201 }
202
203 // Support Woo-Mailerlite in Checkout
204 if (class_exists('MailerLite\Includes\Classes\Settings\MailerLiteSettings')) {
205 // If Mailerlite is active as well as Checkout, then add support for it
206 if (get_option('ml_account_authenticated') && (Vipps::instance()->gateway()->get_option('vipps_checkout_enabled') == 'yes')) {
207
208 // If checkout is active, don't support saving "lost carts"
209 add_filter('option_mailerlite_disable_checkout_sync', function ($value, $option) {
210 return 1;
211 }, 10, 2);
212
213 // Check if the checkout feature is enabled for MailerLite, and if so, add the feature for Checkout
214 $checkout = MailerLite\Includes\Classes\Settings\MailerLiteSettings::getInstance()->getMlOption('checkout', 'no');
215 if ($checkout == 'yes') {
216 add_filter('woo_vipps_checkout_consent_query', function ($text) {
217 return MailerLite\Includes\Classes\Settings\MailerLiteSettings::getInstance()->getMlOption('checkout_label');
218 });
219 add_action('woo_vipps_set_order_shipping_details', function ($order) {
220 if (class_exists('MailerLite\Includes\Classes\Process\OrderProcess')) {
221 $consent = intval($order->get_meta('_vipps_custom_consent_provided'));
222 if ($consent) {
223 MailerLite\Includes\Classes\Process\OrderProcess::getInstance()->setOrderCustomerSubscribe($order->get_id());;
224 }
225 }
226 });
227 // Finalize the job only right before the thankyou page
228 add_action('woo_vipps_before_thankyou', function ($orderid, $order) {
229 if (class_exists('MailerLite\Includes\Classes\Process\OrderProcess')) {
230 $consent = intval($order->get_meta('_vipps_custom_consent_provided'));
231 if ($consent) {
232 MailerLite\Includes\Classes\Process\OrderProcess::getInstance()->processOrderSubscription($orderid);
233 }
234 }
235 }, 10, 2);
236 }
237 }
238 }
239
240 // Support Mailchimp for WooCommerce in Checkout
241 if (function_exists('mailchimp_is_configured') && class_exists('MailChimp_Service')) {
242 // If mailchimp is configured and Checkout is on, add actions
243 if (mailchimp_is_configured() && (Vipps::instance()->gateway()->get_option('vipps_checkout_enabled') == 'yes')) {
244
245 add_filter('woo_vipps_checkout_consent_query', function ($text) {
246 $opts = get_option('mailchimp-woocommerce');
247 $text = ($opts['newsletter_label'] ?? false) ? $opts['newsletter_label'] : $text;
248 return $text;
249 });
250 // This should annotate the order correctly, straight before the order changes status to 'processing'.
251 add_action('woo_vipps_set_order_shipping_details', function ($order) {
252 if (class_exists('MailChimp_Service')) {
253 $is_checkout = $order->get_meta('_vipps_checkout');
254 $consent = intval($order->get_meta('_vipps_custom_consent_provided'));
255 if ($consent && $is_checkout) {
256 $order->update_meta_data('mailchimp_woocommerce_is_subscribed', true);
257 $order->save();
258 }
259 }
260 });
261 }
262
263 }
264
265 // Disable Vipps checkout/express in cart - for carts with products from the plugin: all-products-for-woocommerce-subscriptions. LP 2025-06-13
266 if (class_exists('WCS_ATT_Cart')) {
267 add_filter('woo_vipps_cart_supports_checkout', function ($supports, $cart) {
268 foreach ($cart->get_cart_contents() as $values) {
269 // below returns instance if product is subscription -> we dont support it, else false (not subscription). LP 2025-06-13
270 $is_wcs_att_sub = WCS_ATT_Cart::get_subscription_scheme($values);
271 if ($is_wcs_att_sub) return false;
272 }
273 return $supports;
274 }, 10, 2);
275
276 add_filter('woo_vipps_cart_supports_express_checkout', function ($supports, $cart) {
277 foreach ($cart->get_cart_contents() as $values) {
278 // below returns instance if product is subscription -> we dont support it, else false (not subscription). LP 2025-06-13
279 $is_wcs_att_sub = WCS_ATT_Cart::get_subscription_scheme($values);
280 if ($is_wcs_att_sub) return false;
281 }
282 return $supports;
283 }, 10, 2);
284 }
285 });
286
287
288
289 // Anti-support for WooCommerce subscriptions; but allow turning it off using an (advanced) setting or filter. IOK 2021-10-26
290 // Turn off on "pay for order" too
291 add_filter('woo_vipps_is_available', function ($ok, $gateway) {
292 if (!$ok) return $ok;
293 // Can't do these, so remove Vipps as payment method IOK 2021-05-14
294 if (class_exists( 'WC_Subscriptions_Cart')) {
295 $paying_for_order = absint( get_query_var( 'order-pay' ) );
296 if (WC_Subscriptions_Cart::cart_contains_subscription()) {
297 $ok = false;
298 }
299 if (function_exists('wcs_cart_contains_renewal') && wcs_cart_contains_renewal()) {
300 $ok = false;
301 }
302 if ($paying_for_order && class_exists('WC_Subscriptions_Product')) {
303 $order = wc_get_order($paying_for_order);
304 if (is_a($order,'WC_Order')) {
305 foreach ( $order->get_items() as $item_id => $item ) {
306 if (is_a($item, 'WC_Order_Item_Product')) {
307 $prod = $item->get_product();
308 if (is_a($prod, 'WC_Product')) {
309 if (WC_Subscriptions_Product::is_subscription($prod)) {
310 $ok = false; break;
311 }
312 }
313 }
314 }
315 }
316 }
317 $ok = apply_filters('woo_vipps_support_subscription_cart', $ok);
318 }
319 return $ok;
320 }, 10, 2);
321