id) && ( strpos($screen->id, 'abovewp-min-max-quantity') !== false || strpos($screen->id, 'toplevel_page_abovewp') !== false )) { $hide_notices_css = ' .notice, .updated, .update-nag, .error, .warning, .info { display: none !important; }'; wp_add_inline_style('abovewp-min-max-quantity', $hide_notices_css); } } /** * Remove WordPress admin notices on plugin pages */ public function remove_admin_notices_on_plugin_pages() { $screen = get_current_screen(); // Only remove notices on our plugin pages if (isset($screen->id) && ( strpos($screen->id, 'abovewp-min-max-quantity') !== false || strpos($screen->id, 'toplevel_page_abovewp') !== false )) { // Remove all admin notices remove_all_actions('admin_notices'); remove_all_actions('all_admin_notices'); } } /** * Display plugin card on AboveWP dashboard */ public function display_plugin_card() { ?>

'; // Minimum Quantity woocommerce_wp_text_input(array( 'id' => '_awp_min_quantity', 'label' => __('Minimum Quantity', 'abovewp-min-max-quantity'), 'desc_tip' => true, 'description' => __('Minimum quantity required to purchase this product. Leave blank for no minimum.', 'abovewp-min-max-quantity'), 'type' => 'number', 'custom_attributes' => array( 'step' => '1', 'min' => '0' ) )); // Maximum Quantity woocommerce_wp_text_input(array( 'id' => '_awp_max_quantity', 'label' => __('Maximum Quantity', 'abovewp-min-max-quantity'), 'desc_tip' => true, 'description' => __('Maximum quantity allowed to purchase this product. Leave blank or set to 0 for no maximum.', 'abovewp-min-max-quantity'), 'type' => 'number', 'custom_attributes' => array( 'step' => '1', 'min' => '0' ) )); echo ''; } /** * Save the custom fields */ public function save_min_max_fields($post_id) { // Check if user has permission to edit this post if (!current_user_can('edit_post', $post_id)) { return; } // Check if this is an autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } // Verify nonce - WooCommerce handles its own nonce verification if (!isset($_POST['woocommerce_meta_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['woocommerce_meta_nonce'])), 'woocommerce_save_data')) { return; } // Minimum Quantity $min_qty = isset($_POST['_awp_min_quantity']) ? absint($_POST['_awp_min_quantity']) : ''; update_post_meta($post_id, '_awp_min_quantity', $min_qty); // Maximum Quantity - treat 0 as infinite (empty) $max_qty = isset($_POST['_awp_max_quantity']) ? absint($_POST['_awp_max_quantity']) : ''; // If max quantity is 0, treat it as infinite (no maximum) if ($max_qty === 0) { $max_qty = ''; } update_post_meta($post_id, '_awp_max_quantity', $max_qty); } /** * Validate the quantity when adding to cart */ public function validate_add_to_cart_quantity($passed, $product_id, $quantity) { $min_qty = get_post_meta($product_id, '_awp_min_quantity', true); $max_qty = get_post_meta($product_id, '_awp_max_quantity', true); // Check minimum quantity if ($min_qty != '' && $quantity < $min_qty) { $product = wc_get_product($product_id); $product_title = $product->get_title(); wc_add_notice( sprintf( /* translators: %1$s: product title, %2$s: minimum quantity */ __('The minimum quantity required for %1$s is %2$s', 'abovewp-min-max-quantity'), $product_title, $min_qty ), 'error' ); return false; } // Check maximum quantity - treat 0 as infinite (no maximum) if ($max_qty != '' && $max_qty != '0' && $max_qty > 0 && $quantity > $max_qty) { $product = wc_get_product($product_id); $product_title = $product->get_title(); wc_add_notice( sprintf( /* translators: %1$s: product title, %2$s: maximum quantity */ __('The maximum quantity allowed for %1$s is %2$s', 'abovewp-min-max-quantity'), $product_title, $max_qty ), 'error' ); return false; } return $passed; } /** * Set min/max values for quantity input */ public function quantity_input_args($args, $product) { $product_id = $product->get_id(); $min_qty = get_post_meta($product_id, '_awp_min_quantity', true); $max_qty = get_post_meta($product_id, '_awp_max_quantity', true); // Set minimum quantity if ($min_qty != '') { $args['min_value'] = $min_qty; // If current value is less than min, set it to min if ($args['input_value'] < $min_qty) { $args['input_value'] = $min_qty; } } // Set maximum quantity - treat 0 as infinite (no maximum) if ($max_qty != '' && $max_qty != '0' && $max_qty > 0) { $args['max_value'] = $max_qty; } return $args; } } // Initialize the plugin function abovewp_min_max_quantity_init() { // Initialize parent menu AboveWP_Admin_Menu::init(); // Initialize plugin AboveWP_Min_Max_Quantity::get_instance(); } add_action('plugins_loaded', 'abovewp_min_max_quantity_init');