* @license GPL3 https://www.gnu.org/licenses/gpl-3.0.en.html * @since 1.0.0 */ namespace Optemiz\AWO; defined('ABSPATH') || exit; if (! class_exists('Frontend') ) { /** * Frontend class * * @class Frontend The class that manages all about frontend * * @category Frontend * @package Optemiz\AWO * @author Nazrul Islam Nayan * @license GPL3 https://www.gnu.org/licenses/gpl-3.0.en.html */ class Frontend { /** * Settings * * @var array|null */ public $settings = null; /** * Class constructor * * Sets up all the appropriate hooks and functions * within our plugin. * * @return void */ public function __construct() { $this->hooks(); do_action('hawo_frontend_loaded', $this); } /** * Instance. * * The instance will be created if it does not exist yet. * * @return self The main instance. * @since 1.0.0 */ public static function instance(): self { static $instance = null; if (is_null($instance) ) { $instance = new self(); } return $instance; } /** * All the executed hooks * * @return void */ protected function hooks(): void { // add_filter( 'woocommerce_payment_complete_order_status', array( $this, 'update_order_status_to_complete' ), -1, 2 ); add_filter( 'woocommerce_thankyou', array( $this, 'auto_complete_order_after_checkout' ), 10 ); } /** * After order is done, complete the order. * * @param int $order_id Order ID. * * @return string */ public function auto_complete_order_after_checkout($order_id) { if (! $order_id) { return; } $order = wc_get_order($order_id); if (! $order) { return; } // Early skip if already completed. if ($order->has_status('completed')) { return; } $settings = get_option('hawo_settings', []); if (empty($settings['enable'])) { return; } $should_autocomplete = true; // ✅ 1. Order Total Condition. if (isset($settings['order_total'])) { $mode = $settings['order_total']['mode'] ?? 'include'; $type = $settings['order_total']['value']['type'] ?? ""; $amount = floatval($settings['order_total']['value']['amount'] ?? 0); $total = floatval($order->get_total()); $is_in_range = true; if ($type === "min" && $amount > 0 && $total < $amount) { $is_in_range = false; } if ($type === "max" && $amount > 0 && $total > $amount) { $is_in_range = false; } if ($mode === 'include' && ! $is_in_range) { $should_autocomplete = false; } if ($mode === 'exclude' && $is_in_range) { $should_autocomplete = false; } } // ✅ 2. Shipping Methods Condition. if ($should_autocomplete && isset($settings['shipping_methods'])) { $mode = $settings['shipping_methods']['mode'] ?? 'include'; $values = (array) ($settings['shipping_methods']['value'] ?? []); $chosen_methods = []; foreach ($order->get_shipping_methods() as $item) { $chosen_methods[] = $item->get_method_id(); } $matched = array_intersect($chosen_methods, $values); if (! empty($values)) { if (($mode === 'include' && empty($matched)) || ($mode === 'exclude' && ! empty($matched))) { $should_autocomplete = false; } } } // ✅ 3. User Role Condition. if ($should_autocomplete && isset($settings['user_roles'])) { $mode = $settings['user_roles']['mode'] ?? 'include'; $values = (array) ($settings['user_roles']['value'] ?? []); $user = $order->get_user(); $roles = $user ? (array) $user->roles : []; $matched = array_intersect($roles, $values); if (! empty($values)) { if (($mode === 'include' && empty($matched)) || ($mode === 'exclude' && ! empty($matched))) { $should_autocomplete = false; } } } // ✅ 4. Payment Method Condition. if ($should_autocomplete && isset($settings['payment_methods'])) { $mode = $settings['payment_methods']['mode'] ?? 'include'; $values = (array) ($settings['payment_methods']['value'] ?? []); $payment_method = $order->get_payment_method(); if (! empty($values)) { if (($mode === 'include' && ! in_array($payment_method, $values, true)) || ($mode === 'exclude' && in_array($payment_method, $values, true)) ) { $should_autocomplete = false; } } } // ✅ 5. Product, Category, Tag, Type, Brand Matching. if ($should_autocomplete) { $product_ids = []; $product_cats = []; $product_tags = []; $product_brands = []; $product_types = []; foreach ($order->get_items() as $item) { $product = $item->get_product(); if (! $product) continue; $product_ids[] = $product->get_id(); $product_types[] = $product->get_type(); $product_cats = array_merge( $product_cats, wc_get_product_term_ids($product->get_id(), 'product_cat') ); $product_tags = array_merge( $product_tags, wc_get_product_term_ids($product->get_id(), 'product_tag') ); $product_brands = array_merge( $product_brands, wc_get_product_term_ids($product->get_id(), 'product_brand') ); } $product_checklist = [ 'products' => $product_ids, 'categories' => $product_cats, 'tags' => $product_tags, 'brands' => $product_brands, 'product_types' => $product_types, ]; foreach ($product_checklist as $key => $list) { if (! isset($settings[$key])) { continue; } $mode = $settings[$key]['mode'] ?? 'include'; $values = (array) ($settings[$key]['value'] ?? []); $matched = array_intersect($list, $values); if (! empty($list) && ! empty($values)) { if (($mode === 'include' && empty($matched)) || ($mode === 'exclude' && ! empty($matched))) { $should_autocomplete = false; break; } } } } // ✅ 6. Current Order Status (before complete). if ($should_autocomplete && isset($settings['order_status'])) { $mode = $settings['order_status']['mode'] ?? 'include'; $values = (array) ($settings['order_status']['value'] ?? []); $current_status = 'wc-' . $order->get_status(); if (! empty($values)) { if (($mode === 'include' && ! in_array($current_status, $values, true)) || ($mode === 'exclude' && in_array($current_status, $values, true)) ) { $should_autocomplete = false; } } } // ✅ 7. Finally, mark as completed if all checks pass. if ($should_autocomplete) { $order->update_status('completed', __('Order auto-completed.', 'advanced-autocomplete-orders-for-woocommerce')); } } /** * Automatically complte the orders. * * @param string $order_status Order Status. * @param int $order_id Order ID. * * @return string */ public function update_order_status_to_complete( $order_status, $order_id ) { $order_status = 'completed'; return $order_status; } } }