Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.4-develop' into spartans_pr_15…
Browse files Browse the repository at this point in the history
…042024
  • Loading branch information
glo60612 committed May 29, 2024
2 parents 554f668 + 203c2f7 commit c1c982d
Show file tree
Hide file tree
Showing 11 changed files with 986 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ public function save()
$itemsToDelete = [];
$itemsToSave = [];
/** @var Rate $item */
foreach ($this->getItems() as $item) {
foreach ($this->getItems() as $key => $item) {
if ($item->isDeleted()) {
$itemsToDelete[] = $item;
$this->removeItemByKey($key);
} else {
$itemsToSave[] = $item;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* ADOBE CONFIDENTIAL
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
*/

declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue;

use Magento\Catalog\Model\Product\Option;
use Magento\Catalog\Model\Product\Option\Type\Text as TextOptionType;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\Quote\Model\Quote\Item\Option as SelectedOption;
use Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValueInterface;

class File implements CustomizableOptionValueInterface
{
/**
* @param PriceUnitLabel $priceUnitLabel
*/
public function __construct(
private readonly PriceUnitLabel $priceUnitLabel
) {
}

/**
* @inheritdoc
*/
public function getData(
QuoteItem $cartItem,
Option $option,
SelectedOption $selectedOption
): array {
/** @var TextOptionType $optionTypeRenderer */
$optionTypeRenderer = $option->groupFactory($option->getType());
$optionTypeRenderer->setOption($option);
$priceValueUnits = $this->priceUnitLabel->getData($option->getPriceType());
$optionTypeRenderer->setData('configuration_item_option', $selectedOption);
$value = $optionTypeRenderer->getFormattedOptionValue($selectedOption->getValue());
$selectedOptionValueData = [
'id' => $selectedOption->getId(),
'label' => $option->getTitle(),
'value' => $value,
'price' => [
'type' => strtoupper($option->getPriceType()),
'units' => $priceValueUnits,
'value' => $option->getPrice(),
],
];
return [$selectedOptionValueData];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function execute(Quote $cart, int $pageSize, int $offset, string $orderBy
->addFieldToFilter('quote_id', $cart->getId())
->setOrder($orderBy, $order)
->setCurPage($offset)
->setPageSize($pageSize);
->setPageSize($pageSize)
->setQuote($cart);

$items = [];
$itemDeletedCount = 0;
Expand Down
25 changes: 24 additions & 1 deletion app/code/Magento/QuoteGraphQl/Model/Resolver/CartItemPrices.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,29 @@ private function getOriginalRowTotal(Item $cartItem): float
{
$qty = $cartItem->getTotalQty();
// Round unit price before multiplying to prevent losing 1 cent on subtotal
return $this->priceCurrency->round($cartItem->getOriginalPrice()) * $qty;
return $this->priceCurrency->round($cartItem->getOriginalPrice() + $this->getOptionsPrice($cartItem)) * $qty;
}

/**
* Get the product custom options price
*
* @param Item $cartItem
* @return float
*/
private function getOptionsPrice(Item $cartItem): float
{
$price = 0.0;
$optionIds = $cartItem->getProduct()->getCustomOption('option_ids');
if (!$optionIds) {
return $price;
}
foreach (explode(',', $optionIds->getValue() ?? '') as $optionId) {
$option = $cartItem->getProduct()->getOptionById($optionId);
if ($option) {
$price += $option->getRegularPrice();
}
}

return $price;
}
}
20 changes: 11 additions & 9 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/EstimateTotals.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\AddressFactory;

/**
Expand Down Expand Up @@ -69,7 +70,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
throw new GraphQlInputException(__('Required parameter "country_code" is missing'));
}

$this->totalsInformationManagement->calculate($cartId, $this->getTotalsInformation($args['input']));
$data = $this->getTotalsInformation($args['input']);
$this->totalsInformationManagement->calculate($cartId, $data);

return [
'cart' => [
Expand Down Expand Up @@ -106,14 +108,14 @@ private function getTotalsInformation(array $input): TotalsInformationInterface
*/
private function getAddress(array $data): AddressInterface
{
$data = [
AddressInterface::KEY_COUNTRY_ID => $data['country_code'],
AddressInterface::KEY_REGION => $data['region'][AddressInterface::KEY_REGION] ?? null,
AddressInterface::KEY_REGION_ID => $data['region'][AddressInterface::KEY_REGION_ID] ?? null,
AddressInterface::KEY_REGION_CODE => $data['region'][AddressInterface::KEY_REGION_CODE] ?? null,
AddressInterface::KEY_POSTCODE => $data[AddressInterface::KEY_POSTCODE] ?? null,
];
/** @var Address $address */
$address = $this->addressFactory->create();
$address->setCountryId($data['country_code']);
$address->setRegion($data['region'][AddressInterface::KEY_REGION] ?? null);
$address->setRegionId($data['region'][AddressInterface::KEY_REGION_ID] ?? null);
$address->setRegionCode($data['region'][AddressInterface::KEY_REGION_CODE] ?? null);
$address->setPostcode($data[AddressInterface::KEY_POSTCODE] ?? null);

return $this->addressFactory->create(['data' => array_filter($data)]);
return $address;
}
}
1 change: 1 addition & 0 deletions app/code/Magento/QuoteGraphQl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<item name="radio" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Dropdown</item>
<item name="checkbox" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Multiple</item>
<item name="multiple" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Multiple</item>
<item name="file" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\File</item>
</argument>
</arguments>
</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,117 @@ public function testEstimateTotals(string $countryCode, string $shipping, array
);
}

/**
* @return void
* @throws LocalizedException
*/
#[
DataFixture(
ProductTaxClass::class,
as: 'product_tax_class'
),
DataFixture(
TaxRateFixture::class,
[
'tax_country_id' => 'ES',
'tax_postcode' => '08005',
],
'rate'
),
DataFixture(
TaxRuleFixture::class,
[
'customer_tax_class_ids' => [3],
'product_tax_class_ids' => ['$product_tax_class.classId$'],
'tax_rate_ids' => ['$rate.id$']
],
'rule'
),
DataFixture(
ProductFixture::class,
[
'custom_attributes' => [
'tax_class_id' => '$product_tax_class.classId$'
],
],
'product'
),
DataFixture(GuestCart::class, ['currency' => 'USD'], 'cart'),
DataFixture(QuoteIdMask::class, ['cart_id' => '$cart.id$'], 'quoteIdMask'),
DataFixture(AddProductToCart::class, [
'cart_id' => '$cart.id$',
'product_id' => '$product.id$',
'qty' => 1
])
]
public function testEstimateTotalsCleanPostCode(): void
{
$maskedQuoteId = DataFixtureStorageManager::getStorage()->get('quoteIdMask')->getMaskedId();

$query = <<<QUERY
mutation {
estimateTotals(input: {
cart_id: "{$maskedQuoteId}",
address: {
country_code: ES
postcode: "%s"
},
shipping_method: {
carrier_code: "flatrate",
method_code: "flatrate"
}
}) {
cart {
prices {
applied_taxes {
amount {
value
currency
}
}
}
}
}
}
QUERY;
$response = $this->graphQlMutation(sprintf($query, '08005'));

self::assertEquals(
[
'estimateTotals' => [
'cart' => [
'prices' => [
'applied_taxes' => [
[
'amount' => [
'value' => 1,
'currency' => 'USD'
]
]
]
]
]
]
],
$response
);

$response = $this->graphQlMutation(sprintf($query, ''));

self::assertEquals(
[
'estimateTotals' => [
'cart' => [
'prices' => [
'applied_taxes' => []
]
]
]
],
$response
);
}

public function estimationsProvider(): array
{
return [
Expand Down
Loading

0 comments on commit c1c982d

Please sign in to comment.