Adding Custom Shipping Cost to Magento Checkout Total
In Magento, adding an extra cost to your checkout total involves modifying the shipping method calculation process. You’ll need to create a custom shipping carrier that includes your additional price logic.
Step 1: Create a New Shipping Carrier Module
First, create a new module in app/code/Vendor/CustomShipping. This module will contain your custom shipping carrier class.
Example module structure:
/app/code/Vendor/CustomShipping/
âââ etc/
â âââ module.xml
âââ registration.php
Step 2: Implement the Shipping Carrier Interface
Create a file app/code/Vendor/CustomShipping/etc/module.xml to define your new shipping carrier:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_CustomShipping" setup_version="1.0.0">
<sequence>
<module name="Magento_Shipping"/>
</sequence>
</module>
</config>
Next, create the file app/code/Vendor/CustomShipping/registration.php:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_CustomShipping',
__DIR__
);
Step 3: Implement Custom Shipping Carrier Logic
Create a file app/code/Vendor/CustomShipping/etc/carrier.xml to configure your new carrier:
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ConfigurableProduct:etc/schema.xsd">
<carriers>
<vendor_customshipping>
<class>Vendor\CustomShipping\Model\Carrier</class>
<disabled>1</disabled>
<order_status>processing</order_status>
<sort_order>0</sort_order>
<title translate="true">Custom Shipping</title>
<active>0</active>
</vendor_customshipping>
</carriers>
</config>
Finally, create the carrier file app/code/Vendor/CustomShipping/Model/Carrier.php:
namespace Vendor\CustomShipping\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\AbstractCarrierOnlineAdapter;
class Carrier extends AbstractCarrier
{
const XML_PATH_SHIPPING_METHODS_ENABLED = 'carriers/vendor_customshipping/active';
const XML_PATH_COST_PER_ITEM = 'carriers/vendor_customshipping/cost_per_item';
protected $_code = 'vendor_customshipping';
public function getAllowedMethods()
{
return [$this->_code];
}
/**
* @param RateRequest $request
* @return array|false
*/
public function collectRates(RateRequest $request)
{
if (!$this->isActive()) {
return false;
}
$result = new DataObject();
$ shippingCost = $this->getShippingPrice();
// You can add your custom logic here to calculate the additional price
$customFieldValue = 10; // Replace this with the value from your custom field
$shippingCost += $customFieldValue;
$result->setCode($this->_code)
->setCarrierTitle($this->getConfigData('title'))
->setMethodTitle($this->getConfigData('name'))
->setPrice($shippingCost)
->setErrorMessage(__('Method is temporarily unavailable.'));
return [$result];
}
protected function getShippingPrice()
{
// You can add your shipping price logic here
$costPerItem = (float)$this->getConfigData(self::XML_PATH_COST_PER_ITEM);
$totalCost = 0;
foreach ($this->getItems() as $item) {
$totalCost += $costPerItem * $item['qty'];
}
return $totalCost;
}
// Implement other required methods like getAllowedMethods
}
Step 4: Update the Checkout Configuration
Update your checkout configuration to include your new carrier. You can follow the Magento documentation for more details.
Conclusion
Adding an extra price to the total cart amount in Magento involves creating a custom shipping carrier module and implementing the required logic in the collectRates method. This allows you to add your additional cost directly to the shipping method calculation.