Files
2024-12-17 13:43:22 +01:00

224 lines
10 KiB
PHP

<?php
/**
* 2007-2021 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:
* https://opensource.org/licenses/OSL-3.0
* 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-2021 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class CartController extends CartControllerCore
{
/**
* This process add or update a product in the cart.
*/
protected function processChangeProductInCart()
{
$mode = (Tools::getIsset('update') && $this->id_product) ? 'update' : 'add';
$index = Configuration::get('WD_CUSTOMIZATION_INDEX');
$customization_added_to_cart = Db::getInstance()->executeS('SELECT *
FROM `' . _DB_PREFIX_ . 'customized_data`
WHERE `id_customization` = ' . (int) $this->customization_id);
$ErrorKey = ('update' === $mode) ? 'updateOperationError' : 'errors';
$cart_update = Configuration::get('squaremeter_cartupdate');
if ($cart_update == 'off') {
if ($mode == 'update' && $this->customization_id !== 0 && $customization_added_to_cart[0]['index'] == $index) {
$product = new Product($this->id_product, true, $this->context->language->id);
if ($this->shouldAvailabilityErrorBeRaised($product, $customization_added_to_cart[0]['total_dimension']) == 1) {
$this->{$ErrorKey}[] = $this->trans(
'The product is no longer available in this quantity.',
array(),
'Shop.Notifications.Error'
);
return;
parent::processChangeProductInCart();
}
if (Shop::isFeatureActive()) {
$idShop = (int) Context::getContext()->shop->id;
} else {
$idShop = 1;
}
$dimension_data = Db::getInstance()->executeS('SELECT *
FROM `' . _DB_PREFIX_ . 'squaremeteradmin` pa
WHERE pa.`id_product` = ' . (int) $this->id_product . ' AND
pa.`id_shop` = ' . (int) $idShop);
$this->{$ErrorKey}[] = $this->trans(
'You can not update the customizable product in cart. Please go to the product page and update the dimension.',
array(),
'Shop.Notifications.Error'
);
return;
parent::processChangeProductInCart();
}
}
if (Tools::getIsset('group')) {
$this->id_product_attribute = (int) Product::getIdProductAttributeByIdAttributes(
$this->id_product,
Tools::getValue('group')
);
}
if ($this->qty == 0) {
$this->{$ErrorKey}[] = $this->trans(
'Null quantity.',
[],
'Shop.Notifications.Error'
);
} elseif (!$this->id_product) {
$this->{$ErrorKey}[] = $this->trans(
'Product not found',
[],
'Shop.Notifications.Error'
);
}
$product = new Product($this->id_product, true, $this->context->language->id);
if (!$product->id || !$product->active || !$product->checkAccess($this->context->cart->id_customer)) {
$this->{$ErrorKey}[] = $this->trans(
'This product (%product%) is no longer available.',
['%product%' => $product->name],
'Shop.Notifications.Error'
);
return;
}
if (!$this->id_product_attribute && $product->hasAttributes()) {
$minimum_quantity = ($product->out_of_stock == 2)
? !Configuration::get('PS_ORDER_OUT_OF_STOCK')
: !$product->out_of_stock;
$this->id_product_attribute = Product::getDefaultAttribute($product->id, $minimum_quantity);
if (!$this->id_product_attribute) {
Tools::redirectAdmin($this->context->link->getProductLink($product));
}
}
$qty_to_check = $this->qty;
$cart_products = $this->context->cart->getProducts();
if (is_array($cart_products)) {
foreach ($cart_products as $cart_product) {
if ($this->productInCartMatchesCriteria($cart_product)) {
$qty_to_check = $cart_product['cart_quantity'];
if (Tools::getValue('op', 'up') == 'down') {
$qty_to_check -= $this->qty;
} else {
$qty_to_check += $this->qty;
}
break;
}
}
}
if ('update' !== $mode && $this->shouldAvailabilityErrorBeRaised($product, $qty_to_check)) {
$this->{$ErrorKey}[] = $this->trans(
'The product is no longer available in this quantity.',
[],
'Shop.Notifications.Error'
);
}
$customization_added_to_cart = Db::getInstance()->executeS('SELECT *
FROM `' . _DB_PREFIX_ . 'customized_data`
WHERE `id_customization` = ' . (int) $this->customization_id);
if (((Tools::getValue('op', 'up') == 'down') && ($this->customization_id !== 0) && ($customization_added_to_cart[0]['index'] == $index)) == false) {
if (!$this->id_product_attribute) {
if ($qty_to_check < $product->minimal_quantity) {
$this->errors[] = $this->trans(
'The minimum purchase order quantity for the product %product% is %quantity%.',
['%product%' => $product->name, '%quantity%' => $product->minimal_quantity],
'Shop.Notifications.Error'
);
return;
}
} else {
$combination = new Combination($this->id_product_attribute);
if ($qty_to_check < $combination->minimal_quantity) {
$this->errors[] = $this->trans(
'The minimum purchase order quantity for the product %product% is %quantity%.',
['%product%' => $product->name, '%quantity%' => $combination->minimal_quantity],
'Shop.Notifications.Error'
);
return;
}
}
}
if (!$this->errors) {
if (!$this->context->cart->id) {
if (Context::getContext()->cookie->id_guest) {
$guest = new Guest(Context::getContext()->cookie->id_guest);
$this->context->cart->mobile_theme = $guest->mobile_theme;
}
$this->context->cart->add();
if ($this->context->cart->id) {
$this->context->cookie->id_cart = (int) $this->context->cart->id;
}
}
if (!$product->hasAllRequiredCustomizableFields() && !$this->customization_id) {
$this->{$ErrorKey}[] = $this->trans(
'Please fill in all of the required fields, and then save your customizations.',
[],
'Shop.Notifications.Error'
);
}
if (!$this->errors) {
$cart_rules = $this->context->cart->getCartRules();
$available_cart_rules = CartRule::getCustomerCartRules(
$this->context->language->id,
(isset($this->context->customer->id) ? $this->context->customer->id : 0),
true,
true,
true,
$this->context->cart,
false,
true
);
$update_quantity = $this->context->cart->updateQty(
$this->qty,
$this->id_product,
$this->id_product_attribute,
$this->customization_id,
Tools::getValue('op', 'up'),
$this->id_address_delivery,
null,
true,
true
);
if ($update_quantity < 0) {
$minimal_quantity = ($this->id_product_attribute)
? Attribute::getAttributeMinimalQty($this->id_product_attribute)
: $product->minimal_quantity;
$this->{$ErrorKey}[] = $this->trans(
'You must add %quantity% minimum quantity',
['%quantity%' => $minimal_quantity],
'Shop.Notifications.Error'
);
} elseif (!$update_quantity) {
$this->errors[] = $this->trans(
'You already have the maximum quantity available for this product.',
[],
'Shop.Notifications.Error'
);
} elseif ($this->shouldAvailabilityErrorBeRaised($product, $qty_to_check)) {
$this->{$ErrorKey}[] = $this->trans(
'The product is no longer available in this quantity.',
[],
'Shop.Notifications.Error'
);
}
}
}
$removed = CartRule::autoRemoveFromCart();
CartRule::autoAddToCart();
}
}