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

[frontend-api] applied promo code is now taken into account in priceByTransportQuery and priceByPaymentQuery #3118

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions UPGRADE-14.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,10 @@ Follow the instructions in relevant sections, e.g. `shopsys/coding-standards` or
- see #project-base-diff to update your project

- fix friendly URLs ([#3115](https://github.com/shopsys/shopsys/pull/3115))

- see #project-base-diff to update your project

- take promo code into account in priceByTransportQuery and priceByPaymentQuery ([#3118](https://github.com/shopsys/shopsys/pull/3118))
- see #project-base-diff to update your project

### Storefront
Expand Down
4 changes: 4 additions & 0 deletions project-base/app/config/services_frontend_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,7 @@ services:
App\FrontendApi\Component\SessionChecker\SessionChecker:
tags:
- { name: kernel.event_listener, event: kernel.response, method: onKernelResponse, priority: -999 } # must be run before the session is closed (in Symfony\Component\HttpKernel\EventListener\SessionListener::onKernelResponse)

Shopsys\FrontendApiBundle\Model\Resolver\Price\PriceQuery:
alias: App\FrontendApi\Resolver\Price\PriceQuery
public: true
133 changes: 133 additions & 0 deletions project-base/app/src/FrontendApi/Resolver/Price/PriceQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

declare(strict_types=1);

namespace App\FrontendApi\Resolver\Price;

use ArrayObject;
use Shopsys\FrameworkBundle\Model\Payment\Payment;
use Shopsys\FrameworkBundle\Model\Pricing\Price;
use Shopsys\FrameworkBundle\Model\Transport\Transport;
use Shopsys\FrontendApiBundle\Component\GqlContext\GqlContextHelper;
use Shopsys\FrontendApiBundle\Model\Resolver\Price\PriceQuery as BasePriceQuery;

/**
* @property \App\Model\Product\ProductOnCurrentDomainElasticFacade $productOnCurrentDomainFacade
* @property \App\Model\Customer\User\CurrentCustomerUser $currentCustomerUser
* @property \App\Model\Order\Preview\OrderPreviewFactory $orderPreviewFactory
* @property \App\FrontendApi\Model\Order\OrderApiFacade $orderApiFacade
* @method __construct(\Shopsys\FrameworkBundle\Model\Product\ProductCachedAttributesFacade $productCachedAttributesFacade, \App\Model\Product\ProductOnCurrentDomainElasticFacade $productOnCurrentDomainFacade, \Shopsys\FrameworkBundle\Model\Payment\PaymentPriceCalculation $paymentPriceCalculation, \Shopsys\FrameworkBundle\Component\Domain\Domain $domain, \Shopsys\FrameworkBundle\Model\Pricing\Currency\CurrencyFacade $currencyFacade, \Shopsys\FrameworkBundle\Model\Transport\TransportPriceCalculation $transportPriceCalculation, \Shopsys\FrontendApiBundle\Model\Price\PriceFacade $priceFacade, \App\Model\Customer\User\CurrentCustomerUser $currentCustomerUser, \App\Model\Order\Preview\OrderPreviewFactory $orderPreviewFactory, \Shopsys\FrontendApiBundle\Model\Cart\CartApiFacade $cartApiFacade, \App\FrontendApi\Model\Order\OrderApiFacade $orderApiFacade)
* @method \Shopsys\FrameworkBundle\Model\Product\Pricing\ProductPrice priceByProductQuery(\App\Model\Product\Product|array $data)
* @method \Shopsys\FrameworkBundle\Model\Pricing\Price calculateIndependentPaymentPrice(\App\Model\Payment\Payment $payment)
* @method \Shopsys\FrameworkBundle\Model\Pricing\Price calculateIndependentTransportPrice(\App\Model\Transport\Transport $transport)
*/
class PriceQuery extends BasePriceQuery
{
/**
* @param \App\Model\Transport\Transport $transport
* @param string|null $cartUuid
* @param \ArrayObject|null $context
* @return \Shopsys\FrameworkBundle\Model\Pricing\Price
*/
public function priceByTransportQuery(
Transport $transport,
?string $cartUuid = null,
?ArrayObject $context = null,
): Price {
$cartUuid = $cartUuid ?? GqlContextHelper::getCartUuid($context);

$customerUser = $this->currentCustomerUser->findCurrentCustomerUser();

if ($customerUser === null && $cartUuid === null) {
return $this->calculateIndependentTransportPrice($transport);
}

/** @var \App\Model\Cart\Cart $cart */
$cart = $this->cartApiFacade->findCart($customerUser, $cartUuid);

if ($cart === null) {
return $this->calculateIndependentTransportPrice($transport);
}

$domainId = $this->domain->getId();
$currency = $this->currencyFacade->getDomainDefaultCurrencyByDomainId($domainId);
$orderPreview = $this->orderPreviewFactory->create(
$currency,
$domainId,
$cart->getQuantifiedProducts(),
$transport,
null,
$customerUser,
null,
null,
$cart->getFirstAppliedPromoCode(),
);

return $this->transportPriceCalculation->calculatePrice(
$transport,
$currency,
$orderPreview->getProductsPrice(),
$domainId,
);
}

/**
* @param \App\Model\Payment\Payment $payment
* @param string|null $cartUuid
* @param \ArrayObject|null $context
* @return \Shopsys\FrameworkBundle\Model\Pricing\Price
*/
public function priceByPaymentQuery(
grossmannmartin marked this conversation as resolved.
Show resolved Hide resolved
Payment $payment,
?string $cartUuid = null,
?ArrayObject $context = null,
): Price {
$cartUuid = $cartUuid ?? GqlContextHelper::getCartUuid($context);
$orderUuid = GqlContextHelper::getOrderUuid($context);

if ($cartUuid === null && $orderUuid !== null) {
$order = $this->orderApiFacade->getByUuid($orderUuid);

return $this->paymentPriceCalculation->calculatePrice(
$payment,
$order->getCurrency(),
$order->getTotalProductsPrice(),
$order->getDomainId(),
);
}

$customerUser = $this->currentCustomerUser->findCurrentCustomerUser();

if ($customerUser === null && $cartUuid === null) {
return $this->calculateIndependentPaymentPrice($payment);
}

/** @var \App\Model\Cart\Cart $cart */
$cart = $this->cartApiFacade->findCart($customerUser, $cartUuid);

if ($cart === null) {
return $this->calculateIndependentPaymentPrice($payment);
}

$domainId = $this->domain->getId();
$currency = $this->currencyFacade->getDomainDefaultCurrencyByDomainId($domainId);
$orderPreview = $this->orderPreviewFactory->create(
$currency,
$domainId,
$cart->getQuantifiedProducts(),
null,
$payment,
$customerUser,
null,
null,
$cart->getFirstAppliedPromoCode(),
);

return $this->paymentPriceCalculation->calculatePrice(
$payment,
$currency,
$orderPreview->getProductsPrice(),
$domainId,
);
}
}