305 lines
19 KiB
PHP
305 lines
19 KiB
PHP
<?php
|
|
/*
|
|
* 2007-2011 PrestaShop
|
|
*
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to the Open Software License (OSL 3.0)
|
|
* that is bundled with this package in the file LICENSE.txt.
|
|
* It is also available through the world-wide-web at this URL:
|
|
* http://opensource.org/licenses/osl-3.0.php
|
|
* If you did not receive a copy of the license and are unable to
|
|
* obtain it through the world-wide-web, please send an email
|
|
* to license@prestashop.com so we can send you a copy immediately.
|
|
*
|
|
* DISCLAIMER
|
|
*
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
|
* versions in the future. If you wish to customize PrestaShop for your
|
|
* needs please refer to http://www.prestashop.com for more information.
|
|
*
|
|
* @author PrestaShop SA <contact@prestashop.com>
|
|
* @copyright 2007-2011 PrestaShop SA
|
|
* @version Release: $Revision: 10176 $
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
|
* International Registered Trademark & Property of PrestaShop SA
|
|
*/
|
|
|
|
include_once dirname(__FILE__).'/../../modules/privatesales/Sale.php';
|
|
class CartController extends CartControllerCore
|
|
{
|
|
public function preProcess()
|
|
{
|
|
FrontController::preProcess();
|
|
|
|
$orderTotal = self::$cart->getOrderTotal(true, Cart::ONLY_PRODUCTS);
|
|
|
|
$this->cartDiscounts = self::$cart->getDiscounts();
|
|
foreach ($this->cartDiscounts AS $k => $this->cartDiscount)
|
|
if ($error = self::$cart->checkDiscountValidity(new Discount((int)($this->cartDiscount['id_discount'])), $this->cartDiscounts, $orderTotal, self::$cart->getProducts()))
|
|
self::$cart->deleteDiscount((int)($this->cartDiscount['id_discount']));
|
|
|
|
$add = Tools::getIsset('add') ? 1 : 0;
|
|
$delete = Tools::getIsset('delete') ? 1 : 0;
|
|
$removeAll = Tools::getIsset('removeall') ? 1 : 0;
|
|
|
|
if (Configuration::get('PS_TOKEN_ENABLE') == 1
|
|
&& strcasecmp(Tools::getToken(false), strval(Tools::getValue('token')))
|
|
&& self::$cookie->isLogged() === true)
|
|
$this->errors[] = Tools::displayError('Invalid token');
|
|
// Update the cart ONLY if $this->cookies are available, in order to avoid ghost carts created by bots
|
|
if (($add OR Tools::getIsset('update') OR $delete) AND isset($_COOKIE[self::$cookie->getName()]))
|
|
{
|
|
//get the values
|
|
$idProduct = (int)(Tools::getValue('id_product', NULL));
|
|
$idProductAttribute = (int)(Tools::getValue('id_product_attribute', Tools::getValue('ipa')));
|
|
$customizationId = (int)(Tools::getValue('id_customization', 0));
|
|
$qty = (int)(abs(Tools::getValue('qty', 1)));
|
|
|
|
// @Adding Antadis - Prevent multi sales
|
|
if ($add){
|
|
$current_sale = array();
|
|
foreach(self::$cart->getProducts() as $product) {
|
|
/*$current_sale = Db::getInstance()->getRow('
|
|
SELECT ps.`delivery_delay`, ps.`id_sale`
|
|
FROM `'._DB_PREFIX_.'product_ps_cache` psc
|
|
LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_sale` = psc.`id_sale`)
|
|
WHERE psc.`id_product` = '.(int) $product['id_product']
|
|
);*/
|
|
|
|
$sale = Sale::getSaleFromCategory((int)$product['id_category_default'], false);
|
|
$current_sale = array(
|
|
'id_sale'=> (int) $sale->id,
|
|
'delivery_delay'=> (int) $sale->delivery_delay,
|
|
'shipping' => Sale::getShippingSale((int)$sale->id)
|
|
);
|
|
// Only one delivery id per cart, so we can break now
|
|
break;
|
|
}
|
|
/*$adding_sale = Db::getInstance()->getRow('
|
|
SELECT ps.`delivery_delay`, ps.`id_sale`
|
|
FROM `'._DB_PREFIX_.'product_ps_cache` psc
|
|
LEFT JOIN `'._DB_PREFIX_.'privatesale` ps ON (ps.`id_sale` = psc.`id_sale`)
|
|
WHERE psc.`id_product` = '.(int) $idProduct
|
|
);*/
|
|
$current_product = new Product((int) $idProduct);
|
|
$sale2 = Sale::getSaleFromCategory($current_product->id_category_default, false);
|
|
$adding_sale = array(
|
|
'id_sale'=> (int) $sale2->id,
|
|
'delivery_delay'=> (int) $sale2->delivery_delay,
|
|
'shipping' => Sale::getShippingSale((int)$sale2->id)
|
|
);
|
|
|
|
//die('{"hasError" : true,"popup_error_cart": true, "errors" : [" '.implode(' , ',$current_sale).' - '.implode(' , ',$adding_sale).'"]}');
|
|
|
|
if (!empty($current_sale)) {
|
|
global $smarty;
|
|
// Only classic/Noel delivery sales can be added together or product of the same sale or philea sales with same delivery delay
|
|
if ((
|
|
((int)$current_sale['delivery_delay'] != 1 && (int)$current_sale['delivery_delay'] != 5)
|
|
&& (int)$adding_sale['id_sale'] != (int)$current_sale['id_sale']
|
|
&& (
|
|
(
|
|
((int)$current_sale['shipping'] == 1 && (int)$current_sale['shipping'] == (int)$adding_sale['shipping'])
|
|
&& (int)$adding_sale['delivery_delay'] != (int)$current_sale['delivery_delay']
|
|
)
|
|
|| ((int)$current_sale['shipping'] != 1 && (int)$adding_sale['shipping'] == 1)
|
|
|| ((int)$current_sale['shipping'] == 1 && (int)$adding_sale['shipping'] != 1)
|
|
|| ((int)$current_sale['shipping'] != 1 && (int)$adding_sale['shipping'] != 1)
|
|
)
|
|
) || (
|
|
((int)$current_sale['delivery_delay'] != 1 && (int)$adding_sale['delivery_delay'] == 1)
|
|
|| (
|
|
(int)$current_sale['delivery_delay'] == 1
|
|
&& (int)$adding_sale['delivery_delay'] == 1
|
|
&& (int)$current_sale['shipping'] != (int)$adding_sale['shipping']
|
|
)
|
|
) || (
|
|
((int)$current_sale['delivery_delay'] != 5 && (int)$adding_sale['delivery_delay'] == 5)
|
|
|| ((int)$current_sale['delivery_delay'] == 5 && (int)$adding_sale['delivery_delay'] != 5)
|
|
)
|
|
) {
|
|
// Product in cart comes from special delivery sale - only products from the same sale can be added
|
|
if (Tools::getValue('ajax') == 'true') {
|
|
die('{"hasError" : true,"popup_error_cart": true, "errors" : ["'.Tools::displayError('Sorry your cart already contains products from a sale shipped within a special period. To continue, please confirm your order or empty your cart.', false).'"]}');
|
|
} else {
|
|
$this->errors[] = Tools::displayError('Sorry your cart already contains products from a sale shipped within a special period. To continue, please confirm your order or empty your cart.', false);
|
|
}
|
|
} elseif ((int)$current_sale['delivery_delay'] == 1 && (int)$adding_sale['delivery_delay'] != 1) {
|
|
// Product in cart comes from classic delivery sale - only products from classic delivery sale can be added
|
|
if (Tools::getValue('ajax') == 'true') {
|
|
die('{"hasError" : true,"popup_error_cart": true, "errors" : ["'.Tools::displayError('Sorry your cart already contains products from a sale shipped within 3 weeks. To continue, please confirm your order or empty your cart.', false).'"]}');
|
|
} else {
|
|
$this->errors[] = Tools::displayError('Sorry your cart already contains products from a sale shipped within 3 weeks. To continue, please confirm your order or empty your cart.', false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// @End adding Antadis
|
|
|
|
if ($qty == 0)
|
|
$this->errors[] = Tools::displayError('Null quantity');
|
|
elseif (!$idProduct)
|
|
$this->errors[] = Tools::displayError('Product not found');
|
|
else
|
|
{
|
|
$producToAdd = new Product((int)($idProduct), true, (int)(self::$cookie->id_lang));
|
|
if ((!$producToAdd->id OR !$producToAdd->active) AND !$delete)
|
|
if (Tools::getValue('ajax') == 'true')
|
|
die('{"hasError" : true, "errors" : ["'.Tools::displayError('Product is no longer available.', false).'"]}');
|
|
else
|
|
$this->errors[] = Tools::displayError('Product is no longer available.', false);
|
|
else
|
|
{
|
|
/* Check the quantity availability */
|
|
if ($idProductAttribute AND is_numeric($idProductAttribute))
|
|
{
|
|
if (!$delete AND !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) AND !Attribute::checkAttributeQty((int)$idProductAttribute, (int)$qty))
|
|
if (Tools::getValue('ajax') == 'true')
|
|
die('{"hasError" : true, "errors" : ["'.Tools::displayError('There is not enough product in stock.', false).'"]}');
|
|
else
|
|
$this->errors[] = Tools::displayError('There is not enough product in stock.');
|
|
}
|
|
elseif ($producToAdd->hasAttributes() AND !$delete)
|
|
{
|
|
$idProductAttribute = Product::getDefaultAttribute((int)$producToAdd->id, (int)$producToAdd->out_of_stock == 2 ? !(int)Configuration::get('PS_ORDER_OUT_OF_STOCK') : !(int)$producToAdd->out_of_stock);
|
|
if (!$idProductAttribute)
|
|
Tools::redirectAdmin($link->getProductLink($producToAdd));
|
|
elseif (!$delete AND !$producToAdd->isAvailableWhenOutOfStock($producToAdd->out_of_stock) AND !Attribute::checkAttributeQty((int)$idProductAttribute, (int)$qty))
|
|
if (Tools::getValue('ajax') == 'true')
|
|
die('{"hasError" : true, "errors" : ["'.Tools::displayError('There is not enough product in stock.', false).'"]}');
|
|
else
|
|
$this->errors[] = Tools::displayError('There is not enough product in stock.');
|
|
}
|
|
elseif (!$delete AND !$producToAdd->checkQty((int)$qty))
|
|
if (Tools::getValue('ajax') == 'true')
|
|
die('{"hasError" : true, "errors" : ["'.Tools::displayError('There is not enough product in stock.').'"]}');
|
|
else
|
|
$this->errors[] = Tools::displayError('There is not enough product in stock.');
|
|
/* Check vouchers compatibility */
|
|
if ($add AND (($producToAdd->specificPrice AND (float)($producToAdd->specificPrice['reduction'])) OR $producToAdd->on_sale))
|
|
{
|
|
$discounts = self::$cart->getDiscounts();
|
|
$hasUndiscountedProduct = null;
|
|
foreach($discounts as $discount)
|
|
{
|
|
if (is_null($hasUndiscountedProduct))
|
|
{
|
|
$hasUndiscountedProduct = false;
|
|
foreach(self::$cart->getProducts() as $product)
|
|
if ($product['reduction_applies'] === false)
|
|
{
|
|
$hasUndiscountedProduct = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!$discount['cumulable_reduction'] && ($discount['id_discount_type'] != 1 || !$hasUndiscountedProduct))
|
|
if (Tools::getValue('ajax') == 'true')
|
|
die('{"hasError" : true, "errors" : ["'.Tools::displayError('Cannot add this product because current voucher does not allow additional discounts.').'"]}');
|
|
else
|
|
$this->errors[] = Tools::displayError('Cannot add this product because current voucher does not allow additional discounts.');
|
|
|
|
}
|
|
}
|
|
if (!sizeof($this->errors))
|
|
{
|
|
if ($add AND $qty >= 0)
|
|
{
|
|
/* Product addition to the cart */
|
|
if (!isset(self::$cart->id) OR !self::$cart->id)
|
|
{
|
|
self::$cart->add();
|
|
if (self::$cart->id)
|
|
self::$cookie->id_cart = (int)(self::$cart->id);
|
|
}
|
|
if ($add AND !$producToAdd->hasAllRequiredCustomizableFields() AND !$customizationId)
|
|
$this->errors[] = Tools::displayError('Please fill in all required fields, then save the customization.');
|
|
if (!sizeof($this->errors))
|
|
{
|
|
$updateQuantity = self::$cart->updateQty((int)($qty), (int)($idProduct), (int)($idProductAttribute), $customizationId, Tools::getValue('op', 'up'));
|
|
|
|
if ($updateQuantity < 0)
|
|
{
|
|
/* if product has attribute, minimal quantity is set with minimal quantity of attribute*/
|
|
if ((int)$idProductAttribute)
|
|
$minimal_quantity = Attribute::getAttributeMinimalQty((int)$idProductAttribute);
|
|
else
|
|
$minimal_quantity = $producToAdd->minimal_quantity;
|
|
if (Tools::getValue('ajax') == 'true')
|
|
die('{"hasError" : true, "errors" : ["'.Tools::displayError('You must add', false).' '.$minimal_quantity.' '.Tools::displayError('Minimum quantity', false).'"]}');
|
|
else
|
|
$this->errors[] = Tools::displayError('You must add').' '.$minimal_quantity.' '.Tools::displayError('Minimum quantity')
|
|
.((isset($_SERVER['HTTP_REFERER']) AND basename($_SERVER['HTTP_REFERER']) == 'order.php' OR (!Tools::isSubmit('ajax') AND substr(basename($_SERVER['REQUEST_URI']),0, strlen('cart.php')) == 'cart.php')) ? ('<script language="javascript">setTimeout("history.back()",5000);</script><br />- '.
|
|
Tools::displayError('You will be redirected to your cart in a few seconds.')) : '');
|
|
}
|
|
elseif (!$updateQuantity)
|
|
{
|
|
if (Tools::getValue('ajax') == 'true')
|
|
die('{"hasError" : true, "errors" : ["'.Tools::displayError('You already have the maximum quantity available for this product.', false).'"]}');
|
|
else
|
|
$this->errors[] = Tools::displayError('You already have the maximum quantity available for this product.')
|
|
.((isset($_SERVER['HTTP_REFERER']) AND basename($_SERVER['HTTP_REFERER']) == 'order.php' OR (!Tools::isSubmit('ajax') AND substr(basename($_SERVER['REQUEST_URI']),0, strlen('cart.php')) == 'cart.php')) ? ('<script language="javascript">setTimeout("history.back()",5000);</script><br />- '.
|
|
Tools::displayError('You will be redirected to your cart in a few seconds.')) : '');
|
|
}
|
|
}
|
|
}
|
|
elseif ($delete)
|
|
{
|
|
if (self::$cart->deleteProduct((int)($idProduct), (int)($idProductAttribute), (int)($customizationId)))
|
|
if (!Cart::getNbProducts((int)(self::$cart->id)))
|
|
{
|
|
self::$cart->id_carrier = 0;
|
|
self::$cart->gift = 0;
|
|
self::$cart->gift_message = '';
|
|
self::$cart->update();
|
|
}
|
|
}
|
|
}
|
|
$discounts = self::$cart->getDiscounts();
|
|
foreach($discounts AS $discount)
|
|
{
|
|
$discountObj = new Discount((int)($discount['id_discount']), (int)(self::$cookie->id_lang));
|
|
if ($error = self::$cart->checkDiscountValidity($discountObj, $discounts, self::$cart->getOrderTotal(true, Cart::ONLY_PRODUCTS), self::$cart->getProducts()))
|
|
{
|
|
self::$cart->deleteDiscount((int)($discount['id_discount']));
|
|
self::$cart->update();
|
|
$errors[] = $error;
|
|
}
|
|
}
|
|
if (!sizeof($this->errors))
|
|
{
|
|
$queryString = Tools::safeOutput(Tools::getValue('query', NULL));
|
|
if ($queryString AND !Configuration::get('PS_CART_REDIRECT'))
|
|
Tools::redirect('search.php?search='.$queryString);
|
|
if (isset($_SERVER['HTTP_REFERER']))
|
|
{
|
|
// Redirect to previous page
|
|
preg_match('!http(s?)://(.*)/(.*)!', $_SERVER['HTTP_REFERER'], $regs);
|
|
if (isset($regs[3]) AND !Configuration::get('PS_CART_REDIRECT') AND Tools::getValue('ajax') != 'true')
|
|
Tools::redirect($_SERVER['HTTP_REFERER']);
|
|
}
|
|
}
|
|
}
|
|
if (Tools::getValue('ajax') != 'true' AND !sizeof($this->errors))
|
|
Tools::redirect('order.php?'.(isset($idProduct) ? 'ipa='.(int)($idProduct) : ''));
|
|
|
|
}
|
|
}
|
|
|
|
// @Adding Antadis - remove whole cart
|
|
if ($removeAll) {
|
|
foreach(self::$cart->getProducts() as $product) {
|
|
if (self::$cart->deleteProduct((int)($product['id_product']), (int)($product['id_product_attribute']), (int)($product['id_customization']))) {
|
|
if (!Cart::getNbProducts((int)(self::$cart->id)))
|
|
{
|
|
self::$cart->id_carrier = 0;
|
|
self::$cart->gift = 0;
|
|
self::$cart->gift_message = '';
|
|
self::$cart->update();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// @End adding Antadis
|
|
}
|
|
} |