PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.3.46
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.3.46
1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 All 178 releases
disco / functions / order.php

order.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.3.46, at functions/order.php

95 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Disco
4 *
5 * @package Disco
6 * @author Ohidul Islam <wahid0003@gmail.com>
7 * @link http://domain.tld
8 * @license GPL 2.0+
9 * @copyright 2022 WebAppick
10 */
11
12 // Ensure the file is not accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! function_exists( 'disco_add_order_meta' ) ) {
18
19 /**
20 * Get a campaign id from WC session and set into post meta after place order.
21 * Unset WC session after update post-meta.
22 *
23 * @param int $order_id Order ID.
24 * @return void
25 */
26 function disco_add_order_meta( $order_id ) {
27 // Validate session exists
28 if ( ! WC()->session ) {
29 return;
30 }
31
32 $campaigns = WC()->session->get( 'disco_campaign' );
33
34 if ( empty( $campaigns ) || ! is_array( $campaigns ) ) {
35 return;
36 }
37
38 $order = wc_get_order( $order_id );
39
40 // Validate order exists
41 if ( ! $order instanceof WC_Order ) {
42 return;
43 }
44
45 /**
46 * Add each campaign ID as separate meta entry.
47 * Using unique=false allows multiple campaign IDs per order.
48 */
49 foreach ( $campaigns as $campaign_id ) {
50 $order->add_meta_data( 'disco_campaign', (int) $campaign_id, false );
51 }
52
53 // Save once after all meta is added (more efficient)
54 $order->save();
55
56 WC()->session->__unset( 'disco_campaign' );
57
58 // Clear price cache so user limits are re-evaluated
59 if ( !function_exists( 'disco_clear_price_cache' ) ) {
60 return;
61 }
62
63 disco_clear_price_cache();
64 }
65
66 add_action( 'woocommerce_thankyou', 'disco_add_order_meta', PHP_INT_MAX );
67 add_action( 'woocommerce_payment_complete', 'disco_add_order_meta', PHP_INT_MAX );
68 }
69
70 if ( ! function_exists( 'disco_reset_campaign_session' ) ) {
71
72 /**
73 * Clear the disco_campaign session before cart totals are recalculated.
74 *
75 * This prevents stale campaign IDs (from previously discounted products
76 * that were later removed from the cart) from being saved to order meta.
77 * The session is rebuilt fresh each time checkout prices are recalculated.
78 *
79 * @return void
80 */
81 function disco_reset_campaign_session() {
82 if ( ! is_checkout() ) {
83 return;
84 }
85
86 if ( ! WC()->session ) {
87 return;
88 }
89
90 WC()->session->__unset( 'disco_campaign' );
91 }
92
93 add_action( 'woocommerce_before_calculate_totals', 'disco_reset_campaign_session', 0 );
94 }
95