Skip to content

Commit

Permalink
Merge pull request PrestaShop#36287 from Codencode/fix-36286-problems…
Browse files Browse the repository at this point in the history
…-with-product-customizations

Fix for issue 2 of Problems with Product Customizations
  • Loading branch information
matks authored Aug 22, 2024
2 parents f26e468 + 130d5af commit c9b19f9
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 20 deletions.
12 changes: 8 additions & 4 deletions classes/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,8 @@ private function getOrderPrices(
isset($productRow['id_product_attribute']) ? (int) $productRow['id_product_attribute'] : 0,
true,
false,
true
true,
isset($productRow['id_customization']) ? (int) $productRow['id_customization'] : 0
);

$orderPrices['price_without_reduction_without_tax'] = Product::getPriceFromOrder(
Expand All @@ -1138,7 +1139,8 @@ private function getOrderPrices(
isset($productRow['id_product_attribute']) ? (int) $productRow['id_product_attribute'] : 0,
false,
false,
true
true,
isset($productRow['id_customization']) ? (int) $productRow['id_customization'] : 0
);

$orderPrices['price_with_reduction'] = Product::getPriceFromOrder(
Expand All @@ -1147,7 +1149,8 @@ private function getOrderPrices(
isset($productRow['id_product_attribute']) ? (int) $productRow['id_product_attribute'] : 0,
true,
true,
true
true,
isset($productRow['id_customization']) ? (int) $productRow['id_customization'] : 0
);

$orderPrices['price'] = $orderPrices['price_with_reduction_without_tax'] = Product::getPriceFromOrder(
Expand All @@ -1156,7 +1159,8 @@ private function getOrderPrices(
isset($productRow['id_product_attribute']) ? (int) $productRow['id_product_attribute'] : 0,
false,
true,
true
true,
isset($productRow['id_customization']) ? (int) $productRow['id_customization'] : 0
);

// If the product price was not found in the order, use cart prices as fallback
Expand Down
6 changes: 5 additions & 1 deletion classes/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -4057,7 +4057,8 @@ public static function getPriceFromOrder(
int $combinationId,
bool $withTaxes,
bool $useReduction,
bool $withEcoTax
bool $withEcoTax,
int $customizationId = 0
): ?float {
$sql = new DbQuery();
$sql->select('od.*, t.rate AS tax_rate');
Expand All @@ -4067,6 +4068,9 @@ public static function getPriceFromOrder(
if (Combination::isFeatureActive()) {
$sql->where('od.`product_attribute_id` = ' . $combinationId);
}
if (Customization::isFeatureActive()) {
$sql->where('od.`id_customization` = ' . $customizationId);
}
$sql->leftJoin('order_detail_tax', 'odt', 'odt.id_order_detail = od.id_order_detail');
$sql->leftJoin('tax', 't', 't.id_tax = odt.id_tax');
$res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ public function handle(AddProductToOrderCommand $command)
$command->getProductId()->getValue(),
null !== $command->getCombinationId() ? $command->getCombinationId()->getValue() : 0,
$command->getProductPriceTaxExcluded(),
$command->getProductPriceTaxIncluded()
$command->getProductPriceTaxIncluded(),
0
);
StockAvailable::synchronize($product->id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public function handle(UpdateProductInOrderCommand $command)
(int) $orderDetail->product_id,
(int) $orderDetail->product_attribute_id,
$command->getPriceTaxExcluded(),
$command->getPriceTaxIncluded()
$command->getPriceTaxIncluded(),
(int) $orderDetail->id_customization
);

// Update invoice, quantity and amounts
Expand Down
9 changes: 5 additions & 4 deletions src/Adapter/Order/OrderAmountUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ private function updateOrderDetails(Order $order, Cart $cart): void
$cartProducts = $cart->getProducts(true, false, null, true, $this->keepOrderPrices);
foreach ($order->getCartProducts() as $orderProduct) {
$orderDetail = new OrderDetail($orderProduct['id_order_detail'], null, $this->contextStateManager->getContext());
$cartProduct = $this->getProductFromCart($cartProducts, (int) $orderDetail->product_id, (int) $orderDetail->product_attribute_id);
$cartProduct = $this->getProductFromCart($cartProducts, (int) $orderDetail->product_id, (int) $orderDetail->product_attribute_id, (int) $orderDetail->id_customization);

$this->orderDetailUpdater->updateOrderDetail(
$orderDetail,
Expand All @@ -389,17 +389,18 @@ private function updateOrderDetails(Order $order, Cart $cart): void
*
* @return array
*/
private function getProductFromCart(array $cartProducts, int $productId, int $productAttributeId): array
private function getProductFromCart(array $cartProducts, int $productId, int $productAttributeId, int $customizationId = 0): array
{
$cartProduct = array_reduce($cartProducts, function ($carry, $item) use ($productId, $productAttributeId) {
$cartProduct = array_reduce($cartProducts, function ($carry, $item) use ($productId, $productAttributeId, $customizationId) {
if (null !== $carry) {
return $carry;
}

$productMatch = $item['id_product'] == $productId;
$combinationMatch = $item['id_product_attribute'] == $productAttributeId;
$customizationMatch = $item['id_customization'] == $customizationId;

return $productMatch && $combinationMatch ? $item : null;
return $productMatch && $combinationMatch && $customizationMatch ? $item : null;
});

// This shouldn't happen, if it does something was not done before updating the Order (removing an OrderDetail maybe)
Expand Down
17 changes: 11 additions & 6 deletions src/Adapter/Order/OrderDetailUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public function updateOrderDetailsForProduct(
int $productId,
int $combinationId,
DecimalNumber $priceTaxExcluded,
DecimalNumber $priceTaxIncluded
DecimalNumber $priceTaxIncluded,
int $customizationId = 0
): void {
list($roundType, $computingPrecision, $taxAddress) = $this->prepareOrderContext($order);

Expand All @@ -147,7 +148,8 @@ public function updateOrderDetailsForProduct(
$priceTaxIncluded,
$roundType,
$computingPrecision,
$taxAddress
$taxAddress,
$customizationId
);
} finally {
$this->contextStateManager->restorePreviousContext();
Expand Down Expand Up @@ -314,9 +316,10 @@ private function applyUpdatesForProduct(
DecimalNumber $priceTaxIncluded,
int $roundType,
int $computingPrecision,
Address $taxAddress
Address $taxAddress,
int $customizationId = 0
): void {
$identicalOrderDetails = $this->getOrderDetailsForProduct($order, $productId, $combinationId);
$identicalOrderDetails = $this->getOrderDetailsForProduct($order, $productId, $combinationId, $customizationId);
if (empty($identicalOrderDetails)) {
return;
}
Expand Down Expand Up @@ -361,13 +364,15 @@ private function applyUpdatesForProduct(
private function getOrderDetailsForProduct(
Order $order,
int $productId,
int $combinationId
int $combinationId,
int $customizationId = 0
): array {
$identicalOrderDetails = [];
$orderDetails = $order->getOrderDetailList();
foreach ($orderDetails as $orderDetail) {
if ((int) $orderDetail['product_id'] === $productId
&& (int) $orderDetail['product_attribute_id'] === $combinationId) {
&& (int) $orderDetail['product_attribute_id'] === $combinationId
&& (int) $orderDetail['id_customization'] === $customizationId) {
$identicalOrderDetails[] = new OrderDetail($orderDetail['id_order_detail']);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Adapter/Product/PriceCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,17 @@ public function getOrderPrice(
int $combinationId,
bool $withTaxes,
bool $useReduction,
bool $withEcoTax
bool $withEcoTax,
int $customizationId = 0
): ?float {
return Product::getPriceFromOrder(
$orderId,
$productId,
$combinationId,
$withTaxes,
$useReduction,
$withEcoTax
$withEcoTax,
$customizationId
);
}
}
3 changes: 2 additions & 1 deletion src/Core/Cart/CartRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ protected function getProductPrice(CartCore $cart, $rowData)
(int) $rowData['id_product_attribute'],
$computationParameters['withTaxes'],
true,
$this->useEcotax
$this->useEcotax,
(int) $rowData['id_customization']
);
}
if (null === $productPrices[$productPrice]['value']) {
Expand Down

0 comments on commit c9b19f9

Please sign in to comment.