Customize price in ups shipping method magento 2 plugin

Customizing the price in the UPS shipping method for a Magento 2 plugin involves modifying the shipping rate calculation logic. This typically requires creating a custom module or extending the existing UPS shipping module in Magento 2. Here are the steps to customize the price in the UPS shipping method:

1. Create a Custom Module (if not already created): If you don’t have a custom module in your Magento 2 store, create one. This module will be used to override the UPS shipping method’s behavior.

2. Override the UPS Shipping Method Class: In your custom module, override the UPS shipping method class. To do this, you’ll need to create a custom XML file (di.xml) in your module’s etc folder and configure it to override the UPS shipping method class. Here’s an example of what this XML file might look like:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Ups\Model\Carrier">
        <plugin name="custom_ups_shipping" type="Vendor\Module\Plugin\CustomUpsShipping" sortOrder="10"/>
    </type>
</config>

3. Create the Custom Plugin Class: Create the CustomUpsShipping.php class in your module’s Plugin folder and implement your custom logic to calculate the shipping rate. In this class, you can modify the price based on your specific requirements.

<?php
namespace Vendor\Module\Plugin;

class CustomUpsShipping
{
    public function aroundCollectRates(
        \Magento\Ups\Model\Carrier $subject,
        \Closure $proceed,
        \Magento\Quote\Model\Quote\Address\RateRequest $request
    ) {
        // Call the original method to get the base shipping rates
        $result = $proceed($request);

        // Modify the shipping rates based on your custom logic
        foreach ($result->getAllRates() as $rate) {
            // Modify the rate price here
            $rate->setPrice(NEW_PRICE);
            // You can also set other rate properties if needed
        }

        return $result;
    }
}

3. Clear Cache and Test:
After making these changes, clear the Magento cache and test the checkout process to ensure that the UPS shipping method now calculates the shipping price based on your custom logic.

Remember to replace Vendor\Module with your module’s namespace and name and adjust the logic inside the CustomUpsShipping class to match your specific pricing requirements. Additionally, ensure that your custom module is properly enabled and configured in your Magento 2 store.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *