PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.3.21
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.3.21
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 1.3.45 1.3.44 All 176 releases
disco / app / Features / UserLimit.php

UserLimit.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.3.21, at app/Features/UserLimit.php

134 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UserLimit Feature
4 *
5 * @package Disco
6 * @subpackage App\Utility
7 * @since 1.0.0
8 * @category Utility
9 */
10
11 namespace Disco\App\Features;
12
13 /**
14 * Class DiscountLimit
15 *
16 * @package Disco
17 * @subpackage App\Feature
18 * @author Ohidul Islam <wahid0003@gmail.com>
19 * @link https://webappick.com
20 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
21 * @category Utility
22 */
23 class UserLimit {
24
25 /**
26 * Disco start session function.
27 * Set campaign in WC session as an array.
28 *
29 * @param int $campaign_id Campaign ID.
30 * @return void
31 */
32 public function disco_start_session_on_checkout( $campaign_id ) {
33 if ( ! is_checkout() ) {
34 return;
35 }
36
37 // Retrieve the current associative array from the session
38 $disco_campaign = WC()->session->get( 'disco_campaign', array() );
39
40 if ( !is_array( $disco_campaign ) ) {
41 $disco_campaign = (array) $disco_campaign;
42 }
43
44 // Check if the key (campaign ID) already exists
45 if ( in_array( $campaign_id, $disco_campaign, true ) ) {
46 return;
47 }
48
49 // Add the new data using the campaign ID as the key
50 $disco_campaign[] = $campaign_id;
51
52 // Save the updated associative array back to the session
53 WC()->session->set( 'disco_campaign', $disco_campaign );
54 }
55
56 /**
57 * Retrieve total applied campaign by campaign id.
58 *
59 * @param int $campaign_id Campaign ID.
60 * @return int
61 */
62 public function disco_get_total_applied_campaign( $campaign_id ) { // phpcs:disable
63 global $wpdb;
64
65 // Filter the order statuses
66 $status = apply_filters(
67 'disco_campaign_user_limit_order_statuses',
68 array(
69 'wc-processing',
70 'wc-on-hold',
71 'wc-completed',
72 ),
73 $campaign_id
74 );
75
76 $placeholders = implode( ',', array_fill( 0, count( $status ), '%s' ) );
77
78 // Check HPOS first
79 if ( $this->disco_is_hpos_enabled() ) {
80 $sql = "
81 SELECT COUNT(DISTINCT o.id)
82 FROM {$wpdb->prefix}wc_orders AS o
83 INNER JOIN {$wpdb->prefix}wc_orders_meta AS om ON o.id = om.order_id
84 WHERE o.status IN ($placeholders)
85 AND om.meta_key = %s
86 AND om.meta_value = %s
87 ";
88
89 $count = $wpdb->get_var(
90 $wpdb->prepare(
91 $sql,
92 ...array_merge( $status, array( 'disco_campaign', (string) $campaign_id ) )
93 )
94 );
95
96 return (int) $count;
97 }
98
99 // Legacy query for posts table (non-HPOS only)
100 $sql = "
101 SELECT COUNT(p.ID)
102 FROM {$wpdb->posts} AS p
103 INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
104 WHERE p.post_type = 'shop_order'
105 AND p.post_status IN ($placeholders)
106 AND pm.meta_key = %s
107 AND pm.meta_value = %s
108 ";
109
110 $count = $wpdb->get_var(
111 $wpdb->prepare(
112 $sql,
113 ...array_merge( $status, array( 'disco_campaign', (string) $campaign_id ) )
114 )
115 );
116
117 return (int) $count;
118 }
119
120 /**
121 * Check WooCommerce HPOS is enabled or not.
122 *
123 * @return bool
124 */
125 private function disco_is_hpos_enabled() {
126 if ( class_exists( \Automattic\WooCommerce\Utilities\OrderUtil::class ) ) {
127 return \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
128 }
129
130 return false;
131 }
132
133 }
134