*/ private $settings; /** * Descendant-expanded category IDs, cached per instance. * * @var array */ private $expanded = array(); /** * Constructor. * * @since 1.0.0 * @param array $settings Module settings. */ public function __construct( array $settings ) { $this->settings = $settings; } /** * Rule labels, in display order. Extend with `apwc_autocomplete_rules` and match with * `apwc_autocomplete_rule_matches`. * * @since 1.0.0 * @return array */ public static function labels() { /** * Filter the autocomplete rules (key => label). * * @since 1.0.0 * @param array $rules Rules. */ return (array) apply_filters( 'apwc_autocomplete_rules', array( 'paid' => __( 'The order is paid', 'admin-powerups-for-woocommerce' ), 'virtual' => __( 'Every item is virtual', 'admin-powerups-for-woocommerce' ), 'downloadable' => __( 'Every item is downloadable', 'admin-powerups-for-woocommerce' ), 'products' => __( 'It has selected products', 'admin-powerups-for-woocommerce' ), 'categories' => __( 'It has products in selected categories', 'admin-powerups-for-woocommerce' ), 'tags' => __( 'It has products with selected tags', 'admin-powerups-for-woocommerce' ), 'shipping_classes' => __( 'It has products in selected shipping classes', 'admin-powerups-for-woocommerce' ), 'gateways' => __( 'It was paid with a selected payment method', 'admin-powerups-for-woocommerce' ), ) ); } /** * Evaluate an order. * * @since 1.0.0 * @param WC_Order $order Order. * @param bool $paid_context True when called at the moment payment completes. * @return array{complete: bool, reason: string, blocked: string, paid: bool, checks: array} */ public function evaluate( WC_Order $order, $paid_context = false ) { $labels = self::labels(); $enabled = array_values( array_intersect( array_keys( $labels ), (array) ( $this->settings['rules'] ?? array() ) ) ); $paid = $paid_context || $this->is_paid( $order ); $result = array( 'complete' => false, 'reason' => '', 'blocked' => '', 'paid' => $paid, 'checks' => array(), ); foreach ( $labels as $rule => $label ) { $on = in_array( $rule, $enabled, true ); $matched = $on && $this->rule_matches( $rule, $order, $paid ); // Only the payment-method rule may complete an unpaid order: the merchant chose it on purpose. if ( $matched && ! $paid && 'gateways' !== $rule ) { $matched = false; } $result['checks'][ $rule ] = array( 'label' => $label, 'on' => $on, 'matched' => $matched, ); } if ( ! $enabled ) { $result['blocked'] = __( 'No rules are on.', 'admin-powerups-for-woocommerce' ); return $result; } $blocked = $this->exception( $order ); if ( '' !== $blocked ) { $result['blocked'] = $blocked; return $result; } $matched = array_keys( array_filter( $result['checks'], static fn( $c ) => $c['matched'] ) ); if ( 'all' === ( $this->settings['match'] ?? 'any' ) ) { $complete = count( $matched ) === count( $enabled ); $reason = __( 'all rules match', 'admin-powerups-for-woocommerce' ); } else { $complete = (bool) $matched; $reason = $matched ? $labels[ $matched[0] ] : ''; } /** * Final say on autocompleting an order. Return '' to leave the order alone, or a reason. * * @since 1.0.0 * @param string $reason Reason ('' when not completing). * @param WC_Order $order Order. * @param array $checks Per-rule results. */ $reason = (string) apply_filters( 'apwc_autocomplete_decision', $complete ? $reason : '', $order, $result['checks'] ); $result['complete'] = '' !== $reason; $result['reason'] = $reason; if ( ! $result['complete'] && $complete ) { $result['blocked'] = __( 'Stopped by custom code.', 'admin-powerups-for-woocommerce' ); } return $result; } /** * Whether the order counts as paid outside the payment-complete moment. * * `WC_Order::is_paid()` is true for Processing, and cash on delivery (plus other "not paid * yet" methods) reach Processing at checkout before any money arrives, so those never count. * * @since 1.0.0 * @param WC_Order $order Order. * @return bool */ private function is_paid( WC_Order $order ) { /** * Filter payment methods that put orders in Processing before any money arrives (pay on * delivery). Their orders never count as paid for the "The order is paid" rule. Bank * transfer and cheque aren't here: WooCommerce holds those On hold until staff confirm * payment, and moving them to Processing means the money arrived. * * @since 1.0.0 * @param string[] $ids Gateway IDs. */ $on_delivery = (array) apply_filters( 'apwc_autocomplete_pay_on_delivery_gateways', array( 'cod' ) ); if ( in_array( $order->get_payment_method(), $on_delivery, true ) ) { return false; } return $order->is_paid(); } /** * Why an exception blocks the order, or ''. * * @since 1.0.0 * @param WC_Order $order Order. * @return string */ public function exception( WC_Order $order ) { if ( ! empty( $this->settings['skip_customer_note'] ) && '' !== trim( (string) $order->get_customer_note() ) ) { return __( 'The customer left a note.', 'admin-powerups-for-woocommerce' ); } $max = (float) ( $this->settings['max_total'] ?? 0 ); if ( $max > 0 && (float) $order->get_total() > $max ) { /* translators: %s: amount. */ return sprintf( __( 'The total is above %s.', 'admin-powerups-for-woocommerce' ), wp_strip_all_tags( wc_price( $max ) ) ); } if ( $this->has_products( $order, (array) ( $this->settings['exclude_products'] ?? array() ) ) ) { return __( 'It has an excluded product.', 'admin-powerups-for-woocommerce' ); } if ( $this->has_terms( $order, 'product_cat', (array) ( $this->settings['exclude_categories'] ?? array() ) ) ) { return __( 'It has a product in an excluded category.', 'admin-powerups-for-woocommerce' ); } return ''; } /** * Whether one rule matches. * * @since 1.0.0 * @param string $rule Rule key. * @param WC_Order $order Order. * @param bool $paid Whether the order counts as paid. * @return bool */ private function rule_matches( $rule, WC_Order $order, $paid ) { $products = $this->products( $order ); // "Every item" rules judge every line item: a deleted product can't be checked, so they fail. $complete = $products && count( $products ) === count( $order->get_items( 'line_item' ) ); switch ( $rule ) { case 'paid': $matched = $paid; break; case 'virtual': $matched = $complete && count( array_filter( $products, static fn( $p ) => $p->is_virtual() ) ) === count( $products ); break; case 'downloadable': $matched = $complete && count( array_filter( $products, static fn( $p ) => $p->is_downloadable() ) ) === count( $products ); break; case 'products': $matched = $this->has_products( $order, (array) ( $this->settings['products'] ?? array() ) ); break; case 'categories': $matched = $this->has_terms( $order, 'product_cat', (array) ( $this->settings['categories'] ?? array() ) ); break; case 'tags': $matched = $this->has_terms( $order, 'product_tag', (array) ( $this->settings['tags'] ?? array() ) ); break; case 'shipping_classes': $classes = array_map( 'intval', (array) ( $this->settings['shipping_classes'] ?? array() ) ); $matched = (bool) array_filter( $products, static fn( $p ) => in_array( (int) $p->get_shipping_class_id(), $classes, true ) ); break; case 'gateways': $matched = in_array( $order->get_payment_method(), (array) ( $this->settings['gateways'] ?? array() ), true ); break; default: $matched = false; } /** * Filter whether an autocomplete rule matches. Use it to implement rules added with * `apwc_autocomplete_rules`. * * @since 1.0.0 * @param bool $matched Match. * @param string $rule Rule key. * @param WC_Order $order Order. * @param array $settings Module settings. */ return (bool) apply_filters( 'apwc_autocomplete_rule_matches', $matched, $rule, $order, $this->settings ); } /** * Products on the order's line items (missing products skipped). * * @since 1.0.0 * @param WC_Order $order Order. * @return WC_Product[] */ private function products( WC_Order $order ) { $products = array(); foreach ( $order->get_items( 'line_item' ) as $item ) { if ( ! $item instanceof WC_Order_Item_Product ) { continue; } $product = $item->get_product(); if ( $product instanceof WC_Product ) { $products[] = $product; } } return $products; } /** * Whether the order contains any of the product or variation IDs. * * @since 1.0.0 * @param WC_Order $order Order. * @param int[] $ids IDs. * @return bool */ private function has_products( WC_Order $order, array $ids ) { $ids = array_map( 'intval', $ids ); if ( ! $ids ) { return false; } foreach ( $order->get_items( 'line_item' ) as $item ) { if ( ! $item instanceof WC_Order_Item_Product ) { continue; } if ( in_array( (int) $item->get_product_id(), $ids, true ) || in_array( (int) $item->get_variation_id(), $ids, true ) ) { return true; } } return false; } /** * Whether any product (parent for variations) has one of the terms. Categories include subcategories. * * @since 1.0.0 * @param WC_Order $order Order. * @param string $taxonomy Taxonomy. * @param int[] $ids Term IDs. * @return bool */ private function has_terms( WC_Order $order, $taxonomy, array $ids ) { $ids = $this->expand( $taxonomy, $ids ); if ( ! $ids ) { return false; } foreach ( $this->products( $order ) as $product ) { $parent_id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(); if ( array_intersect( array_map( 'intval', wc_get_product_term_ids( $parent_id, $taxonomy ) ), $ids ) ) { return true; } } return false; } /** * Term IDs plus, for hierarchical taxonomies, all their descendants. * * @since 1.0.0 * @param string $taxonomy Taxonomy. * @param int[] $ids IDs. * @return int[] */ private function expand( $taxonomy, array $ids ) { $ids = array_values( array_filter( array_map( 'intval', $ids ) ) ); $key = $taxonomy . ':' . implode( ',', $ids ); if ( ! isset( $this->expanded[ $key ] ) ) { $all = $ids; if ( is_taxonomy_hierarchical( $taxonomy ) ) { foreach ( $ids as $id ) { $children = get_term_children( $id, $taxonomy ); if ( ! is_wp_error( $children ) ) { $all = array_merge( $all, array_map( 'intval', $children ) ); } } } $this->expanded[ $key ] = array_values( array_unique( $all ) ); } return $this->expanded[ $key ]; } }