/** * @static * @param $lang_id * @param $customer_id * @param bool $published * @param bool $includeGeneric * @param bool $inStock * @param JeproshopCartModelCart|null $cart * @return array */ public static function getCustomerCartRules($lang_id, $customer_id, $published = false, $includeGeneric = true, $inStock = false, JeproshopCartModelCart $cart = null) { if (!JeproshopCartRuleModelCartRule::isFeaturePublished()) { return array(); } $db = JFactory::getDBO(); $query = "SELECT * FROM " . $db->quoteName('#_jeproshop_cart_rule') . " AS cart_rule LEFT JOIN " . $db->quoteName('#__jeproshop_cart_rule_lang') . " AS cart_ruler_lang"; $query .= " ON (cart_rule." . $db->quoteName('cart_rule_id') . " = cart_rule_lang." . $db->quoteName('cart_rule_id') . " AND cart_rule_lang." . $db->quoteName('lang_id'); $query .= " = " . (int) $lang_id . ") WHERE ( cart_rule." . $db->quoteName('customer_id') . " = " . (int) $customer_id . " OR cart_rule.group_restriction = 1 "; $query .= ($includeGeneric ? " OR cart_rule." . $db->quoteName('customer_id') . " = 0" : "") . ") AND cart_rule.date_from < '" . date('Y-m-d H:i:s') . "' AND cart_rule.date_to > '"; $query .= date('Y-m-d H:i:s') . "' " . ($published ? "AND cart_rule." . $db->quoteName('published') . " = 1" : "") . ($inStock ? " AND cart_rule." . $db->quoteName('quantity') . " > 0" : ""); $db->setQuery($query); $result = $db->loadObjectList(); // Remove cart rule that does not match the customer groups $customerGroups = JeproshopCustomerModelCustomer::getStaticGroups($customer_id); foreach ($result as $key => $cart_rule) { if ($cart_rule->group_restriction) { $cartRuleGroups = Db::getInstance()->executeS('SELECT id_group FROM ' . _DB_PREFIX_ . 'cart_rule_group WHERE id_cart_rule = ' . (int) $cart_rule['id_cart_rule']); foreach ($cartRuleGroups as $cartRuleGroup) { if (in_array($cartRuleGroup->group_id, $customerGroups)) { continue 2; } } unset($result[$key]); } } foreach ($result as &$cart_rule) { if ($cart_rule->quantity_per_user) { $quantity_used = Order::getDiscountsCustomer((int) $customer_id, (int) $cart_rule->cart_rule_id); if (isset($cart) && isset($cart)) { $quantity_used += $cart->getDiscountsCustomer((int) $cart_rule->cart_rule_id); } $cart_rule->quantity_for_user = $cart_rule->quantity_per_user - $quantity_used; } else { $cart_rule->quantity_for_user = 0; } } unset($cart_rule); foreach ($result as $key => $cart_rule) { if ($cart_rule->shop_restriction) { $cartRuleShops = Db::getInstance()->executeS('SELECT id_shop FROM ' . _DB_PREFIX_ . 'cart_rule_shop WHERE id_cart_rule = ' . (int) $cart_rule['id_cart_rule']); foreach ($cartRuleShops as $cartRuleShop) { if (Shop::isFeatureActive() && $cartRuleShop->shop_id == JeproshopContext::getContext()->shop->shop_id) { continue 2; } } unset($result[$key]); } } if (isset($cart) && isset($cart->cart_id)) { foreach ($result as $key => $cart_rule) { if ($cart_rule->product_restriction) { $cr = new JeproshopCartRuleModelCartRule((int) $cart_rule->cart_rule_id); $restriction = $cr->checkProductRestrictions(JeproshopContext::getContext(), false, false); if ($restriction !== false) { continue; } unset($result[$key]); } } } foreach ($result as $key => $cart_rule) { if ($cart_rule['country_restriction']) { $countries = Db::getInstance()->ExecuteS(' SELECT `id_country` FROM `' . _DB_PREFIX_ . 'address` WHERE `customer_id` = ' . (int) $customer_id . ' AND `deleted` = 0'); if (is_array($countries) && count($countries)) { foreach ($countries as $country) { $id_cart_rule = (bool) Db::getInstance()->getValue(' SELECT crc.id_cart_rule FROM ' . _DB_PREFIX_ . 'cart_rule_country crc WHERE crc.id_cart_rule = ' . (int) $cart_rule['id_cart_rule'] . ' AND crc.id_country = ' . (int) $country['id_country']); if (!$id_cart_rule) { unset($result[$key]); } } } } } // Retro-compatibility with 1.4 discounts foreach ($result as &$cart_rule) { $cart_rule['value'] = 0; $cart_rule['minimal'] = Tools::convertPriceFull($cart_rule['minimum_amount'], new Currency($cart_rule['minimum_amount_currency']), Context::getContext()->currency); $cart_rule['cumulable'] = !$cart_rule['cart_rule_restriction']; $cart_rule['id_discount_type'] = false; if ($cart_rule['free_shipping']) { $cart_rule['id_discount_type'] = Discount::FREE_SHIPPING; } elseif ($cart_rule['reduction_percent'] > 0) { $cart_rule['id_discount_type'] = Discount::PERCENT; $cart_rule['value'] = $cart_rule['reduction_percent']; } elseif ($cart_rule['reduction_amount'] > 0) { $cart_rule->discount_type_id = Discount::AMOUNT; $cart_rule['value'] = $cart_rule['reduction_amount']; } } unset($cart_rule); return $result; }
public function getGroups() { return JeproshopCustomerModelCustomer::getStaticGroups((int) $this->customer_id); }