// Context part 18

This commit is contained in:
tDidierjean
2011-07-25 15:19:39 +00:00
parent 9b0b588b09
commit 2c4f27f1be
41 changed files with 611 additions and 638 deletions
+33 -35
View File
@@ -31,7 +31,6 @@ class CartControllerCore extends FrontController
{
$this->init();
$this->preProcess();
$context = Context::getContext();
if (Tools::getValue('ajax') == 'true')
{
@@ -39,16 +38,16 @@ class CartControllerCore extends FrontController
{
if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 1)
{
if (Validate::isLoadedObject($context->customer))
$groups = $context->customer->getGroups();
if (Validate::isLoadedObject($this->context->customer))
$groups = $this->context->customer->getGroups();
else
$groups = array(1);
if ($context->cart->id_address_delivery)
$deliveryAddress = new Address($context->cart->id_address_delivery);
if ($this->context->cart->id_address_delivery)
$deliveryAddress = new Address($this->context->cart->id_address_delivery);
$result = array('carriers' => Carrier::getCarriersForOrder(Country::getIdZone((isset($deliveryAddress) AND (int)$deliveryAddress->id) ? (int)$deliveryAddress->id_country : (int)Configuration::get('PS_COUNTRY_DEFAULT')), $groups));
}
$result['summary'] = $context->cart->getSummaryDetails();
$result['customizedDatas'] = Product::getAllCustomizedDatas($context->cart->id, null, true);
$result['summary'] = $this->context->cart->getSummaryDetails();
$result['customizedDatas'] = Product::getAllCustomizedDatas($this->context->cart->id, null, true);
$result['HOOK_SHOPPING_CART'] = Module::hookExec('shoppingCart', $result['summary']);
$result['HOOK_SHOPPING_CART_EXTRA'] = Module::hookExec('shoppingCartExtra', $result['summary']);
die(Tools::jsonEncode($result));
@@ -74,23 +73,22 @@ class CartControllerCore extends FrontController
public function preProcess()
{
parent::preProcess();
$context = Context::getContext();
$orderTotal = $context->cart->getOrderTotal(true, Cart::ONLY_PRODUCTS);
$this->cartDiscounts = $context->cart->getDiscounts();
$orderTotal = $this->context->cart->getOrderTotal(true, Cart::ONLY_PRODUCTS);
$this->cartDiscounts = $this->context->cart->getDiscounts();
foreach ($this->cartDiscounts AS $k => $this->cartDiscount)
if ($error = self::$cart->checkDiscountValidity(new Discount((int)($this->cartDiscount['id_discount'])), $this->cartDiscounts, $orderTotal, $context->cart->getProducts(), false))
$context->cart->deleteDiscount((int)($this->cartDiscount['id_discount']));
if ($error = $this->context->cart->checkDiscountValidity(new Discount((int)($this->cartDiscount['id_discount'])), $this->cartDiscounts, $orderTotal, $this->context->cart->getProducts(), false))
$this->context->cart->deleteDiscount((int)($this->cartDiscount['id_discount']));
$add = Tools::getIsset('add') ? 1 : 0;
$delete = Tools::getIsset('delete') ? 1 : 0;
if (Configuration::get('PS_TOKEN_ENABLE') == 1 &&
strcasecmp(Tools::getToken(false), strval(Tools::getValue('token'))) &&
$context->cookie->isLogged() === true)
$this->context->customer->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[$context->cookie->getName()]))
if (($add OR Tools::getIsset('update') OR $delete) AND isset($_COOKIE[$this->context->cookie->getName()]))
{
//get the values
$idProduct = (int)(Tools::getValue('id_product', NULL));
@@ -103,7 +101,7 @@ class CartControllerCore extends FrontController
$this->errors[] = Tools::displayError('Product not found');
else
{
$producToAdd = new Product($idProduct, true, $context->language->id);
$producToAdd = new Product($idProduct, true, $this->context->language->id);
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).'"]}');
@@ -124,7 +122,7 @@ class CartControllerCore extends FrontController
{
$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($context->link->getProductLink($producToAdd));
Tools::redirectAdmin($this->context->link->getProductLink($producToAdd));
elseif (!$delete AND !Product::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).'"]}');
@@ -139,14 +137,14 @@ class CartControllerCore extends FrontController
/* Check vouchers compatibility */
if ($add AND (($producToAdd->specificPrice AND (float)($producToAdd->specificPrice['reduction'])) OR $producToAdd->on_sale))
{
$discounts = $context->cart->getDiscounts();
$discounts = $this->context->cart->getDiscounts();
$hasUndiscountedProduct = null;
foreach($discounts as $discount)
{
if(is_null($hasUndiscountedProduct))
{
$hasUndiscountedProduct = false;
foreach($context->cart->getProducts() as $product)
foreach($this->context->cart->getProducts() as $product)
if($product['reduction_applies'] === false)
{
$hasUndiscountedProduct = true;
@@ -166,18 +164,18 @@ class CartControllerCore extends FrontController
if ($add AND $qty >= 0)
{
/* Product addition to the cart */
if (!$context->cart->id)
if (!$this->context->cart->id)
{
$context->cart->add();
if ($context->cart->id)
$context->cookie->id_cart = (int)$context->cart->id;
$this->context->cart->add();
if ($this->context->cart->id)
$this->context->cookie->id_cart = (int)$this->context->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 = $context->cart->updateQty($qty, $idProduct, $idProductAttribute, $customizationId, Tools::getValue('op', 'up'));
$updateQuantity = $this->context->cart->updateQty($qty, $idProduct, $idProductAttribute, $customizationId, Tools::getValue('op', 'up'));
if ($updateQuantity < 0)
{
@@ -206,25 +204,25 @@ class CartControllerCore extends FrontController
}
elseif ($delete)
{
if ($context->cart->deleteProduct($idProduct, $idProductAttribute, $customizationId))
if (!Cart::getNbProducts((int)(self::$cart->id)))
if ($this->context->cart->deleteProduct($idProduct, $idProductAttribute, $customizationId))
if (!Cart::getNbProducts((int)($this->context->cart->id)))
{
self::$cart->id_carrier = 0;
self::$cart->gift = 0;
self::$cart->gift_message = '';
self::$cart->update();
$this->context->cart->id_carrier = 0;
$this->context->cart->gift = 0;
$this->context->cart->gift_message = '';
$this->context->cart->update();
}
}
}
$discounts = self::$cart->getDiscounts();
$discounts = $this->context->cart->getDiscounts();
foreach($discounts AS $discount)
{
$discountObj = new Discount((int)($discount['id_discount']), (int)(self::$cookie->id_lang));
$discountObj = new Discount($discount['id_discount'], $this->context->language->id);
if ($error = self::$cart->checkDiscountValidity($discountObj, $discounts, self::$cart->getOrderTotal(true, Cart::ONLY_PRODUCTS), self::$cart->getProducts(), false))
if ($error = $this->context->cart->checkDiscountValidity($discountObj, $discounts, $this->context->cart->getOrderTotal(true, Cart::ONLY_PRODUCTS), $this->context->cart->getProducts(), false))
{
self::$cart->deleteDiscount((int)($discount['id_discount']));
self::$cart->update();
$this->context->cart->deleteDiscount((int)($discount['id_discount']));
$this->context->cart->update();
$errors[] = $error;
}
}
@@ -252,6 +250,6 @@ class CartControllerCore extends FrontController
public function displayContent()
{
parent::displayContent();
self::$smarty->display(_PS_THEME_DIR_.'errors.tpl');
$this->context->smarty->display(_PS_THEME_DIR_.'errors.tpl');
}
}