Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MrImranJaved/7a9e2956222ad31f000f490c0d59f375 to your computer and use it in GitHub Desktop.
Save MrImranJaved/7a9e2956222ad31f000f490c0d59f375 to your computer and use it in GitHub Desktop.
a solution to implement an add-on product that only appears when a customer orders a minimum quantity of the main product. This uses WooCommerce hooks and doesn't require a plugin
<?php
/**
* Add conditional add-on product option when minimum quantity is met
*/
function add_conditional_addon_product() {
global $product;
// Only run for specific product IDs (adjust as needed)
$target_product_ids = array(123); // Replace with your main product ID
// Add-on product details
$addon_product_id = 456; // Replace with your add-on product ID
$min_quantity = 10; // Minimum quantity to show add-on
$addon_price = 7; // Add-on price
if (in_array($product->get_id(), $target_product_ids)) {
?>
<div id="conditional-addon" style="display: none; margin-top: 20px; padding: 15px; background: #f5f5f5; border: 1px solid #ddd;">
<h4>Special Offer</h4>
<p>Since you're ordering <?php echo $min_quantity; ?> or more, you can add a storage box for just $<?php echo $addon_price; ?>!</p>
<label>
<input type="checkbox" name="add_addon_product" id="add_addon_product">
Add Storage Box (+$<?php echo $addon_price; ?>)
</label>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Check quantity on page load and quantity change
function checkQuantity() {
var quantity = parseInt($('input.qty').val()) || 1;
if (quantity >= <?php echo $min_quantity; ?>) {
$('#conditional-addon').slideDown();
} else {
$('#conditional-addon').slideUp();
$('#add_addon_product').prop('checked', false);
}
}
// Initial check
checkQuantity();
// Check when quantity changes
$('input.qty').on('change', function() {
checkQuantity();
});
// Add click handler for the checkbox
$('form.cart').on('submit', function(e) {
if ($('#add_addon_product').is(':checked')) {
// Create a hidden input to pass the add-on selection
$(this).append('<input type="hidden" name="add_addon_to_cart" value="1">');
}
});
});
</script>
<?php
}
}
add_action('woocommerce_after_add_to_cart_button', 'add_conditional_addon_product');
/**
* Add the add-on product to cart when checkbox is checked
*/
function add_addon_product_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
if (!isset($_POST['add_addon_to_cart']) || !$_POST['add_addon_to_cart']) {
return;
}
// Add-on product details (must match above)
$addon_product_id = 456; // Replace with your add-on product ID
$min_quantity = 10; // Minimum quantity to show add-on
// Verify the main product quantity meets minimum
if ($quantity >= $min_quantity) {
// Check if add-on is already in cart
$addon_in_cart = false;
foreach (WC()->cart->get_cart() as $cart_item) {
if ($cart_item['product_id'] == $addon_product_id && isset($cart_item['addon_for']) {
$addon_in_cart = true;
break;
}
}
// Add add-on to cart if not already there
if (!$addon_in_cart) {
WC()->cart->add_to_cart($addon_product_id, 1, 0, array(), array('addon_for' => $product_id));
}
}
}
add_action('woocommerce_add_to_cart', 'add_addon_product_to_cart', 10, 6);
/**
* Hide add-on product from shop and search
*/
function hide_addon_product($query) {
if (!is_admin() && $query->is_main_query() && (is_shop() || is_product_category() || is_product_tag() || is_search())) {
$addon_product_id = 456; // Replace with your add-on product ID
$post__not_in = $query->get('post__not_in');
$post__not_in = is_array($post__not_in) ? $post__not_in : array();
$post__not_in[] = $addon_product_id;
$query->set('post__not_in', $post__not_in);
}
}
add_action('pre_get_posts', 'hide_addon_product');
/**
* Remove add-on if main product is removed from cart
*/
function remove_addon_if_main_removed($cart_item_key, $cart) {
$cart_item = $cart->cart_contents[$cart_item_key];
// If removed item has addon_for, it's an add-on - we don't need to do anything
if (isset($cart_item['addon_for'])) {
return;
}
$addon_product_id = 456; // Replace with your add-on product ID
// Check if removed item is a main product that might have an add-on
foreach ($cart->cart_contents as $key => $item) {
if (isset($item['addon_for']) && $item['addon_for'] == $cart_item['product_id'] && $item['product_id'] == $addon_product_id) {
$cart->remove_cart_item($key);
break;
}
}
}
add_action('woocommerce_remove_cart_item', 'remove_addon_if_main_removed', 10, 2);
/*
Why this code was written?
This code was written on request posted on this reddit post. https://www.reddit.com/r/woocommerce/s/4S2SuBBWZY
*/
/*
Setup Instructions
Replace 123 with your main product ID (the product that needs minimum quantity)
Replace 456 with your add-on product ID (the storage box product)
Adjust $min_quantity and $addon_price as needed
The code will:
Show the add-on option only when quantity ≥ 10
Add the add-on to cart when checkbox is checked
Keep the add-on linked to the main product
Remove the add-on if the main product is removed
Hide the add-on product from shop/category pages
Alternative Plugin Solutions
If you prefer a plugin approach, consider these options:
WooCommerce Product Add-ons - Official WooCommerce extension that allows conditional fields
Conditional Discounts for WooCommerce - Can set rules for add-ons based on quantity
WPC Product Bundles - Creates bundle relationships while maintaining inventory
# Support
For support, please contact MoizWordpress Development Team. We're happy to help with any questions or issues you may have.
# Changelog
- Version 1.0: Initial release
# Contributing
If you'd like to contribute to the development of this plugin, please fork the repository and submit a pull request. We appreciate your feedback and contributions!
# Email:
[email protected]
# WhatsApp
+92-304-4887447
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment