* @link https://webappick.com * @license https://opensource.org/licenses/gpl-license.php GNU Public License * @category Utility */ class UserLimit { /** * Disco start session function. * Set campaign in WC session as an array. * * @param int $campaign_id Campaign ID. * @return void */ public function disco_start_session_on_checkout( $campaign_id ) { if ( ! is_checkout() ) { return; } // Retrieve the current associative array from the session $disco_campaign = WC()->session->get( 'disco_campaign', array() ); if ( !is_array( $disco_campaign ) ) { $disco_campaign = (array) $disco_campaign; } // Check if the key (campaign ID) already exists if ( in_array( $campaign_id, $disco_campaign, true ) ) { return; } // Add the new data using the campaign ID as the key $disco_campaign[] = $campaign_id; // Save the updated associative array back to the session WC()->session->set( 'disco_campaign', $disco_campaign ); } /** * Retrieve total applied campaign by campaign id. * * @param int $campaign_id Campaign ID. * @return int */ public function disco_get_total_applied_campaign( $campaign_id ) { // Filter the order statuses to include in the query $status = apply_filters( 'disco_campaign_user_limit_order_statuses', array( 'wc-processing', 'wc-on-hold', 'wc-completed', ), $campaign_id ); // Q: how to get woocommerce HPOS status? // // Common query arguments $args = array( 'post_type' => 'shop_order', // WooCommerce orders are stored as 'shop_order' post-type 'post_status' => $status, // Get orders with any status 'fields' => 'ids', // Only retrieve post IDs 'posts_per_page' => -1, // Retrieve all matching orders ); if ( $this->disco_is_hpos_enabled() ) {// The campaign ID to match $args['meta_query'] = array( array( 'key' => 'disco_campaign', // Meta-key to filter by 'value' => $campaign_id, // The campaign ID to match 'compare' => '=', // Comparison operator ), ); } else { $args['meta_key'] = 'disco_campaign'; // a Meta-key to filter by phpcs:ignore $args['meta_value'] = $campaign_id; // The campaign ID to match } // Return the count of matching orders return ( new WP_Query( $args ) )->found_posts;// phpcs:disable WordPress.DB.SlowDBQuery.meta_query, WordPress.DB.SlowDBQuery.meta_key, WordPress.DB.SlowDBQuery.meta_value } /** * Check WooCommerce HPOS is enabled or not. * * @return bool */ private function disco_is_hpos_enabled() { if ( class_exists( \Automattic\WooCommerce\Utilities\OrderUtil::class ) ) { return \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled(); } return false; } }