/** * Check if conditions of a rule are matched * * @access public * @param array $rule * @return bool */ public function rule_conditions_match($rule) { // Customers if ($rule['user_method'] == 'roles_include') { if (count(array_intersect(RP_WCDPD::current_user_roles(), $rule['roles'])) < 1) { return false; } } if ($rule['user_method'] == 'roles_exclude') { if (count(array_intersect(RP_WCDPD::current_user_roles(), $rule['roles'])) > 0) { return false; } } if ($rule['user_method'] == 'capabilities_include') { if (count(array_intersect(RP_WCDPD::current_user_capabilities(), $rule['capabilities'])) < 1) { return false; } } if ($rule['user_method'] == 'capabilities_exclude') { if (count(array_intersect(RP_WCDPD::current_user_capabilities(), $rule['capabilities'])) > 0) { return false; } } if ($rule['user_method'] == 'users_include') { if (!in_array(get_current_user_id(), $rule['users'])) { return false; } } if ($rule['user_method'] == 'users_exclude') { if (in_array(get_current_user_id(), $rule['users'])) { return false; } } // Get counts of items/products/categories depending on "Quantities based on" setting $quantities = $this->count_in_cart($rule); // Check if quantity discount quantities are matched if ($rule['method'] == 'quantity') { $is_ok = false; foreach ($quantities as $quantity_key => $quantity) { foreach ($rule['pricing'] as $row_key => $row) { if (!isset($row['added']) && $quantity >= $row['min'] && $quantity <= $row['max']) { $is_ok = true; break; } } } if (!$is_ok) { return false; } } else { $is_ok = false; foreach ($quantities as $quantity_key => $quantity) { if ($quantity >= $rule['special_purchase']) { $is_ok = true; break; } } if (!$is_ok) { return false; } } return true; }
/** * Validate, sanitize, check conditions and return single discount rule to be applied * * @access public * @param float $subtotal * @param array $discount_rule * @return mixed */ public function validate_rule($subtotal, $discount_rule) { // Valid from if (isset($discount_rule['valid_from']) && !empty($discount_rule['valid_from']) && strtotime($discount_rule['valid_from'] . ' 00:00:00') > time()) { return false; } // Valid until if (isset($discount_rule['valid_until']) && !empty($discount_rule['valid_until']) && strtotime($discount_rule['valid_until'] . ' 23:59:59') < time()) { return false; } // Only if pricing not adjusted if (isset($discount_rule['only_if_pricing_not_adjusted']) && $discount_rule['only_if_pricing_not_adjusted'] && !empty($this->pricing->applied)) { return false; } // CONDITIONS foreach ($discount_rule['conditions'] as $condition_key => $condition) { switch ($condition['key']) { /** * Total at least */ case 'subtotal_bottom': if ($subtotal < $condition['value']) { return false; } break; /** * Total less than */ /** * Total less than */ case 'subtotal_top': if ($subtotal >= $condition['value']) { return false; } break; /** * At least one product in cart */ /** * At least one product in cart */ case 'products': if (!$this->product_in_cart($condition['products'])) { return false; } break; /** * None of selected products in cart */ /** * None of selected products in cart */ case 'products_not': if ($this->product_in_cart($condition['products'])) { return false; } break; /** * At least one category in cart */ /** * At least one category in cart */ case 'categories': if (!$this->category_in_cart($condition['categories'])) { return false; } break; /** * None of selected categories in cart */ /** * None of selected categories in cart */ case 'categories_not': if ($this->category_in_cart($condition['categories'])) { return false; } break; /** * Count of cart items at least */ /** * Count of cart items at least */ case 'item_count_bottom': if (count($this->items) < $condition['value']) { return false; } break; /** * Count of cart items less than */ /** * Count of cart items less than */ case 'item_count_top': if (count($this->items) >= $condition['value']) { return false; } break; /** * Sum of item quantities at least */ /** * Sum of item quantities at least */ case 'quantity_bottom': if ($this->total_cart_quantity() < $condition['value']) { return false; } break; /** * Sum of item quantities less than */ /** * Sum of item quantities less than */ case 'quantity_top': if ($this->total_cart_quantity() >= $condition['value']) { return false; } break; /** * Specific users */ /** * Specific users */ case 'users': if (get_current_user_id() == 0 || !in_array(get_current_user_id(), $condition['users'])) { return false; } break; /** * Specific user roles */ /** * Specific user roles */ case 'roles': if (get_current_user_id() == 0 || count(array_intersect(RP_WCDPD::current_user_roles(), $condition['roles'])) == 0) { return false; } break; /** * Specific user capabilities */ /** * Specific user capabilities */ case 'capabilities': if (get_current_user_id() == 0 || count(array_intersect(RP_WCDPD::current_user_capabilities(), $condition['capabilities'])) == 0) { return false; } break; /** * Count of orders to date */ /** * Count of orders to date */ case 'history_count': if (get_current_user_id() == 0 || $this->customer_order_count(get_current_user_id()) < $condition['value']) { return false; } break; /** * Total amount of orders to date */ /** * Total amount of orders to date */ case 'history_amount': if (get_current_user_id() == 0 || $this->customer_value(get_current_user_id()) < $condition['value']) { return false; } break; /** * Shipping country */ /** * Shipping country */ case 'shipping_countries': if (get_current_user_id() == 0) { return false; } else { $user_meta = get_user_meta(get_current_user_id()); if (!$user_meta || !isset($user_meta['shipping_country']) || empty($user_meta['shipping_country']) || !in_array($user_meta['shipping_country'][0], $condition['shipping_countries'])) { return false; } } break; default: break; } } return true; }