PHP

Implementing Minimum and Maximum Quantity Limits Per Category in WooCommerce

To thoroughly understand how this code works, we’ll check both the admin-facing and public-facing code in detail.

Admin Facing Code

The admin-facing code provides a user interface for setting up the quantity limits in the WordPress admin dashboard. It allows administrators to enable/disable the functionality, define error messages, and set quantity limits for product categories based on user roles.

Initial Setup and Hooks

  • __construct: Initializes the class and sets up hooks.
  • get_product_categories: Retrieves product categories.
  • add_menu_page: Adds the plugin settings page to the admin menu.
  • add_admin_style and add_admin_script: Enqueues custom styles and scripts for the admin page.

Retrieve Product Categories

  • get_product_categories: Fetches all product categories and stores them in a global variable $lpc_product_categories.

Add Menu Page

  • add_menu_page: Adds a new page to the WordPress admin menu for the plugin settings.
  • esc_html__: Used for localization.
  • manage_options: Capability required to access this menu.
  • lpc-settings: The slug for the menu.
  • settings_callback: The callback function to display the settings page.
  • dashicons-businessman: The icon for the menu.
  • 55.5: The position of the menu.

Settings Page Callback

  • settings_callback: Includes the settings page template.

Enqueue Styles and Scripts

  • add_admin_style: Enqueues the admin CSS file.
  • add_admin_script: Enqueues the admin JS files and localizes the script to pass PHP data to JavaScript.

Settings Form Handling

  • Global Variables: $lpc_product_categories and $wp_roles are used to fetch product categories and user roles.
  • Form Handling: When the form is submitted, the settings are saved to the WordPress options table using update_option.
  • Fetching Settings: Retrieves existing settings using get_option.
  • User Roles: Fetches all editable user roles.

Settings Form

  • Settings Form: This form contains the settings for enabling the functionality, defining error messages, and setting quantity limits.
  • Enable Checkbox: Toggles the functionality on or off.
  • Error Messages: Fields to enter custom error messages for minimum and maximum quantity limits.
  • Quantity Thresholds: A repeater field to set quantity limits for different customer groups and product categories.

Public Facing Code

The public-facing code enforces the quantity limits set by the admin on the WooCommerce cart page. It checks the quantities before proceeding to checkout and displays error messages if the limits are not met.

Initial Setup and Hooks

  • __construct: Initializes the class and retrieves the plugin settings.
  • woocommerce_before_cart: Hook that calls the quantity check function before displaying the cart.

Helper Function to Get Product Category by ID

  • get_product_category_by_id: Fetches the name of a product category by its ID.

Check Cart Quantity Limits

  • custom_check_cart_qty_before_checkout: The main function to check cart quantity limits.
  • Retrieve Settings: Fetches the quantity limits from the settings.
  • Calculate Category Quantities: Loops through the cart items and calculates the total quantities for each product category.
  • Check Limits: Compares the quantities against the set limits and displays error messages if the limits are exceeded or not met. It also removes the “Proceed to Checkout” button if the limits are violated.

Leave a Reply