• Resolved mamagee

    (@mamagee)


    I created a plugin to do schedule a task for me, when the plugin is loaded and activated it successful but i try test the plugin if it working i get internal error at this stage

    Your order Product Subtotal BASIC – $10 × 1 $10.00 Subtotal $10.00 Total $10.00 Wallet Payment [Your Amount : $49,500.00] Bcon Global: BTC Your personal data will be used to process your order, support your experience throughout this website, and for other purposes described in our privacy policy. Place order

    This the description of the plugin
    code implements a plugin called “Custom Order Restriction” for WooCommerce. Here’s an overview of its functionality:

    1. Description: The plugin regulates how often and how much users can order in WooCommerce. It ensures fair and controlled order placement by resetting certain limits daily, tracking user orders, and restricting users from placing orders too frequently or spending too much in a short time.
    2. Key Features:
      • Reset certain limits daily.
      • Track user orders.
      • Restrict users from placing orders too frequently.
      • Restrict users from spending too much in a short time.
      • Prevent users from checking out until the restrictions are met.
    3. Activation/Deactivation Hooks:
      • The plugin registers activation and deactivation hooks to perform actions when the plugin is activated or deactivated. These hooks schedule daily events to reset order restrictions and clear scheduled events on deactivation.
    4. Daily Event Scheduling:
      • The plugin schedules a daily event to reset order restrictions using the wp_schedule_event function. This event is triggered once every day at midnight.
    5. Order Restriction Logic:
      • The plugin implements logic to reset order restrictions and amounts. It uses WP_User_Query to retrieve user IDs and resets order-related data for each user.
    6. Last Order Information:
      • The plugin adds a custom field to user meta to store the last order timestamp and amount. This information is used to determine if the user can place a new order based on predefined restrictions.
    7. Checkout Availability Filtering:
      • The plugin filters the checkout availability based on the order restrictions. It checks if the user can place a new order and adds an error notice if the restrictions are not met.

    Overall, code provides a comprehensive solution for managing order restrictions in WooCommerce, ensuring a fair and controlled ordering process for users.

    This my plugin code

    <?php
    /*
    Plugin Name: Custom Order Restriction
    Description: This plugin implements order restriction functionality for WooCommerce. It regulates how often and how much users can order, ensuring fair and controlled order placement. It resets certain limits daily, tracks user orders, and restricts users from placing orders too frequently or spending too much in a short time. If users exceed these limits, they'll be unable to checkout until the restrictions are met.
    Version: 1.1
    Author: THE-HOOD
    */
    
    // Define plugin constants
    define('CUSTOM_ORDER_RESTRICTION_PLUGIN_NAME', 'Custom Order Restriction');
    define('CUSTOM_ORDER_RESTRICTION_PLUGIN_VERSION', '1.0.0');
    define('CUSTOM_ORDER_RESTRICTION_PLUGIN_OPTIONS', 'custom_order_restriction_options');
    
    // Create a class for the plugin logic
    class Custom_Order_Restriction {
    
        // Constructor
        public function __construct() {
            // Register activation and deactivation hooks
            register_activation_hook(__FILE__, array($this, 'activate'));
            register_deactivation_hook(__FILE__, array($this, 'deactivate'));
    
            // Schedule a daily event to reset order restrictions
            add_action('wp', array($this, 'schedule_daily_event'));
    
            // Hook into the scheduled event to reset order restrictions and amounts
            add_action('reset_order_restrictions_event', array($this, 'reset_order_restrictions'));
    
            // Add a custom field to user meta to store the last order timestamp and amount
            add_action('woocommerce_new_order', array($this, 'set_last_order_info'));
    
            // Filter the checkout availability based on the order restrictions
            add_filter('woocommerce_can_checkout', array($this, 'filter_checkout_availability'));
        }
    
        // Activation hook
        public function activate() {
            // Log activation message
            error_log(CUSTOM_ORDER_RESTRICTION_PLUGIN_NAME . ' plugin activated');
    
            // Schedule daily event on activation
            $this->schedule_daily_event();
    
            // Log activation completed
            error_log('Activation completed');
        }
    
        // Deactivation hook
        public function deactivate() {
            // Log deactivation message
            error_log(CUSTOM_ORDER_RESTRICTION_PLUGIN_NAME . ' plugin deactivated');
    
            // Clear scheduled event on deactivation
            wp_clear_scheduled_hook('reset_order_restrictions_event');
    
            // Log deactivation completed
            error_log('Deactivation completed');
        }
    
        // Schedule a daily event to reset order restrictions
        public function schedule_daily_event() {
            // Clear any existing scheduled events
            wp_clear_scheduled_hook('reset_order_restrictions_event');
    
            // Schedule a new event
            wp_schedule_event(strtotime('midnight'), 'daily', 'reset_order_restrictions_event');
    
            // Log scheduled event message
            error_log('Scheduled daily event to reset order restrictions');
        }
    
        // Hook into the scheduled event to reset order restrictions and amounts
        public function reset_order_restrictions() {
            // Log reset order restrictions message
            error_log('Resetting order restrictions');
    
            // Use WP_User_Query instead of get_users for better performance and flexibility
            $user_query = new WP_User_Query(array(
                'fields' => 'ID',
                // Add any other query parameters here
            ));
    
            $user_ids = $user_query->get_results();
    
            foreach ($user_ids as $user_id) {
                // Delete the transients that store the last order timestamp and amount
                delete_transient('last_order_timestamp_' . $user_id);
                delete_transient('last_order_amount_' . $user_id);
                // Reset the order count within 15 days
                update_user_meta($user_id, 'order_count_within_15_days', 0);
            }
        }
    
        // Add a custom field to user meta to store the last order timestamp and amount
        public function set_last_order_info($order_id) {
            // Log set last order info message
            error_log('Setting last order information');
    
            $order = wc_get_order($order_id);
            $user_id = $order->get_customer_id();
    
            if ($user_id) {
                $order_amount = $order->get_subtotal(); // Use subtotal instead of total
    
                // Set the transients that store the last order timestamp and amount
                // The transients will expire after 15 days
                set_transient('last_order_timestamp_' . $user_id, get_current_time('timestamp'), 15 * DAY_IN_SECONDS);
                set_transient('last_order_amount_' . $user_id, $order_amount, 15 * DAY_IN_SECONDS);
    
                // Increase order count within 15 days
                $order_count = get_user_meta($user_id, 'order_count_within_15_days', true);
                $order_count++;
                update_user_meta($user_id, 'order_count_within_15_days', $order_count);
            }
        }
    
        // Check if the user can place a new order based on the restrictions
        public function can_place_new_order() {
            // Log check can place new order message
            error_log('Checking if the user can place a new order');
    
            $user_id = wc_get_customer_id();
            if ($user_id) {
                $last_order_timestamp = get_transient('last_order_timestamp_' . $user_id); // Get the last order timestamp from the transient
                $last_order_amount = get_transient('last_order_amount_' . $user_id); // Get the last order amount from the transient
                $order_count_within_15_days = get_user_meta($user_id, 'order_count_within_15_days', true);
    
                if ($last_order_timestamp) {
                    $time_difference = get_current_time('timestamp') - $last_order_timestamp;
                    $hours_passed = $time_difference / 3600; // 3600 seconds in an hour
    
                    if ($hours_passed < 24) {
                        // User cannot place a new order until 24 hours have passed
                        return false;
                    } elseif ($hours_passed >= 24 && $hours_passed < (24 * 15)) {
                        // Within the 15-day period
                        if ($order_count_within_15_days >= 15 || $last_order_amount != wc_get_cart_subtotal()) {
                            // Order amount should be the same for 15 days
                            return false;
                        }
                    } else {
                        // After the 15-day period, reset the cycle
                        update_user_meta($user_id, 'order_count_within_15_days', 0);
                    }
                }
            }
    
            return true; // User can place a new order
        }
    
        // Filter the checkout availability based on the order restrictions
        public function filter_checkout_availability($can_checkout) {
            // Log filter checkout availability message
            error_log('Filtering checkout availability');
    
            if (!$this->can_place_new_order()) {
                // Escape the error message
                $error_message = esc_html__('You cannot place a new order yet or the order amount is different.');
                // Check if there are any existing error notices
                if (wc_notice_count('error') == 0) {
                    // Add a new error notice
                    wc_add_notice($error_message, 'error');
                }
                return false;
            }
            return $can_checkout;
        }
    }
    
    // Instantiate the plugin class
    $custom_order_restriction = new Custom_Order_Restriction();
    
    // Register uninstall hook
    register_uninstall_hook(__FILE__, array('Custom_Order_Restriction', 'uninstall'));
    ?>
    

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @mamagee,

    For reference, these particular forums are meant for general support with the core functionality of WooCommerce itself. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

Viewing 1 replies (of 1 total)
  • The topic ‘Internal Server Error’ is closed to new replies.