Skip to content

Commit

Permalink
feat: add delivery fee (#872)
Browse files Browse the repository at this point in the history
* feat: add delivery fee

* feat: add delivery fee

* feat: add delivery fee

* feat: add delivery fee

* fix: calculate delivery fee before options

* refactor: clearer part name constants

* fix comments

* fix comments

* fix xml error

* fix: prevent double settingfee calculation

---------

Co-authored-by: Joeri van Veen <[email protected]>
  • Loading branch information
GravendeelJochem and joerivanveen authored Nov 25, 2024
1 parent 8a219ad commit 64a7c2b
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 23 deletions.
5 changes: 4 additions & 1 deletion Model/Quote/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ private function getDeliveryData(?string $packageType = null): array
}

$basePrice = $this->helper->getBasePrice();
$deliveryFee = $this->helper->getMethodPrice($carrierPath, 'delivery/delivery_fee', false);
$mondayFee = $canHaveMonday ? $this->helper->getMethodPrice($carrierPath, 'delivery/monday_fee') : 0;
$morningFee = $canHaveMorning ? $this->helper->getMethodPrice($carrierPath, 'morning/fee') : 0;
$eveningFee = $canHaveEvening ? $this->helper->getMethodPrice($carrierPath, 'evening/fee') : 0;
Expand Down Expand Up @@ -253,7 +254,8 @@ private function getDeliveryData(?string $packageType = null): array
'priceCollect' => $collectFee,
'priceReceiptCode' => $receiptCodeFee,
'priceOnlyRecipient' => $onlyRecipientFee,
'priceStandardDelivery' => $showTotalPrice ? $basePrice : 0,
'priceStandardDelivery' => $showTotalPrice ? ($basePrice + $deliveryFee) : $deliveryFee,
'priceDeliveryFee' => $deliveryFee,
'priceMondayDelivery' => $mondayFee,
'priceMorningDelivery' => $morningFee,
'priceEveningDelivery' => $eveningFee,
Expand Down Expand Up @@ -463,4 +465,5 @@ private function isPickupAllowed(string $carrier): bool

return ! $this->package->deliveryOptionsDisabled && $pickupEnabled && $showPickup;
}

}
45 changes: 23 additions & 22 deletions Model/Rate/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@

class Result extends \Magento\Shipping\Model\Rate\Result
{
private const FIRST_PART = 0;
private const SECOND_PART = 1;
private const THIRD_PART = 2;
private const FOURTH_PART = 3;
private const CARRIERS_WITH_MAILBOX = [
private const CARRIER_PATH = 0;
private const DELIVERY_PART = 1;
private const FIRST_SHIPPING_OPTION = 2;
private const SECOND_SHIPPING_OPTION = 3;
private const CARRIERS_WITH_MAILBOX = [
CarrierPostNL::NAME,
CarrierDHLForYou::NAME,
CarrierDPD::NAME,
Expand Down Expand Up @@ -156,6 +156,7 @@ public static function getMethods(): array
'digital_stamp' => 'digital_stamp',
'same_day_delivery' => 'delivery/same_day_delivery',
'same_day_delivery_only_recipient' => 'delivery/only_recipient/same_day_delivery',
'delivery_fee' => 'delivery/delivery_fee',
];
}

Expand Down Expand Up @@ -356,57 +357,57 @@ private function getPrice($settingPath): float
$settingFee = 0;

// Explode settingPath like: myparcelnl_magento_postnl_settings/delivery/only_recipient/signature
$settingPath = explode('/', $settingPath ?? '');
$settingPathParts = explode('/', $settingPath ?? '');

// Check if the selected delivery options are delivery, only_recipient and signature
// delivery/only_recipient/signature
if (isset($settingPath[self::THIRD_PART], $settingPath[self::FOURTH_PART]) && 'delivery' === $settingPath[self::SECOND_PART]) {
if (isset($settingPathParts[self::FIRST_SHIPPING_OPTION], $settingPathParts[self::SECOND_SHIPPING_OPTION]) && 'delivery' === $settingPathParts[self::DELIVERY_PART]) {
$settingFee += (float) $this->myParcelHelper->getConfigValue(
sprintf(
"%s/%s/%s_fee",
$settingPath[self::FIRST_PART],
$settingPath[self::SECOND_PART],
$settingPath[self::THIRD_PART]
$settingPathParts[self::CARRIER_PATH],
$settingPathParts[self::DELIVERY_PART],
$settingPathParts[self::FIRST_SHIPPING_OPTION]
)
);
$settingFee += (float) $this->myParcelHelper->getConfigValue(
sprintf(
"%s/%s/%sfee",
$settingPath[self::FIRST_PART],
$settingPath[self::SECOND_PART],
$settingPath[self::FOURTH_PART]
$settingPathParts[self::CARRIER_PATH],
$settingPathParts[self::DELIVERY_PART],
$settingPathParts[self::SECOND_SHIPPING_OPTION]
)
);
}

// Check if the selected delivery is morning or evening and select the fee
if (AbstractConsignment::DELIVERY_TYPE_MORNING_NAME === $settingPath[self::SECOND_PART] || AbstractConsignment::DELIVERY_TYPE_EVENING_NAME === $settingPath[self::SECOND_PART]) {
if (AbstractConsignment::DELIVERY_TYPE_MORNING_NAME === $settingPathParts[self::DELIVERY_PART] || AbstractConsignment::DELIVERY_TYPE_EVENING_NAME === $settingPathParts[self::DELIVERY_PART]) {
$settingFee = (float) $this->myParcelHelper->getConfigValue(
sprintf("%s/%s/fee", $settingPath[self::FIRST_PART], $settingPath[self::SECOND_PART])
sprintf("%s/%s/fee", $settingPathParts[self::CARRIER_PATH], $settingPathParts[self::DELIVERY_PART])
);

// change delivery type if there is a signature selected
if (isset($settingPath[self::FOURTH_PART])) {
$settingPath[self::SECOND_PART] = 'delivery';
if (isset($settingPathParts[self::SECOND_SHIPPING_OPTION])) {
$settingPathParts[self::DELIVERY_PART] = 'delivery';
}
// Unset only_recipient to select the correct price
unset($settingPath[self::THIRD_PART]);
unset($settingPathParts[self::FIRST_SHIPPING_OPTION]);
}

$settingFee += (float) $this->myParcelHelper->getConfigValue(implode('/', $settingPath ?? []) . 'fee');
$settingFee += (float) $this->myParcelHelper->getConfigValue(implode('/', $settingPathParts ?? []) . 'fee');

// For mailbox and digital stamp the base price should not be calculated
if (AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME === $settingPath[self::SECOND_PART]) {
if (AbstractConsignment::PACKAGE_TYPE_MAILBOX_NAME === $settingPathParts[self::DELIVERY_PART]) {
// for international mailbox, we have a different price :-)
$cc = $this->session->getQuote()->getShippingAddress()->getCountryId();
if ($cc !== 'NL') {
$settingFee = (float) $this->myParcelHelper->getConfigValue(
sprintf("%s/%s/international_fee", $settingPath[self::FIRST_PART], $settingPath[self::SECOND_PART])
sprintf("%s/%s/international_fee", $settingPathParts[self::CARRIER_PATH], $settingPathParts[self::DELIVERY_PART])
);
}
return min($settingFee, $basePrice);
}
if (AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME === $settingPath[self::SECOND_PART]){
if (AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME === $settingPathParts[self::DELIVERY_PART]){
return min($settingFee, $basePrice);
}

Expand Down
56 changes: 56 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@
<label>Delivery enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="delivery_fee" translate="label comment" type="text" sortOrder="305" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Delivery fee</label>
<validate>validate-number</validate>
<comment>Enter an amount that is either positive or negative. For example, do you want to give a discount for using this function or do you want to charge extra for this delivery option.</comment>
<tooltip>This will be added to or subtracted from the regular shipping price</tooltip>
<depends>
<field id="active">1</field>
</depends>
</field>
<field id="monday_active" translate="label comment" type="select" sortOrder="306" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Monday delivery</label>
Expand Down Expand Up @@ -722,6 +731,15 @@
<label>Delivery enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="delivery_fee" translate="label comment" type="text" sortOrder="305" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Delivery fee</label>
<validate>validate-number</validate>
<comment>Enter an amount that is either positive or negative. For example, do you want to give a discount for using this function or do you want to charge extra for this delivery option.</comment>
<tooltip>This will be added to or subtracted from the regular shipping price</tooltip>
<depends>
<field id="active">1</field>
</depends>
</field>
<field id="signature_active" translate="label comment" type="select" sortOrder="310" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Signature on receipt</label>
Expand Down Expand Up @@ -1003,6 +1021,15 @@
<label>Delivery enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="delivery_fee" translate="label comment" type="text" sortOrder="305" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Delivery fee</label>
<validate>validate-number</validate>
<comment>Enter an amount that is either positive or negative. For example, do you want to give a discount for using this function or do you want to charge extra for this delivery option.</comment>
<tooltip>This will be added to or subtracted from the regular shipping price</tooltip>
<depends>
<field id="active">1</field>
</depends>
</field>
</group>
<group id="drop_off_days" translate="label comment" type="text" sortOrder="250" showInDefault="1"
showInWebsite="1" showInStore="1">
Expand Down Expand Up @@ -1169,6 +1196,15 @@
<label>Delivery enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="delivery_fee" translate="label comment" type="text" sortOrder="305" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Delivery fee</label>
<validate>validate-number</validate>
<comment>Enter an amount that is either positive or negative. For example, do you want to give a discount for using this function or do you want to charge extra for this delivery option.</comment>
<tooltip>This will be added to or subtracted from the regular shipping price</tooltip>
<depends>
<field id="active">1</field>
</depends>
</field>
</group>
<group id="drop_off_days" translate="label comment" type="text" sortOrder="250" showInDefault="1"
showInWebsite="1" showInStore="1">
Expand Down Expand Up @@ -1342,6 +1378,15 @@
<label>Delivery enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="delivery_fee" translate="label comment" type="text" sortOrder="305" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Delivery fee</label>
<validate>validate-number</validate>
<comment>Enter an amount that is either positive or negative. For example, do you want to give a discount for using this function or do you want to charge extra for this delivery option.</comment>
<tooltip>This will be added to or subtracted from the regular shipping price</tooltip>
<depends>
<field id="active">1</field>
</depends>
</field>
</group>
<group id="drop_off_days" translate="label comment" type="text" sortOrder="250" showInDefault="1"
showInWebsite="1" showInStore="1">
Expand Down Expand Up @@ -1509,14 +1554,24 @@
<label>Delivery enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>

<field id="delivery_fee" translate="label comment" type="text" sortOrder="305" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Delivery fee</label>
<validate>validate-number</validate>
<comment>Enter an amount that is either positive or negative. For example, do you want to give a discount for using this function or do you want to charge extra for this delivery option.</comment>
<tooltip>This will be added to or subtracted from the regular shipping price</tooltip>
</field>

<field id="signature_active" translate="label comment" type="select" sortOrder="310" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Signature on receipt</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>

<depends>
<field id="active">1</field>
</depends>
</field>

<field id="signature_fee" translate="label comment" type="text" sortOrder="311" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Signature on receipt fee</label>
Expand Down Expand Up @@ -1561,6 +1616,7 @@
<field id="only_recipient_active">1</field>
</depends>
</field>

</group>
<group id="drop_off_days" translate="label comment" type="text" sortOrder="250" showInDefault="1"
showInWebsite="1" showInStore="1">
Expand Down
6 changes: 6 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<large_format_active>No</large_format_active>
<large_format_from_price>1</large_format_from_price>
<age_check_active>0</age_check_active>
<delivery_fee>0</delivery_fee>
</default_options>
<general>
<deliverydays_window>14</deliverydays_window>
Expand Down Expand Up @@ -119,6 +120,7 @@
<insurance_local_amount>0</insurance_local_amount>
<insurance_belgium_amount>0</insurance_belgium_amount>
<insurance_percentage>0</insurance_percentage>
<delivery_fee>0</delivery_fee>
</default_options>
<general>
<deliverydays_window>14</deliverydays_window>
Expand Down Expand Up @@ -156,6 +158,7 @@
<insurance_eu_amount>0</insurance_eu_amount>
<insurance_row_amount>0</insurance_row_amount>
<insurance_percentage>0</insurance_percentage>
<delivery_fee>0</delivery_fee>
</default_options>
<general>
<deliverydays_window>14</deliverydays_window>
Expand All @@ -176,6 +179,7 @@
<insurance_eu_amount>0</insurance_eu_amount>
<insurance_row_amount>0</insurance_row_amount>
<insurance_percentage>0</insurance_percentage>
<delivery_fee>0</delivery_fee>
</default_options>
<general>
<deliverydays_window>14</deliverydays_window>
Expand All @@ -196,6 +200,7 @@

<myparcelnl_magento_ups_settings>
<default_options>
<delivery_fee>0</delivery_fee>
</default_options>
<general>
<deliverydays_window>3</deliverydays_window>
Expand All @@ -212,6 +217,7 @@

<myparcelnl_magento_dpd_settings>
<default_options>
<delivery_fee>0</delivery_fee>
</default_options>
<general>
<deliverydays_window>3</deliverydays_window>
Expand Down
1 change: 1 addition & 0 deletions i18n/fr_FR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Saturday,Samedi
Sunday,Dimanche
Automate 'Large format',Automatiser "Plus grand que 100 x 70 x 58 cm ou plus lourd que 23 kg"
Large package,Plus grand que 100 x 70 x 58 cm ou plus lourd que 23 kg
Delivery fee,Frais de livraison

{field} is required.,{field} est requis.
Address not found.,Adresse introuvable.
Expand Down
1 change: 1 addition & 0 deletions i18n/nl_NL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ This is the type of weight that I use with my products.,Dit is de eenheid die je
Default,Standaard
No,Nee
Yes,Ja
Delivery fee,Bezorgkosten

{field} is required.,{field} is verplicht.
Address not found.,Dit adres is niet gevonden.
Expand Down
1 change: 1 addition & 0 deletions view/frontend/web/js/view/delivery-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ define(
*/
methodCodeDeliveryOptionsConfigMap: {
'myparcelnl_magento_postnl_settings/delivery': 'config.carrierSettings.postnl.priceStandardDelivery',
'myparcelnl_magento_postnl_settings/delivery_fee': 'config.carrierSettings.postnl.priceDeliveryFee',
'myparcelnl_magento_postnl_settings/mailbox': 'config.carrierSettings.postnl.pricePackageTypeMailbox',
'myparcelnl_magento_postnl_settings/package_small': 'config.carrierSettings.postnl.pricePackageTypePackageSmall',
'myparcelnl_magento_postnl_settings/digital_stamp': 'config.carrierSettings.postnl.pricePackageTypeDigitalStamp',
Expand Down

0 comments on commit 64a7c2b

Please sign in to comment.