From 9727f74b1ada6f8564ff1111427d7d5381d8ca57 Mon Sep 17 00:00:00 2001 From: Gytautas Date: Tue, 20 Feb 2024 15:11:25 +0200 Subject: [PATCH 1/2] initial price rules fix --- dpdbaltics.php | 11 ++++++-- src/Repository/ZoneRepository.php | 28 +++++++++++++++++++ .../ShippingPriceCalculationService.php | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/dpdbaltics.php b/dpdbaltics.php index e22b3855..893deca8 100644 --- a/dpdbaltics.php +++ b/dpdbaltics.php @@ -464,13 +464,18 @@ public function getOrderShippingCostExternal($cart) $currentCountryProvider = $this->getModuleContainer('invertus.dpdbaltics.provider.current_country_provider'); $deliveryAddress = new Address($cart->id_address_delivery); + $carrier = new Carrier($this->id_carrier); + +// if (empty($zoneRepository->findZoneInRangeByAddress($deliveryAddress))) { +// return false; +// } + + $availableDpdProductsByZone = $zoneRepository->findProductsInZoneRange($deliveryAddress, $carrier->id_reference); - if (empty($zoneRepository->findZoneInRangeByAddress($deliveryAddress))) { + if (empty($availableDpdProductsByZone)) { return false; } - $carrier = new Carrier($this->id_carrier); - if (!$productAvailabilityService->checkIfCarrierIsAvailable($carrier->id_reference)) { return false; } diff --git a/src/Repository/ZoneRepository.php b/src/Repository/ZoneRepository.php index a26f3d77..04effa9b 100644 --- a/src/Repository/ZoneRepository.php +++ b/src/Repository/ZoneRepository.php @@ -114,4 +114,32 @@ public function findZoneInRangeByAddress(Address $address) return $result ?: []; } + + public function findProductsInZoneRange(Address $address, string $carrierReference) + { + $idCountry = $address->id_country ?: (int)\Configuration::get('PS_COUNTRY_DEFAULT'); + $zipCode = $address->postcode; + + $query = new DbQuery(); + $query->select('dp.*'); + $query->from('dpd_zone', 'dz'); + $query->leftJoin('dpd_zone_range', 'dzr', 'dzr.id_dpd_zone = dz.id_dpd_zone'); + $query->leftJoin('dpd_product_zone', 'dpz', 'dpz.id_dpd_zone = dz.id_dpd_zone'); + $query->leftJoin('dpd_product', 'dp', 'dp.id_dpd_product = dpz.id_dpd_product'); + + $query->where('dp.active = ' . (int) 1); + $query->where('dzr.id_country = ' . (int)$idCountry); + $query->where('dzr.include_all_zip_codes = 1 OR (dzr.zip_code_from <= \'' . pSQL($zipCode) . '\' AND dzr.zip_code_to >= \'' . pSQL($zipCode) . '\')'); + + $productsIdReferences = $this->db->executeS($query); + $result = []; + + foreach($productsIdReferences as $product) { + if ($product['id_reference'] === $carrierReference) { + $result[] = $product['id_reference']; + } + } + + return $result ?: []; + } } diff --git a/src/Service/ShippingPriceCalculationService.php b/src/Service/ShippingPriceCalculationService.php index 1bbc8e3f..fff6b5cd 100644 --- a/src/Service/ShippingPriceCalculationService.php +++ b/src/Service/ShippingPriceCalculationService.php @@ -53,7 +53,7 @@ public function __construct( public function calculate(Cart $cart, \Carrier $carrier, Address $deliveryAddress) { $shippingCosts = 0.0; - + //todo debug here why prices are not shown correctly $priceRulesIds = $this->priceRuleRepository->getByCarrierReference( $deliveryAddress, $carrier->id_reference, From 82f9354c21c46364967905dd2ed88617668472dc Mon Sep 17 00:00:00 2001 From: Gytautas Date: Thu, 29 Feb 2024 15:25:05 +0200 Subject: [PATCH 2/2] price rule fix --- src/Repository/ZoneRepository.php | 16 +++++++++++++++- src/Service/ShippingPriceCalculationService.php | 5 ++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Repository/ZoneRepository.php b/src/Repository/ZoneRepository.php index 04effa9b..9bf2ba95 100644 --- a/src/Repository/ZoneRepository.php +++ b/src/Repository/ZoneRepository.php @@ -120,6 +120,20 @@ public function findProductsInZoneRange(Address $address, string $carrierReferen $idCountry = $address->id_country ?: (int)\Configuration::get('PS_COUNTRY_DEFAULT'); $zipCode = $address->postcode; + $query = new DbQuery(); + + $query->select('dp.*'); + $query->from('dpd_product', 'dp'); + $query->where('dp.id_reference = ' . pSql($carrierReference)); + $query->where('dp.active = ' . (int) 1); + $query->where('dp.all_zones = ' . (int) 1); + + $result = $this->db->executeS($query); + + if (!empty($result)) { + return $result; + } + $query = new DbQuery(); $query->select('dp.*'); $query->from('dpd_zone', 'dz'); @@ -129,7 +143,7 @@ public function findProductsInZoneRange(Address $address, string $carrierReferen $query->where('dp.active = ' . (int) 1); $query->where('dzr.id_country = ' . (int)$idCountry); - $query->where('dzr.include_all_zip_codes = 1 OR (dzr.zip_code_from <= \'' . pSQL($zipCode) . '\' AND dzr.zip_code_to >= \'' . pSQL($zipCode) . '\')'); + $query->where('dzr.include_all_zip_codes = 1 OR (dzr.zip_code_from_numeric <= \'' . pSQL($zipCode) . '\' AND dzr.zip_code_to_numeric >= \'' . pSQL($zipCode) . '\')'); $productsIdReferences = $this->db->executeS($query); $result = []; diff --git a/src/Service/ShippingPriceCalculationService.php b/src/Service/ShippingPriceCalculationService.php index fff6b5cd..7151814e 100644 --- a/src/Service/ShippingPriceCalculationService.php +++ b/src/Service/ShippingPriceCalculationService.php @@ -53,7 +53,10 @@ public function __construct( public function calculate(Cart $cart, \Carrier $carrier, Address $deliveryAddress) { $shippingCosts = 0.0; - //todo debug here why prices are not shown correctly + //todo price rules do not exist if setting is all price rule exist + // if prz all zones = 1 + // check if carrier set to all zones if yes we take price rule of the all_zones + $priceRulesIds = $this->priceRuleRepository->getByCarrierReference( $deliveryAddress, $carrier->id_reference,