Beispiel #1
0
    /**
     * Return customer discounts
     *
     * @param integer $id_lang Language ID
     * @param boolean $id_customer Customer ID
     * @return array Discounts
     */
    public static function getCustomerDiscounts($id_lang, $id_customer, $active = false, $includeGenericOnes = true, $stock = false)
    {
        global $cart;
        $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
        SELECT d.*, dtl.`name` AS `type`, dl.`description`
		FROM `' . _DB_PREFIX_ . 'discount` d
		LEFT JOIN `' . _DB_PREFIX_ . 'discount_lang` dl ON (d.`id_discount` = dl.`id_discount` AND dl.`id_lang` = ' . (int) $id_lang . ')
		LEFT JOIN `' . _DB_PREFIX_ . 'discount_type` dt ON dt.`id_discount_type` = d.`id_discount_type`
		LEFT JOIN `' . _DB_PREFIX_ . 'discount_type_lang` dtl ON (dt.`id_discount_type` = dtl.`id_discount_type` AND dtl.`id_lang` = ' . (int) $id_lang . ')
		WHERE (`id_customer` = ' . (int) $id_customer . '
		OR `id_group` IN (SELECT `id_group` FROM `' . _DB_PREFIX_ . 'customer_group` WHERE `id_customer` = ' . (int) $id_customer . ')' . ($includeGenericOnes ? ' OR (`id_customer` = 0 AND `id_group` = 0)' : '') . ')
		' . ($active ? ' AND d.`active` = 1' : '') . '
		' . ($stock ? ' AND d.`quantity` != 0' : ''));
        foreach ($res as &$discount) {
            if ($discount['quantity_per_user']) {
                $quantity_used = Order::getDiscountsCustomer((int) $id_customer, (int) $discount['id_discount']);
                if (isset($cart) and isset($cart->id)) {
                    $quantity_used += $cart->getDiscountsCustomer((int) $discount['id_discount']);
                }
                $discount['quantity_for_user'] = $discount['quantity_per_user'] - $quantity_used;
            } else {
                $discount['quantity_for_user'] = 0;
            }
        }
        return $res;
    }
Beispiel #2
0
    /**
     * @static
     * @param $id_lang
     * @param $id_customer
     * @param bool $active
     * @param bool $includeGeneric
     * @param bool $inStock
     * @param Cart|null $cart
     * @return array
     */
    public static function getCustomerCartRules($id_lang, $id_customer, $active = false, $includeGeneric = true, $inStock = false, Cart $cart = null)
    {
        if (!CartRule::isFeatureActive()) {
            return array();
        }
        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
		SELECT *
		FROM `' . _DB_PREFIX_ . 'cart_rule` cr
		LEFT JOIN `' . _DB_PREFIX_ . 'cart_rule_lang` crl ON (cr.`id_cart_rule` = crl.`id_cart_rule` AND crl.`id_lang` = ' . (int) $id_lang . ')
		WHERE (
			cr.`id_customer` = ' . (int) $id_customer . ' OR cr.group_restriction = 1
			' . ($includeGeneric ? 'OR cr.`id_customer` = 0' : '') . '
		)
		AND cr.date_from < "' . date('Y-m-d H:i:s') . '"
		AND cr.date_to > "' . date('Y-m-d H:i:s') . '"
		' . ($active ? 'AND cr.`active` = 1' : '') . '
		' . ($inStock ? 'AND cr.`quantity` > 0' : ''));
        // Remove cart rule that does not match the customer groups
        $customerGroups = Customer::getGroupsStatic($id_customer);
        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['id_group'], $customerGroups)) {
                        continue 2;
                    }
                }
                unset($result[$key]);
            }
        }
        foreach ($result as &$cart_rule) {
            if ($cart_rule['quantity_per_user']) {
                $quantity_used = Order::getDiscountsCustomer((int) $id_customer, (int) $cart_rule['id_cart_rule']);
                if (isset($cart) && isset($cart->id)) {
                    $quantity_used += $cart->getDiscountsCustomer((int) $cart_rule['id_cart_rule']);
                }
                $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 $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['id_shop'] == Context::getContext()->shop->id) {
                        continue 2;
                    }
                }
                unset($result[$key]);
            }
        }
        // Retrocompatibility with 1.4 discounts
        foreach ($result as &$cart_rule) {
            $cart_rule['value'] = 0;
            $cart_rule['minimal'] = $cart_rule['minimum_amount'];
            $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['id_discount_type'] = Discount::AMOUNT;
                $cart_rule['value'] = $cart_rule['reduction_amount'];
            }
        }
        return $result;
    }
Beispiel #3
0
    /**
     * Check discount validity
     *
     * @return mixed Return a string if an error occurred and false otherwise
     */
    function checkDiscountValidity($discountObj, $discounts, $order_total, $products, $checkCartDiscount = false)
    {
        global $cookie;
        if (!$order_total) {
            return Tools::displayError('Cannot add voucher if order is free.');
        }
        if (!$discountObj->active) {
            return Tools::displayError('This voucher has already been used or is disabled.');
        }
        if (!$discountObj->quantity) {
            return Tools::displayError('This voucher has expired (usage limit attained).');
        }
        if ($discountObj->id_discount_type == 2 and $this->id_currency != $discountObj->id_currency) {
            return Tools::displayError('This voucher can only be used in the following currency:') . '
				' . Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT `name` FROM `' . _DB_PREFIX_ . 'currency` WHERE id_currency = ' . (int) $discountObj->id_currency);
        }
        if ($checkCartDiscount and ($this->getDiscountsCustomer($discountObj->id) >= $discountObj->quantity_per_user or (Order::getDiscountsCustomer((int) $cookie->id_customer, $discountObj->id) + $this->getDiscountsCustomer($discountObj->id) >= $discountObj->quantity_per_user) >= $discountObj->quantity_per_user)) {
            return Tools::displayError('You cannot use this voucher anymore (usage limit attained).');
        }
        if (strtotime($discountObj->date_from) > time()) {
            return Tools::displayError('This voucher is not yet valid');
        }
        if (strtotime($discountObj->date_to) < time()) {
            return Tools::displayError('This voucher has expired.');
        }
        if (sizeof($discounts) >= 1 and $checkCartDiscount) {
            if (!$discountObj->cumulable) {
                return Tools::displayError('This voucher is not valid with other current discounts.');
            }
            foreach ($discounts as $discount) {
                if (!$discount['cumulable']) {
                    return Tools::displayError('Voucher is not valid with other discounts.');
                }
            }
            foreach ($discounts as $discount) {
                if ($discount['id_discount'] == $discountObj->id) {
                    return Tools::displayError('This voucher is already in your cart');
                }
            }
        }
        $groups = Customer::getGroupsStatic($this->id_customer);
        if (($discountObj->id_customer or $discountObj->id_group) and (($this->id_customer != $discountObj->id_customer or $this->id_customer == 0) and !in_array($discountObj->id_group, $groups))) {
            if (!$cookie->isLogged()) {
                return Tools::displayError('You cannot use this voucher.') . ' - ' . Tools::displayError('Please log in.');
            }
            return Tools::displayError('You cannot use this voucher.');
        }
        $onlyProductWithDiscount = true;
        if (!$discountObj->cumulable_reduction) {
            foreach ($products as $product) {
                if (!$product['reduction_applies'] and !$product['on_sale']) {
                    $onlyProductWithDiscount = false;
                }
            }
        }
        if (!$discountObj->cumulable_reduction and $onlyProductWithDiscount) {
            return Tools::displayError('This voucher is not valid for marked or reduced products.');
        }
        $total_cart = 0;
        $categories = Discount::getCategories($discountObj->id);
        $returnErrorNoProductCategory = true;
        foreach ($products as $product) {
            if (count($categories)) {
                if (Product::idIsOnCategoryId($product['id_product'], $categories)) {
                    if (!$discountObj->cumulable_reduction and !$product['reduction_applies'] and !$product['on_sale'] or $discountObj->cumulable_reduction) {
                        $total_cart += $discountObj->include_tax ? $product['total_wt'] : $product['total'];
                    }
                    $returnErrorNoProductCategory = false;
                }
            }
        }
        if ($returnErrorNoProductCategory) {
            return Tools::displayError('This discount does not apply to that product category.');
        }
        if ($total_cart < $discountObj->minimal) {
            return Tools::displayError('The order total is not high enough or this voucher cannot be used with those products.');
        }
        return false;
    }
Beispiel #4
0
 /**
  * Check discount validity
  *
  * @return mixed Return a string if an error occurred and false otherwise
  */
 function checkDiscountValidity($discountObj, $discounts, $order_total, $products, $checkCartDiscount = false)
 {
     global $cookie;
     if (!$order_total) {
         return Tools::displayError('cannot add voucher if order is free');
     }
     if (!$discountObj->active) {
         return Tools::displayError('this voucher has already been used or is disabled');
     }
     if (!$discountObj->quantity) {
         return Tools::displayError('this voucher has expired (usage limit attained)');
     }
     if ($checkCartDiscount and ($this->getDiscountsCustomer($discountObj->id) >= $discountObj->quantity_per_user or (Order::getDiscountsCustomer(intval($cookie->id_customer), $discountObj->id) + $this->getDiscountsCustomer($discountObj->id) >= $discountObj->quantity_per_user) >= $discountObj->quantity_per_user)) {
         return Tools::displayError('you cannot use this voucher anymore (usage limit attained)');
     }
     if (strtotime($discountObj->date_from) > time()) {
         return Tools::displayError('this voucher is not yet valid');
     }
     if (strtotime($discountObj->date_to) < time()) {
         return Tools::displayError('this voucher has expired');
     }
     if (sizeof($discounts) >= 1 and $checkCartDiscount) {
         if (!$discountObj->cumulable) {
             return Tools::displayError('this voucher isn\'t cumulative with other current discounts');
         }
         foreach ($discounts as $discount) {
             if (!$discount['cumulable']) {
                 return Tools::displayError('previous voucher added isn\'t cumulative with other discounts');
             }
         }
     }
     if (is_array($discounts) and in_array($discountObj->id, $discounts)) {
         return Tools::displayError('this voucher is already in your cart');
     }
     if ($discountObj->id_customer and $this->id_customer != $discountObj->id_customer) {
         if (!$cookie->isLogged()) {
             return Tools::displayError('you cannot use this voucher') . ' - ' . Tools::displayError('try to log in if you own it');
         }
         return Tools::displayError('you cannot use this voucher');
     }
     $currentDate = date('Y-m-d');
     if (!$discountObj->cumulable_reduction) {
         foreach ($products as $product) {
             if ((intval($product['reduction_price']) or intval($product['reduction_percent'])) and ($product['reduction_from'] == $product['reduction_to'] or $currentDate >= $product['reduction_from'] and $currentDate <= $product['reduction_to']) or $product['on_sale']) {
                 return Tools::displayError('this voucher isn\'t cumulative on products with reduction or marked as on sale');
             }
         }
     }
     $products = $this->getProducts();
     $total_cart = 0;
     $categories = Discount::getCategories($discountObj->id);
     foreach ($products as $product) {
         if (count($categories)) {
             if (Product::idIsOnCategoryId($product['id_product'], $categories)) {
                 $total_cart += $product['total_wt'];
             }
         }
     }
     if ($total_cart < $discountObj->minimal) {
         return Tools::displayError('the total amount of your order isn\'t high enough or this voucher cannot be used with those products');
     }
     return false;
 }
Beispiel #5
0
    /**
     * Get CartRules for the given Customer
     *
     * @param int       $id_lang            Language ID
     * @param int       $id_customer        Customer ID
     * @param bool      $active             Active vouchers only
     * @param bool      $includeGeneric     Include generic AND highlighted vouchers, regardless of highlight_only setting
     * @param bool      $inStock            Vouchers in stock only
     * @param Cart|null $cart               Cart
     * @param bool      $free_shipping_only Free shipping only
     * @param bool      $highlight_only     Highlighted vouchers only
     * @return array
     * @throws PrestaShopDatabaseException
     */
    public static function getCustomerCartRules($id_lang, $id_customer, $active = false, $includeGeneric = true, $inStock = false, Cart $cart = null, $free_shipping_only = false, $highlight_only = false)
    {
        if (!CartRule::isFeatureActive()) {
            return array();
        }
        $sql_part1 = '* FROM `' . _DB_PREFIX_ . 'cart_rule` cr
				LEFT JOIN `' . _DB_PREFIX_ . 'cart_rule_lang` crl ON (cr.`id_cart_rule` = crl.`id_cart_rule` AND crl.`id_lang` = ' . (int) $id_lang . ')';
        $sql_part2 = ' AND cr.date_from < "' . date('Y-m-d H:i:s') . '"
				AND cr.date_to > "' . date('Y-m-d H:i:s') . '"
				' . ($active ? 'AND cr.`active` = 1' : '') . '
				' . ($inStock ? 'AND cr.`quantity` > 0' : '');
        if ($free_shipping_only) {
            $sql_part2 .= ' AND free_shipping = 1 AND carrier_restriction = 1';
        }
        if ($highlight_only) {
            $sql_part2 .= ' AND highlight = 1 AND code NOT LIKE "' . pSQL(CartRule::BO_ORDER_CODE_PREFIX) . '%"';
        }
        $sql = '(SELECT SQL_NO_CACHE ' . $sql_part1 . ' WHERE cr.`id_customer` = ' . (int) $id_customer . ' ' . $sql_part2 . ')';
        $sql .= ' UNION (SELECT ' . $sql_part1 . ' WHERE cr.`group_restriction` = 1 ' . $sql_part2 . ')';
        if ($includeGeneric && (int) $id_customer != 0) {
            $sql .= ' UNION (SELECT ' . $sql_part1 . ' WHERE cr.`id_customer` = 0 ' . $sql_part2 . ')';
        }
        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql, true, false);
        if (empty($result)) {
            return array();
        }
        // Remove cart rule that does not match the customer groups
        $customerGroups = Customer::getGroupsStatic($id_customer);
        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['id_group'], $customerGroups)) {
                        continue 2;
                    }
                }
                unset($result[$key]);
            }
        }
        foreach ($result as &$cart_rule) {
            if ($cart_rule['quantity_per_user']) {
                $quantity_used = Order::getDiscountsCustomer((int) $id_customer, (int) $cart_rule['id_cart_rule']);
                if (isset($cart) && isset($cart->id)) {
                    $quantity_used += $cart->getDiscountsCustomer((int) $cart_rule['id_cart_rule']);
                }
                $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['id_shop'] == Context::getContext()->shop->id) {
                        continue 2;
                    }
                }
                unset($result[$key]);
            }
        }
        if (isset($cart) && isset($cart->id)) {
            foreach ($result as $key => $cart_rule) {
                if ($cart_rule['product_restriction']) {
                    $cr = new CartRule((int) $cart_rule['id_cart_rule']);
                    $r = $cr->checkProductRestrictions(Context::getContext(), false, false);
                    if ($r !== false) {
                        continue;
                    }
                    unset($result[$key]);
                }
            }
        }
        $result_bak = $result;
        $result = array();
        $country_restriction = false;
        foreach ($result_bak as $key => $cart_rule) {
            if ($cart_rule['country_restriction']) {
                $country_restriction = true;
                $countries = Db::getInstance()->ExecuteS('
                    SELECT `id_country`
                    FROM `' . _DB_PREFIX_ . 'address`
                    WHERE `id_customer` = ' . (int) $id_customer . '
                    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) {
                            $result[] = $result_bak[$key];
                            break;
                        }
                    }
                }
            } else {
                $result[] = $result_bak[$key];
            }
        }
        if (!$country_restriction) {
            $result = $result_bak;
        }
        // Retrocompatibility with 1.4 discounts
        // TODO: remove? We're at 1.7 now
        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['id_discount_type'] = Discount::AMOUNT;
                $cart_rule['value'] = $cart_rule['reduction_amount'];
            }
        }
        unset($cart_rule);
        return $result;
    }
Beispiel #6
0
 /**
  * Check discount validity
  *
  * @return mixed Return a string if an error occurred and false otherwise
  */
 function checkDiscountValidity($discountObj, $discounts, $order_total, $products, $checkCartDiscount = true)
 {
     global $cookie;
     // if(strpos('B1G1', $discountObj->name) == 0)
     //   return false;
     if (!$order_total) {
         return Tools::displayError('Cannot add voucher if order is free.');
     }
     if (!$discountObj->active) {
         return Tools::displayError('This voucher has already been used or is disabled.');
     }
     if (!$discountObj->quantity) {
         return Tools::displayError('This voucher has expired (usage limit attained).');
     }
     //if (($discountObj->id_discount_type == 2 || $discountObj->id_discount_type == 4) AND $this->id_currency != $discountObj->id_currency)
     //return Tools::displayError('This voucher can only be used in the following currency:').'
     //'.Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT `name` FROM `'._DB_PREFIX_.'currency` WHERE id_currency = '.(int)$discountObj->id_currency);
     if ($checkCartDiscount and ($this->getDiscountsCustomer($discountObj->id) >= $discountObj->quantity_per_user or (Order::getDiscountsCustomer((int) $cookie->id_customer, $discountObj->id) + $this->getDiscountsCustomer($discountObj->id) >= $discountObj->quantity_per_user) >= $discountObj->quantity_per_user)) {
         return Tools::displayError('You cannot use this voucher anymore (usage limit attained).');
     }
     /*if ((
           $this->getDiscountsGroupCustomer($discountObj->discount_group) >= $discountObj->quantity_per_user
           OR (Order::getDiscountsGroupCustomer((int)($cookie->id_customer), $discountObj->discount_group) + $this->getDiscountsGroupCustomer($discountObj->discount_group) >= $discountObj->quantity_per_user) >= $discountObj->quantity_per_user
           )
       )
       return Tools::displayError('Vouchers from this promotion can only be used once by a customer.');*/
     if (strtotime($discountObj->date_from) > time()) {
         return Tools::displayError('This voucher is not yet valid');
     }
     if (strtotime($discountObj->date_to) < time()) {
         return Tools::displayError('This voucher has expired.');
     }
     if (sizeof($discounts) >= 1 and $checkCartDiscount) {
         if (!$discountObj->cumulable) {
             return Tools::displayError('This voucher is not valid with other current discounts.');
         }
         foreach ($discounts as $discount) {
             if (!$discount['cumulable']) {
                 return Tools::displayError('Voucher is not valid with other discounts.');
             }
         }
         foreach ($discounts as $discount) {
             if ($discount['id_discount'] == $discountObj->id) {
                 return Tools::displayError('This voucher is already in your cart');
             }
         }
     }
     $groups = Customer::getGroupsStatic($this->id_customer);
     if (($discountObj->id_customer or $discountObj->id_group) and ($this->id_customer != $discountObj->id_customer and !in_array($discountObj->id_group, $groups))) {
         if (!$cookie->isLogged()) {
             return Tools::displayError('You cannot use this voucher.') . ' - ' . Tools::displayError('Please log in.');
         }
         return Tools::displayError('You cannot use this voucher.');
     }
     $currentDate = date('Y-m-d');
     $onlyProductWithDiscount = true;
     if (!$discountObj->cumulable_reduction) {
         foreach ($products as $product) {
             if (!$product['reduction_applies'] and !$product['on_sale']) {
                 $onlyProductWithDiscount = false;
             }
         }
     }
     if (!$discountObj->cumulable_reduction and $onlyProductWithDiscount) {
         return Tools::displayError('This voucher is not valid for marked or reduced products.');
     }
     $total_cart = 0;
     $categories = Discount::getCategories($discountObj->id);
     $returnErrorNoProductCategory = true;
     foreach ($products as $product) {
         if (count($categories)) {
             if (Product::idIsOnCategoryId($product['id_product'], $categories)) {
                 if (!$discountObj->cumulable_reduction and !$product['reduction_applies'] and !$product['on_sale'] or $discountObj->cumulable_reduction) {
                     $total_cart += $product['total_wt'];
                 }
                 $returnErrorNoProductCategory = false;
             }
         }
     }
     if (isset($discountObj->brands) && isset($discountObj->min_cart) && $discountObj->min_cart > 0) {
         $cartTotalForBrands = $this->getCartTotalForBrands($discountObj->brands);
         if ($cartTotalForBrands < $discountObj->min_cart) {
             return Tools::displayError('This discount applies to products worth atleast ' . $discountObj->min_cart . ' of specific brands.');
         }
     }
     if ($returnErrorNoProductCategory) {
         return Tools::displayError('This discount does not apply to that product category.');
     }
     //conver total to USD
     $total_cart = Tools::convertPrice($total_cart, $this->id_currency, false);
     if ($total_cart < $discountObj->minimal) {
         return Tools::displayError('The order total is not high enough or this voucher cannot be used with those products.');
     }
     return false;
 }
Beispiel #7
0
    /**
     * @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;
    }
Beispiel #8
0
    /**
     * Check discount validity
     *
     * @return mixed Return a string if an error occurred and false otherwise
     */
    function checkDiscountValidity($discountObj, $discounts, $order_total, $products, $checkCartDiscount = false)
    {
        global $cookie;
        if (!$order_total) {
            return Tools::displayError('cannot add voucher if order is free');
        }
        if (!$discountObj->active) {
            return Tools::displayError('this voucher has already been used or is disabled');
        }
        if (!$discountObj->quantity) {
            return Tools::displayError('this voucher has expired (usage limit attained)');
        }
        if ($discountObj->id_discount_type == 2 and $this->id_currency != $discountObj->id_currency) {
            return Tools::displayError('this voucher can only be used in the following currency:') . '
				' . Db::getInstance()->getValue('SELECT `name` FROM `' . _DB_PREFIX_ . 'currency` WHERE id_currency = ' . (int) $discountObj->id_currency);
        }
        if ($checkCartDiscount and ($this->getDiscountsCustomer($discountObj->id) >= $discountObj->quantity_per_user or (Order::getDiscountsCustomer(intval($cookie->id_customer), $discountObj->id) + $this->getDiscountsCustomer($discountObj->id) >= $discountObj->quantity_per_user) >= $discountObj->quantity_per_user)) {
            return Tools::displayError('you cannot use this voucher anymore (usage limit attained)');
        }
        if (strtotime($discountObj->date_from) > time()) {
            return Tools::displayError('this voucher is not yet valid');
        }
        if (strtotime($discountObj->date_to) < time()) {
            return Tools::displayError('this voucher has expired');
        }
        if (sizeof($discounts) >= 1 and $checkCartDiscount) {
            if (!$discountObj->cumulable) {
                return Tools::displayError('this voucher isn\'t cumulative with other current discounts');
            }
            foreach ($discounts as $discount) {
                if (!$discount['cumulable']) {
                    return Tools::displayError('previous voucher added isn\'t cumulative with other discounts');
                }
            }
        }
        if (is_array($discounts) and in_array($discountObj->id, $discounts)) {
            return Tools::displayError('this voucher is already in your cart');
        }
        if ($discountObj->id_customer and $this->id_customer != $discountObj->id_customer) {
            if (!$cookie->isLogged()) {
                return Tools::displayError('you cannot use this voucher') . ' - ' . Tools::displayError('try to log in if you own it');
            }
            return Tools::displayError('you cannot use this voucher');
        }
        $currentDate = date('Y-m-d');
        $onlyProductWithDiscount = true;
        if (!$discountObj->cumulable_reduction) {
            foreach ($products as $product) {
                if (!intval($product['reduction_price']) and !intval($product['reduction_percent']) and !$product['on_sale']) {
                    $onlyProductWithDiscount = false;
                }
            }
        }
        if (!$discountObj->cumulable_reduction and $onlyProductWithDiscount) {
            return Tools::displayError('this voucher isn\'t cumulative on products with reduction or marked as on sale');
        }
        $total_cart = 0;
        $categories = Discount::getCategories($discountObj->id);
        $returnErrorNoProductCategory = true;
        foreach ($products as $product) {
            if (count($categories)) {
                if (Product::idIsOnCategoryId($product['id_product'], $categories)) {
                    if (!$discountObj->cumulable_reduction and !intval($product['reduction_price']) and !intval($product['reduction_percent']) and !$product['on_sale'] or $discountObj->cumulable_reduction) {
                        $total_cart += $product['total_wt'];
                    }
                    $returnErrorNoProductCategory = false;
                }
            }
        }
        if ($returnErrorNoProductCategory) {
            return Tools::displayError('this discount isn\'t applicable to that product category');
        }
        if ($total_cart < $discountObj->minimal) {
            return Tools::displayError('the total amount of your order isn\'t high enough or this voucher cannot be used with those products');
        }
        return false;
    }