get_settings(); if ($s['enabled'] !== 'yes' || (float) $s['threshold'] <= 0) { return; } add_action('woocommerce_checkout_process', array($this, 'enforce_on_submit')); add_action('woocommerce_store_api_cart_errors', array($this, 'enforce_block_checkout'), 10, 2); add_action('woocommerce_before_cart', array($this, 'cart_notice')); add_action('woocommerce_before_checkout_form', array($this, 'checkout_notice'), 5); } public function get_settings($raw = false) { $stored = get_option(self::OPTION_KEY, array()); if (!is_array($stored)) { $stored = array(); } return $raw ? (array_merge(array( 'enabled' => 'no', 'threshold' => 5.00, 'message' => '', ), $stored)) : self::fill_i18n_defaults(array_merge(array( 'enabled' => 'no', 'threshold' => 5.00, 'message' => '', ), $stored)); } private static function i18n_labels() { return array( 'message' => __('Minimum order amount is {min}. Add {remaining} more to proceed to checkout.', 'ai-eshop-optimizer'), ); } private static function fill_i18n_defaults(array $s) { if (!did_action('init')) { return $s; } foreach (self::i18n_labels() as $k => $v) { if (!isset($s[$k]) || '' === trim((string) $s[$k])) { $s[$k] = $v; } } return $s; } public function ajax_save() { AIEO_DMM::verify_ajax_request(); $patch = array( 'enabled' => !empty($_POST['enabled']) ? 'yes' : 'no', 'threshold' => isset($_POST['threshold']) ? max(0, (float) $_POST['threshold']) : 0, 'message' => isset($_POST['message']) ? sanitize_text_field(wp_unslash($_POST['message'])) : '', ); $aieo_mo_default = 'Minimum order amount is {min}. Add {remaining} more to proceed to checkout.'; $aieo_mo_labels = self::i18n_labels(); if (in_array(trim($patch['message']), array($aieo_mo_default, trim((string) $aieo_mo_labels['message'])), true)) { $patch['message'] = ''; } update_option(self::OPTION_KEY, $patch, false); wp_send_json_success(array('message' => __('Minimum order settings saved.', 'ai-eshop-optimizer'))); } private function current_subtotal() { if (function_exists('WC') && WC() && WC()->cart) { return (float) WC()->cart->get_subtotal(); } return 0.0; } private function build_message($min, $current) { $s = $this->get_settings(); $template = (string) $s['message']; return strtr($template, array( '{min}' => wp_strip_all_tags(wc_price($min)), '{remaining}' => wp_strip_all_tags(wc_price(max(0, $min - $current))), '{current}' => wp_strip_all_tags(wc_price($current)), )); } public function enforce_on_submit() { $s = $this->get_settings(); $min = (float) $s['threshold']; $cur = $this->current_subtotal(); if ($cur > 0 && $cur < $min) { wc_add_notice($this->build_message($min, $cur), 'error'); } } public function enforce_block_checkout($errors, $cart) { if (!is_wp_error($errors)) { return; } $s = $this->get_settings(); $min = (float) $s['threshold']; $cur = ($cart && is_callable(array($cart, 'get_subtotal'))) ? (float) $cart->get_subtotal() : $this->current_subtotal(); if ($cur > 0 && $cur < $min) { $errors->add('aieo_min_order', $this->build_message($min, $cur)); } } public function cart_notice() { $s = $this->get_settings(); $min = (float) $s['threshold']; $cur = $this->current_subtotal(); if ($cur > 0 && $cur < $min) { wc_print_notice($this->build_message($min, $cur), 'notice'); } } public function checkout_notice() { if (!function_exists('WC') || !WC() || !WC()->cart) { return; } $s = $this->get_settings(); $min = (float) $s['threshold']; $cur = $this->current_subtotal(); if ($cur > 0 && $cur < $min) { wc_print_notice($this->build_message($min, $cur), 'error'); } } public function render_subtab() { $s = $this->get_settings(true); $aieo_mo = self::i18n_labels(); $currency = function_exists('get_woocommerce_currency') ? get_woocommerce_currency() : 'EUR'; $sym = function_exists('get_woocommerce_currency_symbol') ? html_entity_decode(get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8') : $currency; $form_id = 'aieo-card-min-order-threshold-and-message-form'; list($open, $close) = AIEO_DMM_UI::card(array( 'anchor' => 'min-order-threshold-and-message', 'title' => __('Threshold and message', 'ai-eshop-optimizer'), 'description' => __('Block checkout submission below a configurable cart subtotal. Shown to the customer at cart, checkout entry, and as a hard error on submit.', 'ai-eshop-optimizer'), 'footer' => class_exists('\Rooster\Admin\States') ? \Rooster\Admin\States::save_button($form_id) : '', )); echo $open; $rows = \Rooster\Admin\Ui::field_row(array( 'id' => 'aieo-mo-enabled', 'label' => __('Enable minimum order', 'ai-eshop-optimizer'), 'control' => \Rooster\Admin\Ui::toggle('enabled', $s['enabled'] === 'yes', array('id' => 'aieo-mo-enabled', 'hidden' => false, 'label' => __('Block checkout below the threshold', 'ai-eshop-optimizer'))), )); $rows .= \Rooster\Admin\Ui::field_row(array( 'id' => 'aieo-mo-threshold', 'label' => __('Threshold', 'ai-eshop-optimizer') . ' (' . $currency . ')', 'control' => \Rooster\Admin\Ui::input('threshold', (string) $s['threshold'], array('id' => 'aieo-mo-threshold', 'type' => 'number', 'short' => true, 'unit' => $sym, 'min' => 0, 'step' => 'any')), 'help' => __('Cart subtotal floor. Compared against `WC()->cart->get_subtotal()`, which excludes shipping + fees.', 'ai-eshop-optimizer'), )); $rows .= \Rooster\Admin\Ui::field_row(array( 'id' => 'aieo-mo-message', 'label' => __('Message', 'ai-eshop-optimizer'), 'control' => \Rooster\Admin\Ui::input('message', (string) $s['message'], array('id' => 'aieo-mo-message', 'placeholder' => (string) $aieo_mo['message'])), 'help' => __('Tokens: {min} {remaining} {current}', 'ai-eshop-optimizer'), )); echo AIEO_DMM_UI::card_form('aieo_dmm_min_order_save', $this->get_id(), $form_id, '