You've already forked wc-tier-and-package-prices
Release version 1.1.0 - Package quantity restriction feature
Added comprehensive package quantity restriction functionality that allows limiting product purchases to predefined package sizes only. Features: - Global setting to enable package quantity restrictions - Per-product override for quantity restrictions - Automatic hiding of quantity input field when restricted - Frontend validation with package selection UI - Server-side cart validation - User-friendly error messages - Complete translations for all supported languages 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,7 @@ class WC_TPP_Cart {
|
||||
add_action('woocommerce_before_calculate_totals', array($this, 'apply_tier_package_pricing'), 10, 1);
|
||||
add_filter('woocommerce_cart_item_price', array($this, 'display_cart_item_price'), 10, 3);
|
||||
add_filter('woocommerce_cart_item_subtotal', array($this, 'display_cart_item_subtotal'), 10, 3);
|
||||
add_filter('woocommerce_add_to_cart_validation', array($this, 'validate_package_quantity'), 10, 3);
|
||||
}
|
||||
|
||||
public function apply_tier_package_pricing($cart) {
|
||||
@@ -89,6 +90,53 @@ class WC_TPP_Cart {
|
||||
}
|
||||
return $subtotal;
|
||||
}
|
||||
|
||||
public function validate_package_quantity($passed, $product_id, $quantity) {
|
||||
// Check if restriction is enabled globally or for this product
|
||||
$global_restrict = get_option('wc_tpp_restrict_package_quantities', 'no') === 'yes';
|
||||
$product_restrict = get_post_meta($product_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
||||
|
||||
if (!$global_restrict && !$product_restrict) {
|
||||
return $passed;
|
||||
}
|
||||
|
||||
// Get packages for this product
|
||||
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
||||
|
||||
if (empty($packages) || !is_array($packages)) {
|
||||
return $passed;
|
||||
}
|
||||
|
||||
// Check if the quantity matches any package
|
||||
$valid_quantity = false;
|
||||
$available_quantities = array();
|
||||
|
||||
foreach ($packages as $package) {
|
||||
$available_quantities[] = $package['qty'];
|
||||
if ($quantity == $package['qty']) {
|
||||
$valid_quantity = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$valid_quantity) {
|
||||
$product = wc_get_product($product_id);
|
||||
$product_name = $product ? $product->get_name() : __('this product', 'wc-tier-package-prices');
|
||||
|
||||
wc_add_notice(
|
||||
sprintf(
|
||||
__('The quantity %1$d is not available for %2$s. Please choose from the available package sizes: %3$s', 'wc-tier-package-prices'),
|
||||
$quantity,
|
||||
$product_name,
|
||||
implode(', ', $available_quantities)
|
||||
),
|
||||
'error'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
return $passed;
|
||||
}
|
||||
}
|
||||
|
||||
new WC_TPP_Cart();
|
||||
|
||||
Reference in New Issue
Block a user