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
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.
- Uses the
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.
- Retrieves and sanitizes the values entered in the custom fields (
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 usingget_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
.
- Retrieves the custom multiple value (
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 usingget_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
.
- Loops through each item in the cart using
- 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
andapply_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.
Was This Tutorial Helpful?