Add Discount for Multiples In Item Subtotal & Cart Totals

This code enables you to set a discount for multiples, replace the original subtotal line item with the discounted subtotal and adds the discounted amount to the cart totals in WooCommerce.

You can add the multiple and discount amount on a per product basis :

Demo Video

Code Installation

Copy and paste to the end of your child themes functions file or custom functionality/ code snippets plugin.

How The Functions Work

  1. add_custom_fields Function:
    • Purpose: Adds custom fields to the Product Data General tab in the WooCommerce admin.
    • Explanation:
      • Uses the woocommerce_wp_text_input function to create two text input fields for the custom values (_custom_multiple_value and _custom_discount_amount).
      • The fields are displayed in the General tab of the product data section in the WooCommerce admin.
  2. save_custom_fields Function:
    • Purpose: Saves the values entered in the custom fields when a product is saved or updated.
    • Explanation:
      • Retrieves and sanitizes the values entered in the custom fields (_custom_multiple_value and _custom_discount_amount).
      • Uses the update_post_meta function to save these values for the product.
      • This ensures that the custom values are associated with each product and can be retrieved later.
  3. replace_with_discounted_subtotal Function:
    • Purpose: Modifies the cart item subtotal display in the cart based on custom conditions.
    • Explanation:
      • Retrieves the custom multiple value (_custom_multiple_value) and discount amount (_custom_discount_amount) for the current product using get_post_meta.
      • Checks if the product quantity meets the specified conditions (quantity is a multiple of the custom multiple value).
      • If conditions are met, calculates a discount based on the custom discount amount.
      • Modifies the subtotal display with a strikeout for the original subtotal and an inserted subtotal with the discount applied using sprintf.
  4. apply_custom_discount Function:
    • Purpose: Applies a custom discount to the cart based on product conditions.
    • Explanation:
      • Loops through each item in the cart using $cart->get_cart().
      • Retrieves the custom multiple value (_custom_multiple_value) and discount amount (_custom_discount_amount) for each product using get_post_meta.
      • Checks if the product quantity meets the specified conditions (quantity is a multiple of the custom multiple value).
      • If conditions are met, calculates a discount based on the custom discount amount and accumulates the total discount for all eligible products.
      • Adds a fee to the cart with the total discount amount using $cart->add_fee.
  5. WooCommerce Functions:
    • woocommerce_wp_text_input:
      • Purpose: Creates a text input field with WooCommerce styling.
      • Usage: Used in add_custom_fields to create text input fields for custom values.
    • update_post_meta:
      • Purpose: Saves metadata for a specified post.
      • Usage: Used in save_custom_fields to save the custom values (_custom_multiple_value and _custom_discount_amount) for each product.
    • get_post_meta:
      • Purpose: Retrieves the value of a specific post meta field.
      • Usage: Used in both replace_with_discounted_subtotal and apply_custom_discount to retrieve the custom multiple value and discount amount for each product.
    • wc_price:
      • Purpose: Formats a given price as a string.
      • Usage: Used in replace_with_discounted_subtotal to format the original and modified subtotals as currency strings.
    • $cart->get_cart():
      • Purpose: Retrieves an array of cart items.
      • Usage: Used in apply_custom_discount to loop through each item in the cart and perform calculations.
    • $cart->add_fee:
      • Purpose: Adds a fee to the cart.
      • Usage: Used in apply_custom_discount to add a fee with the total discount amount to the cart.

Download Folder

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.