Skip to content

MC-38025: FPT disappears when switching back to the cart page #31563

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

Merged
merged 1 commit into from
Jan 13, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/code/Magento/Quote/Model/Quote/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,13 @@ public function requestShippingRates(AbstractItem $item = null)
*/
public function getTotals()
{
$totalsData = array_merge($this->getData(), ['address_quote_items' => $this->getAllItems()]);
$totalsData = array_merge(
$this->getData(),
[
'address_quote_items' => $this->getAllItems(),
'quote_items' => $this->getQuote()->getAllItems(),
]
);
$totals = $this->totalsReader->fetch($this->getQuote(), $totalsData);
foreach ($totals as $total) {
$this->addTotal($total);
Expand Down
58 changes: 32 additions & 26 deletions app/code/Magento/Weee/Model/Total/Quote/WeeeTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@
*/
namespace Magento\Weee\Model\Total\Quote;

use Magento\Quote\Api\Data\ShippingAssignmentInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Address\Total;
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
use Magento\Store\Model\Store;
use Magento\Tax\Model\Sales\Total\Quote\CommonTaxCollector;

class WeeeTax extends Weee
{
/**
* Collect Weee taxes amount and prepare items prices for taxation and discount
*
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface|\Magento\Quote\Model\Quote\Address $shippingAssignment
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @param Quote $quote
* @param ShippingAssignmentInterface|Address $shippingAssignment
* @param Total $total
* @return $this
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function collect(
\Magento\Quote\Model\Quote $quote,
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
Quote $quote,
ShippingAssignmentInterface $shippingAssignment,
Total $total
) {
\Magento\Quote\Model\Quote\Address\Total\AbstractTotal::collect($quote, $shippingAssignment, $total);
AbstractTotal::collect($quote, $shippingAssignment, $total);
$this->_store = $quote->getStore();
if (!$this->weeeData->isEnabled($this->_store)) {
return $this;
Expand Down Expand Up @@ -65,13 +68,12 @@ public function collect(
$weeeCodeToWeeeTaxDetailsMap[$weeeCode] = $weeeTaxDetails;
}
}

$productTaxes = [];
//Process each item that has taxable weee
foreach ($itemToWeeeCodeMap as $mapping) {
$item = $mapping['item'];

$this->weeeData->setApplied($item, []);
$productTaxes = [];

$totalValueInclTax = 0;
$baseTotalValueInclTax = 0;
Expand Down Expand Up @@ -113,15 +115,17 @@ public function collect(
$baseTotalRowValueExclTax += $baseRowValueExclTax;

$productTaxes[] = [
'title' => $attributeCode, //TODO: fix this
'base_amount' => $baseValueExclTax,
'amount' => $valueExclTax,
'row_amount' => $rowValueExclTax,
'base_row_amount' => $baseRowValueExclTax,
'base_amount_incl_tax' => $baseValueInclTax,
'amount_incl_tax' => $valueInclTax,
'row_amount_incl_tax' => $rowValueInclTax,
'base_row_amount_incl_tax' => $baseRowValueInclTax,
[
'title' => $attributeCode, //TODO: fix this
'base_amount' => $baseValueExclTax,
'amount' => $valueExclTax,
'row_amount' => $rowValueExclTax,
'base_row_amount' => $baseRowValueExclTax,
'base_amount_incl_tax' => $baseValueInclTax,
'amount_incl_tax' => $valueInclTax,
'row_amount_incl_tax' => $rowValueInclTax,
'base_row_amount_incl_tax' => $baseRowValueInclTax,
],
];
}

Expand All @@ -143,8 +147,11 @@ public function collect(
$baseTotalRowValueInclTax
);

$this->weeeData->setApplied($item, array_merge($this->weeeData->getApplied($item), $productTaxes));
}
$this->weeeData->setApplied(
$item,
array_merge($this->weeeData->getApplied($item), ...$productTaxes)
);
}
return $this;
}
Expand Down Expand Up @@ -196,7 +203,7 @@ protected function createItemToWeeeCodeMapping($weeeCodeToItemMap)
/**
* Process row amount based on FPT total amount configuration setting
*
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @param Total $total
* @param float $rowValueExclTax
* @param float $baseRowValueExclTax
* @param float $rowValueInclTax
Expand Down Expand Up @@ -226,14 +233,13 @@ protected function processTotalAmount(
/**
* Fetch the Weee total amount for display in totals block when building the initial quote
*
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @param Quote $quote
* @param Total $total
* @return array
*/
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
public function fetch(Quote $quote, Total $total)
{
/** @var $items \Magento\Sales\Model\Order\Item[] */
$items = isset($total['address_quote_items']) ? $total['address_quote_items'] : [];
$items = $total['quote_items'] ?? [];

$weeeTotal = $this->weeeData->getTotalAmounts($items, $quote->getStore());
if ($weeeTotal) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Weee\Model\Total\Quote;

use Magento\Checkout\Api\Data\TotalsInformationInterface;
use Magento\Checkout\Model\TotalsInformationManagement;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\AddressFactory;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\ObjectManager;
use PHPUnit\Framework\TestCase;

/**
* Quote totals calculate tests class
*/
class CalculateTest extends TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var TotalsInformationManagement
*/
private $totalsManagement;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = ObjectManager::getInstance();
$this->totalsManagement = $this->objectManager->get(TotalsInformationManagement::class);
}

/**
* Multishipping quote with FPT Weee TAX totals calculation test
*
* @magentoDataFixture Magento/Weee/_files/quote_multishipping.php
* @magentoConfigFixture default_store tax/weee/enable 1
* @magentoConfigFixture default_store tax/weee/apply_vat 1
*/
public function testGetWeeTaxTotals()
{
/** @var QuoteFactory $quoteFactory */
$quoteFactory = $this->objectManager->get(QuoteFactory::class);
/** @var QuoteResource $quoteResource */
$quoteResource = $this->objectManager->get(QuoteResource::class);
$quote = $quoteFactory->create();
$quoteResource->load($quote, 'multishipping_fpt_quote_id', 'reserved_order_id');
$cartId = $quote->getId();
$addressFactory = $this->objectManager->get(AddressFactory::class);
/** @var Address $newAddress */
$newAddress = $addressFactory->create()->setAddressType('shipping');
$newAddress->setCountryId('US')->setRegionId(12)->setRegion('California')->setPostcode('90230');
$addressInformation = $this->objectManager->create(
TotalsInformationInterface::class,
[
'data' => [
'address' => $newAddress,
'shipping_method_code' => 'flatrate',
'shipping_carrier_code' => 'flatrate',
],
]
);

$actual = $this->totalsManagement->calculate($cartId, $addressInformation);

$items = $actual->getTotalSegments();
$this->assertTrue(array_key_exists('weee_tax', $items));
$this->assertEquals(25.4, $items['weee_tax']->getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\PaymentInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\Address\Rate;
use Magento\Quote\Model\Quote\Item;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;

/** @var ObjectManager $objectManager */
$objectManager = Bootstrap::getObjectManager();
/** @var QuoteFactory $quoteFactory */
$quoteFactory = Bootstrap::getObjectManager()->get(QuoteFactory::class);
/** @var QuoteResource $quoteResource */
$quoteResource = Bootstrap::getObjectManager()->get(QuoteResource::class);
/** @var StoreManagerInterface $storeManager */
$storeManager = $objectManager->get(StoreManagerInterface::class);
/** @var CartRepositoryInterface $quoteRepository */
$quoteRepository = $objectManager->get(CartRepositoryInterface::class);
$store = $storeManager->getStore();
/** @var Quote $quote */
$quote = $quoteFactory->create();
$quote->setReservedOrderId('multishipping_fpt_quote_id')
->setCustomerEmail('[email protected]')
->setStoreId($storeManager->getStore()->getId());

$shipping = [
'firstname' => 'Jonh',
'lastname' => 'Doe',
'telephone' => '0333-233-221',
'street' => ['Main Division 1'],
'city' => 'Culver City',
'region' => 'CA',
'postcode' => 90800,
'country_id' => 'US',
'email' => '[email protected]',
'address_type' => 'shipping',
];
$methodCode = 'flatrate_flatrate';
/** @var Rate $rate */
$rate = $objectManager->create(Rate::class);
$rate->setCode($methodCode)
->setPrice(5.00);

$address = $objectManager->create(AddressInterface::class, ['data' => $shipping]);
$address->setShippingMethod($methodCode)
->addShippingRate($rate)
->setShippingAmount(5.00)
->setBaseShippingAmount(5.00);

$quote->addAddress($address);

/** @var AddressInterface $address */
$address = $objectManager->create(
AddressInterface::class,
[
'data' => [
'firstname' => 'Jonh',
'lastname' => 'Doe',
'telephone' => '0333-233-221',
'street' => ['Third Division 1'],
'city' => 'New York',
'region' => 'NY',
'postcode' => 10029,
'country_id' => 'US',
'email' => '[email protected]',
'address_type' => 'billing',
],
]
);
$quote->setBillingAddress($address);

$quote->setIsMultiShipping(1);
$quoteRepository->save($quote);

/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
Resolver::getInstance()->requireDataFixture('Magento/Weee/_files/product_with_fpt.php');
/** @var Product $product */
$product = $productRepository->get('simple-with-ftp');

/** @var Item $item */
$item = $objectManager->create(Item::class);
$item->setProduct($product)
->setPrice($product->getPrice())
->setQty(2);
$quote->addItem($item);
$quoteRepository->save($quote);

$addressList = $quote->getAllShippingAddresses();
$address = reset($addressList);
$item->setQty(2);
$address->setTotalQty(2);
$address->addItem($item);
$quoteRepository->save($quote);

$quote = $quoteFactory->create();
$quoteResource->load($quote, 'multishipping_fpt_quote_id', 'reserved_order_id');
/** @var PaymentInterface $payment */
$payment = $objectManager->create(PaymentInterface::class);
$payment->setMethod('checkmo');
$quote->setPayment($payment);
$quote->collectTotals();
$quoteRepository->save($quote);
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;

/** @var ObjectManager $objectManager */
$objectManager = Bootstrap::getObjectManager();

/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
$searchCriteria = $searchCriteriaBuilder
->addFilter('reserved_order_id', 'multishipping_fpt_quote_id')
->create();
/** @var CartRepositoryInterface $quoteRepository */
$quoteRepository = $objectManager->get(CartRepositoryInterface::class);
$quotesList = $quoteRepository->getList($searchCriteria)->getItems();

if (!empty($quotesList)) {
$quote = array_pop($quotesList);
$quoteRepository->delete($quote);
}

Resolver::getInstance()->requireDataFixture('Magento/Weee/_files/product_with_fpt_rollback.php');