Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when adding product in cart or editing quantity #9405

Merged
merged 19 commits into from Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 14 additions & 6 deletions admin-dev/themes/default/webpack.config.js
Expand Up @@ -22,11 +22,11 @@
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
const config = {
entry: [
'./js/theme.js'
],
Expand Down Expand Up @@ -74,7 +74,13 @@ module.exports = {
}]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new ExtractTextPlugin('theme.css'),
]
};

if (process.env.NODE_ENV === 'production') {
config.plugins.push(
new webpack.optimize.UglifyJsPlugin({
sourceMap: false,
compress: {
Expand All @@ -89,5 +95,7 @@ module.exports = {
comments: false
}
})
]
};
);
}

module.exports = config;
5 changes: 3 additions & 2 deletions admin-dev/themes/new-theme/webpack.config.js
Expand Up @@ -22,12 +22,13 @@
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const keepLicense = require('uglify-save-license');

let config = {
const config = {
entry: {
main: [
'prestakit/dist/js/prestashop-ui-kit.js',
Expand Down Expand Up @@ -190,7 +191,6 @@ let config = {
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new ExtractTextPlugin('theme.css'),
new webpack.ProvidePlugin({
moment: 'moment', // needed for bootstrap datetime picker
Expand All @@ -216,6 +216,7 @@ if (process.env.NODE_ENV === 'production') {
})
);
} else {
config.plugins.push(new webpack.HotModuleReplacementPlugin());
config.entry.stock.push('webpack/hot/only-dev-server');
config.entry.stock.push('webpack-dev-server/client?http://localhost:8080');
}
Expand Down
150 changes: 86 additions & 64 deletions classes/Cart.php
Expand Up @@ -1241,7 +1241,12 @@ public function getProductQuantity($idProduct, $idProductAttribute = 0, $idCusto
*/
public function containsProduct($id_product, $id_product_attribute = 0, $id_customization = 0, $id_address_delivery = 0)
{
$result = $this->getProductQuantity($id_product, $id_product_attribute, $id_customization, $id_address_delivery);
$result = $this->getProductQuantity(
$id_product,
$id_product_attribute,
$id_customization,
$id_address_delivery
);

if (empty($result['quantity'])) {
return false;
Expand Down Expand Up @@ -1276,10 +1281,13 @@ public function updateQty(
}

if (Context::getContext()->customer->id) {
if ($id_address_delivery == 0 && (int)$this->id_address_delivery) { // The $id_address_delivery is null, use the cart delivery address
// The $id_address_delivery is null, use the cart delivery address
if ($id_address_delivery == 0 && (int)$this->id_address_delivery) {
$id_address_delivery = $this->id_address_delivery;
} elseif ($id_address_delivery == 0) { // The $id_address_delivery is null, get the default customer address
$id_address_delivery = (int)Address::getFirstCustomerAddressId((int)Context::getContext()->customer->id);
$id_address_delivery = (int) Address::getFirstCustomerAddressId(
(int) Context::getContext()->customer->id
);
} elseif (!Customer::customerHasAddress(Context::getContext()->customer->id, $id_address_delivery)) { // The $id_address_delivery must be linked with customer
$id_address_delivery = 0;
}
Expand Down Expand Up @@ -1332,87 +1340,101 @@ public function updateQty(
// Hook::exec('actionBeforeCartUpdateQty', $data);
Hook::exec('actionCartUpdateQuantityBefore', $data);

if ((int)$quantity <= 0) {
return $this->deleteProduct($id_product, $id_product_attribute, (int)$id_customization);
} elseif (!$product->available_for_order
|| (Configuration::isCatalogMode() && !defined('_PS_ADMIN_DIR_'))
if ((int) $quantity <= 0) {
return $this->deleteProduct($id_product, $id_product_attribute, (int) $id_customization);
}

if (!$product->available_for_order ||
Configuration::isCatalogMode() && !defined('_PS_ADMIN_DIR_')
) {
return false;
} else {
/* Check if the product is already in the cart */
$cartProductQuantity = $this->getProductQuantity($id_product, $id_product_attribute, (int)$id_customization, (int)$id_address_delivery);
}

/* Update quantity if product already exist */
if (!empty($cartProductQuantity['quantity'])) {
$productQuantity = Product::getQuantity($id_product, $id_product_attribute, null, $this);
$availableOutOfStock = Product::isAvailableWhenOutOfStock($product->out_of_stock);
/* Check if the product is already in the cart */
$cartProductQuantity = $this->getProductQuantity(
$id_product,
$id_product_attribute,
(int) $id_customization,
(int) $id_address_delivery
);

if ($operator == 'up') {
$updateQuantity = '+ ' . $quantity;
$newProductQuantity = $productQuantity - $quantity;
/* Update quantity if product already exist */
if (!empty($cartProductQuantity['quantity'])) {
$productQuantity = Product::getQuantity($id_product, $id_product_attribute, null, $this);
$availableOutOfStock = Product::isAvailableWhenOutOfStock($product->out_of_stock);

if ($newProductQuantity < 0 && !$availableOutOfStock && !$skipAvailabilityCheckOutOfStock) {
return false;
}
} else if ($operator == 'down') {
$cartFirstLevelProductQuantity = $this->getProductQuantity((int) $id_product, (int) $id_product_attribute, $id_customization);
$updateQuantity = '- ' . $quantity;
$newProductQuantity = $productQuantity + $quantity;
if ($operator == 'up') {
$updateQuantity = '+ ' . $quantity;
$newProductQuantity = $productQuantity - $quantity;

if ($cartFirstLevelProductQuantity['quantity'] <= 1) {
return $this->deleteProduct((int)$id_product, (int)$id_product_attribute, (int)$id_customization);
}
} else {
if ($newProductQuantity < 0 && !$availableOutOfStock && !$skipAvailabilityCheckOutOfStock) {
return false;
}
Db::getInstance()->execute(
'UPDATE `'._DB_PREFIX_.'cart_product`
} elseif ($operator == 'down') {
$cartFirstLevelProductQuantity = $this->getProductQuantity(
(int) $id_product,
(int) $id_product_attribute,
$id_customization
);
$updateQuantity = '- ' . $quantity;
$newProductQuantity = $productQuantity + $quantity;

if ($cartFirstLevelProductQuantity['quantity'] <= 1 ||
$cartProductQuantity['quantity'] - $quantity <= 0
) {
return $this->deleteProduct((int)$id_product, (int)$id_product_attribute, (int)$id_customization);
}
} else {
return false;
}

Db::getInstance()->execute(
'UPDATE `'._DB_PREFIX_.'cart_product`
SET `quantity` = `quantity` ' . $updateQuantity . '
WHERE `id_product` = '.(int)$id_product.
' AND `id_customization` = '.(int)$id_customization.
(!empty($id_product_attribute) ? ' AND `id_product_attribute` = '.(int)$id_product_attribute : '').'
' AND `id_customization` = '.(int)$id_customization.
(!empty($id_product_attribute) ? ' AND `id_product_attribute` = '.(int)$id_product_attribute : '').'
AND `id_cart` = '.(int)$this->id.(Configuration::get('PS_ALLOW_MULTISHIPPING') && $this->isMultiAddressDelivery() ? ' AND `id_address_delivery` = '.(int)$id_address_delivery : '').'
LIMIT 1'
);
} elseif ($operator == 'up') {
/* Add product to the cart */
);
} elseif ($operator == 'up') {
/* Add product to the cart */

$sql = 'SELECT stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity
$sql = 'SELECT stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity
FROM '._DB_PREFIX_.'product p
'.Product::sqlStock('p', $id_product_attribute, true, $shop).'
WHERE p.id_product = '.$id_product;

$result2 = Db::getInstance()->getRow($sql);
$result2 = Db::getInstance()->getRow($sql);

// Quantity for product pack
if (Pack::isPack($id_product)) {
$result2['quantity'] = Pack::getQuantity($id_product, $id_product_attribute, null, $this);
}
// Quantity for product pack
if (Pack::isPack($id_product)) {
$result2['quantity'] = Pack::getQuantity($id_product, $id_product_attribute, null, $this);
}

if (!Product::isAvailableWhenOutOfStock((int)$result2['out_of_stock']) && !$skipAvailabilityCheckOutOfStock) {
if ((int)$quantity > $result2['quantity']) {
return false;
}
if (!Product::isAvailableWhenOutOfStock((int)$result2['out_of_stock']) && !$skipAvailabilityCheckOutOfStock) {
if ((int)$quantity > $result2['quantity']) {
return false;
}
}

if ((int)$quantity < $minimal_quantity) {
return -1;
}
if ((int)$quantity < $minimal_quantity) {
return -1;
}

$result_add = Db::getInstance()->insert('cart_product', array(
'id_product' => (int)$id_product,
'id_product_attribute' => (int)$id_product_attribute,
'id_cart' => (int)$this->id,
'id_address_delivery' => (int)$id_address_delivery,
'id_shop' => $shop->id,
'quantity' => (int)$quantity,
'date_add' => date('Y-m-d H:i:s'),
'id_customization' => (int)$id_customization,
));

if (!$result_add) {
return false;
}
$result_add = Db::getInstance()->insert('cart_product', array(
'id_product' => (int)$id_product,
'id_product_attribute' => (int)$id_product_attribute,
'id_cart' => (int)$this->id,
'id_address_delivery' => (int)$id_address_delivery,
'id_shop' => $shop->id,
'quantity' => (int)$quantity,
'date_add' => date('Y-m-d H:i:s'),
'id_customization' => (int)$id_customization,
));

if (!$result_add) {
return false;
}
}

Expand All @@ -1428,9 +1450,9 @@ public function updateQty(

if ($product->customizable) {
return $this->_updateCustomizationQuantity((int)$quantity, (int)$id_customization, (int)$id_product, (int)$id_product_attribute, (int)$id_address_delivery, $operator);
} else {
return true;
}

return true;
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/Unit/Core/Cart/Adding/Product/AddStandardProductTest.php
Expand Up @@ -80,4 +80,61 @@ public function testProductCanBeAddedInCartIfMoreThanStockButAvailableWhenOutOfS
Configuration::set('PS_ORDER_OUT_OF_STOCK', $oldOrderOutOfStock);
}

/**
* @dataProvider updateQuantitiesProvider
*/
public function testUpdateQuantity($quantity, $operator, $expected, $quantityExpected)
{
$product = $this->getProductFromFixtureId(1);
$result = $this->cart->updateQty(
$quantity,
$product->id,
$id_product_attribute = null,
$id_customization = false,
$operator
);
$cartProductQuantity = $this->cart->getProductQuantity(
$product->id,
$id_product_attribute,
(int) $id_customization,
$id_address_delivery = 0
);

$this->assertEquals($expected, $result);
$this->assertEquals($quantityExpected, $cartProductQuantity['quantity']);
}

public function updateQuantitiesProvider()
{
return [
[1, 'up', true, 1],
[2, 'up', true, 2],
[2, 'down', true, 0],
[0, 'down', true, 0],
];
}

/**
* @dataProvider multipleUpdateQuantitiesProvider
*/
public function testMultipleUpdateQuantity($first, $second)
{
list($quantity, $operator, $expected, $quantityExpected) = $first;
$this->testUpdateQuantity($quantity, $operator, $expected, $quantityExpected);

list($quantity, $operator, $expected, $quantityExpected) = $second;
$this->testUpdateQuantity($quantity, $operator, $expected, $quantityExpected);
}

public function multipleUpdateQuantitiesProvider()
{
return [
[[1, 'up', true, 1], [1, 'up', true, 2]],
[[2, 'up', true, 2], [2, 'down', true, 0]],
[[2, 'down', true, 0], [2, 'up', true, 2]],
[[0, 'down', true, 0], [1, 'nothing', true, 0]],
[[1, 'down', true, 0], [1, 'nothing', true, 0]],
[[1, 'up', true, 1], [10, 'nothing', false, 1]],
];
}
}
11 changes: 10 additions & 1 deletion tests/phpunit.xml
@@ -1,4 +1,5 @@
<phpunit bootstrap="bootstrap.php">
<phpunit bootstrap="bootstrap.php"
color="true">
<php>
<server name="KERNEL_CLASS" value="AppKernel" />
<env name="SYMFONY_ENV" value="test"/>
Expand All @@ -23,4 +24,12 @@
<directory>Integration</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">../src</directory>
<directory suffix=".php">../classes</directory>
<directory suffix=".php">../controllers</directory>
</whitelist>
</filter>
</phpunit>