Skip to content

Commit 9d666cc

Browse files
author
Serhii Balko
committed
MC-41069: Free Shipping enables with wrong total for DHL
1 parent adc70c4 commit 9d666cc

File tree

2 files changed

+105
-9
lines changed

2 files changed

+105
-9
lines changed

app/code/Magento/Shipping/Model/Carrier/AbstractCarrierOnline.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,13 +648,23 @@ public function setRawRequest($request)
648648
*/
649649
public function getMethodPrice($cost, $method = '')
650650
{
651+
if ($method == $this->getConfigData(
652+
$this->_freeMethod
653+
) && $this->getConfigFlag(
654+
'free_shipping_enable'
655+
) && $this->getConfigData(
656+
'free_shipping_subtotal'
657+
) <= $this->_request->getBaseSubtotalWithDiscountInclTax()) {
658+
return '0.00';
659+
}
660+
651661
return $method == $this->getConfigData(
652662
$this->_freeMethod
653663
) && $this->getConfigFlag(
654664
'free_shipping_enable'
655665
) && $this->getConfigData(
656666
'free_shipping_subtotal'
657-
) <= $this->_rawRequest->getBaseSubtotalInclTax() ? '0.00' : $this->getFinalPriceWithHandlingFee(
667+
) <= $this->_request->getBaseSubtotalWithDiscountInclTax() ? '0.00' : $this->getFinalPriceWithHandlingFee(
658668
$cost
659669
);
660670
}

dev/tests/integration/testsuite/Magento/Dhl/Model/CarrierTest.php

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,7 @@ private function getExpectedLabelRequestXml(
443443
public function testCollectRates()
444444
{
445445
$requestData = $this->getRequestData();
446-
//phpcs:disable Magento2.Functions.DiscouragedFunction
447-
$response = new Response(
448-
200,
449-
[],
450-
file_get_contents(__DIR__ . '/../_files/dhl_quote_response.xml')
451-
);
452-
//phpcs:enable Magento2.Functions.DiscouragedFunction
453-
$this->httpClient->nextResponses(array_fill(0, Carrier::UNAVAILABLE_DATE_LOOK_FORWARD + 1, $response));
446+
$this->setNextResponse(__DIR__ . '/../_files/dhl_quote_response.xml');
454447
/** @var RateRequest $request */
455448
$request = Bootstrap::getObjectManager()->create(RateRequest::class, $requestData);
456449
$expectedRates = [
@@ -563,6 +556,80 @@ private function setDhlConfig(array $params)
563556
}
564557
}
565558

559+
/**
560+
* Tests that the free rate is returned when sending a quotes request
561+
*
562+
* @param array $addRequestData
563+
* @param bool $freeShippingExpects
564+
* @magentoConfigFixture default_store carriers/dhl/active 1
565+
* @magentoConfigFixture default_store carriers/dhl/id some ID
566+
* @magentoConfigFixture default_store carriers/dhl/shipment_days Mon,Tue,Wed,Thu,Fri,Sat
567+
* @magentoConfigFixture default_store carriers/dhl/intl_shipment_days Mon,Tue,Wed,Thu,Fri,Sat
568+
* @magentoConfigFixture default_store carriers/dhl/allowed_methods IE
569+
* @magentoConfigFixture default_store carriers/dhl/international_service IE
570+
* @magentoConfigFixture default_store carriers/dhl/gateway_url https://xmlpi-ea.dhl.com/XMLShippingServlet
571+
* @magentoConfigFixture default_store carriers/dhl/id some ID
572+
* @magentoConfigFixture default_store carriers/dhl/password some password
573+
* @magentoConfigFixture default_store carriers/dhl/content_type N
574+
* @magentoConfigFixture default_store carriers/dhl/nondoc_methods 1,3,4,8,P,Q,E,F,H,J,M,V,Y
575+
* @magentoConfigFixture default_store carriers/dhl/showmethod' => 1,
576+
* @magentoConfigFixture default_store carriers/dhl/title DHL Title
577+
* @magentoConfigFixture default_store carriers/dhl/specificerrmsg dhl error message
578+
* @magentoConfigFixture default_store carriers/dhl/unit_of_measure K
579+
* @magentoConfigFixture default_store carriers/dhl/size 1
580+
* @magentoConfigFixture default_store carriers/dhl/height 1.6
581+
* @magentoConfigFixture default_store carriers/dhl/width 1.6
582+
* @magentoConfigFixture default_store carriers/dhl/depth 1.6
583+
* @magentoConfigFixture default_store carriers/dhl/debug 1
584+
* @magentoConfigFixture default_store carriers/dhl/free_method_nondoc P
585+
* @magentoConfigFixture default_store carriers/dhl/free_shipping_enable 1
586+
* @magentoConfigFixture default_store carriers/dhl/free_shipping_subtotal 25
587+
* @magentoConfigFixture default_store shipping/origin/country_id GB
588+
* @magentoAppIsolation enabled
589+
* @dataProvider collectRatesWithFreeShippingDataProvider
590+
*/
591+
public function testCollectRatesWithFreeShipping(array $addRequestData, bool $freeShippingExpects): void
592+
{
593+
$this->setNextResponse(__DIR__ . '/../_files/dhl_quote_response.xml');
594+
595+
$requestData = $this->getRequestData();
596+
$requestData['data'] += $addRequestData;
597+
/** @var RateRequest $request */
598+
$request = Bootstrap::getObjectManager()->create(RateRequest::class, $requestData);
599+
600+
$actualRates = $this->dhlCarrier->collectRates($request)->getAllRates();
601+
$freeRateExists = false;
602+
foreach ($actualRates as $i => $actualRate) {
603+
$actualRate = $actualRate->getData();
604+
if ($actualRate['method'] === 'P' && $actualRate['price'] === 0.0) {
605+
$freeRateExists = true;
606+
}
607+
}
608+
609+
self::assertEquals($freeShippingExpects, $freeRateExists);
610+
}
611+
612+
/**
613+
* @return array
614+
*/
615+
public function collectRatesWithFreeShippingDataProvider(): array
616+
{
617+
return [
618+
[
619+
['base_subtotal_incl_tax' => 25, 'base_subtotal_with_discount_incl_tax' => 22],
620+
false
621+
],
622+
[
623+
['base_subtotal_incl_tax' => 25, 'base_subtotal_with_discount_incl_tax' => 25],
624+
true
625+
],
626+
[
627+
['base_subtotal_incl_tax' => 28, 'base_subtotal_with_discount_incl_tax' => 25],
628+
true
629+
],
630+
];
631+
}
632+
566633
/**
567634
* Returns request data.
568635
*
@@ -614,4 +681,23 @@ private function getRequestData(): array
614681
]
615682
];
616683
}
684+
685+
/**
686+
* Set next response content from file
687+
*
688+
* @param string $file
689+
*/
690+
private function setNextResponse(string $file): void
691+
{
692+
//phpcs:disable Magento2.Functions.DiscouragedFunction
693+
$response = new Response(
694+
200,
695+
[],
696+
file_get_contents($file)
697+
);
698+
//phpcs:enable Magento2.Functions.DiscouragedFunction
699+
$this->httpClient->nextResponses(
700+
array_fill(0, Carrier::UNAVAILABLE_DATE_LOOK_FORWARD + 1, $response)
701+
);
702+
}
617703
}

0 commit comments

Comments
 (0)