| 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 |
use WP_Query; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class DiscountLimit |
| 17 |
* |
| 18 |
* @package Disco |
| 19 |
* @subpackage App\Feature |
| 20 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 21 |
* @link https://webappick.com |
| 22 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 23 |
* @category Utility |
| 24 |
*/ |
| 25 |
class UserLimit { |
| 26 |
|
| 27 |
/** |
| 28 |
* Disco start session function. |
| 29 |
* Set campaign in WC session as an array. |
| 30 |
* |
| 31 |
* @param int $campaign_id Campaign ID. |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function disco_start_session_on_checkout( $campaign_id ) { |
| 35 |
if ( ! is_checkout() ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
// Retrieve the current associative array from the session |
| 40 |
$disco_campaign = WC()->session->get( 'disco_campaign', array() ); |
| 41 |
|
| 42 |
if ( !is_array( $disco_campaign ) ) { |
| 43 |
$disco_campaign = (array) $disco_campaign; |
| 44 |
} |
| 45 |
|
| 46 |
// Check if the key (campaign ID) already exists |
| 47 |
if ( in_array( $campaign_id, $disco_campaign, true ) ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
// Add the new data using the campaign ID as the key |
| 52 |
$disco_campaign[] = $campaign_id; |
| 53 |
|
| 54 |
// Save the updated associative array back to the session |
| 55 |
WC()->session->set( 'disco_campaign', $disco_campaign ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Retrieve total applied campaign by campaign id. |
| 60 |
* |
| 61 |
* @param int $campaign_id Campaign ID. |
| 62 |
* @return int |
| 63 |
*/ |
| 64 |
public function disco_get_total_applied_campaign( $campaign_id ) { |
| 65 |
// Filter the order statuses to include in the query |
| 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 |
// Q: how to get woocommerce HPOS status? |
| 76 |
// |
| 77 |
// Common query arguments |
| 78 |
$args = array( |
| 79 |
'post_type' => 'shop_order', // WooCommerce orders are stored as 'shop_order' post-type |
| 80 |
'post_status' => $status, // Get orders with any status |
| 81 |
'fields' => 'ids', // Only retrieve post IDs |
| 82 |
'posts_per_page' => -1, // Retrieve all matching orders |
| 83 |
); |
| 84 |
|
| 85 |
if ( $this->disco_is_hpos_enabled() ) {// The campaign ID to match |
| 86 |
$args['meta_query'] = array( |
| 87 |
array( |
| 88 |
'key' => 'disco_campaign', // Meta-key to filter by |
| 89 |
'value' => $campaign_id, // The campaign ID to match |
| 90 |
'compare' => '=', // Comparison operator |
| 91 |
), |
| 92 |
); |
| 93 |
} else { |
| 94 |
$args['meta_key'] = 'disco_campaign'; // a Meta-key to filter by phpcs:ignore |
| 95 |
$args['meta_value'] = $campaign_id; // The campaign ID to match |
| 96 |
} |
| 97 |
|
| 98 |
// Return the count of matching orders |
| 99 |
|
| 100 |
return ( new WP_Query( $args ) )->found_posts;// phpcs:disable WordPress.DB.SlowDBQuery.meta_query, WordPress.DB.SlowDBQuery.meta_key, WordPress.DB.SlowDBQuery.meta_value |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Check WooCommerce HPOS is enabled or not. |
| 105 |
* |
| 106 |
* @return bool |
| 107 |
*/ |
| 108 |
private function disco_is_hpos_enabled() { |
| 109 |
if ( class_exists( \Automattic\WooCommerce\Utilities\OrderUtil::class ) ) { |
| 110 |
return \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled(); |
| 111 |
} |
| 112 |
|
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|