Skip to content

Commit

Permalink
[frontend-api] applied promo code is now taken into account in priceB…
Browse files Browse the repository at this point in the history
…yTransportQuery and priceByPaymentQuery (#3118)
  • Loading branch information
vitek-rostislav committed Apr 24, 2024
2 parents 6ba3263 + e20539a commit 784888e
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
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(
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,
);
}
}

0 comments on commit 784888e

Please sign in to comment.