/** * Validate container against our minimum quantity requirement * * @param bool $passed * @param obj $mnm_stock * @param obj $product * @return void */ public static function validate_container($passed, $mnm_stock, $product) { $total_items_in_container = $mnm_stock->get_total_quantity(); $min_qty = intval(get_post_meta($product->id, '_mnm_container_size', true)); $max_qty = get_post_meta($product->id, '_mnm_max_container_size', true); // if a max quantity exists and is not equal to the min quantity we have a non-fixed container size if (false !== $max_qty && $min_qty != intval($max_qty)) { $min_qty = $min_qty > 0 ? $min_qty : 1; $max_qty = intval($max_qty); // validate that an unlimited container is in min/max range & build a specific error message if ($max_qty > 0 && $min_qty > 0 && ($total_items_in_container > $max_qty || $total_items_in_container < $min_qty)) { $message = $total_items_in_container > $max_qty ? __('You have selected too many items.', 'woocommerce-mix-and-match-min-max-quantities') : __('You have selected too few items.', 'woocommerce-mix-and-match-min-max-quantities'); $message .= ' ' . sprintf(__('Please choose between %d and %d items for "%s".', 'woocommerce-mix-and-match-min-max-quantities'), $min_qty, $max_qty, $product->get_title()); wc_add_notice($message, 'error'); $passed = false; } else { if ($min_qty > 0 && $total_items_in_container < $min_qty) { wc_add_notice(sprintf(__('Please choose at least %d items for "%s".', 'woocommerce-mix-and-match-min-max-quantities'), $min_qty, $product->get_title()), 'error'); $passed = false; } } } return $passed; }
/** * Validate container against our minimum quantity requirement * * @param bool $passed * @param obj $mnm_stock * @param obj $product * @return void */ public static function validate_container($error_message, $mnm_stock, $product) { $total_items_in_container = $mnm_stock->get_total_quantity(); $min_qty = $product->get_container_size(); $max_qty = self::get_max_container_size($product); // if a max quantity exists and is not equal to the min quantity we have a non-fixed container size if ($min_qty != $max_qty) { // reset the error message $error_message = false; // min_container_size is always at least 1, because the container can't be empty $min_qty = $min_qty > 0 ? $min_qty : 1; // validate that a container is in min/max range & build a specific error message if ($max_qty > 0 && $min_qty > 0 && ($total_items_in_container > $max_qty || $total_items_in_container < $min_qty)) { $error_message = $total_items_in_container > $max_qty ? __('You have selected too many items.', 'woocommerce-mix-and-match-min-max-quantities') : __('You have selected too few items.', 'woocommerce-mix-and-match-min-max-quantities'); $error_message .= ' ' . sprintf(__('Please choose between %d and %d items for "%s".', 'woocommerce-mix-and-match-min-max-quantities'), $min_qty, $max_qty, $product->get_title()); } else { if ($min_qty > 0 && $total_items_in_container < $min_qty) { $error_message = sprintf(_n('Please choose at least %d item for "%s".', 'Please choose at least %d items for "%s".', $min_qty, 'woocommerce-mix-and-match-min-max-quantities'), $min_qty, $product->get_title()); } } } return $error_message; }