4

Is there any way to override Shipping Price by Special User (Customer Service Team) when they place an order for the customer.

I want to add a text box for the Custom Shipping price which will override existing shipping cost to quote when user will go to the next step.

enter image description here

For Example, I want to make 46.53 to 40.

4
  • is this shipping method coming from one carrier?
    – Amit Bera
    Commented Oct 5, 2018 at 7:41
  • Yes amit. It’s coming from one carrier Commented Oct 5, 2018 at 12:12
  • can you please share yours carrier method code?
    – Amit Bera
    Commented Oct 8, 2018 at 6:30
  • Can you specify which shipping method you want to change rate? Commented Oct 9, 2018 at 13:52

2 Answers 2

2

First, I think you should set the price to depend on the customer inputed.

with function setPrice of \Magento\Quote\Model\Quote\Address\RateResult\Method

The solution is plugin to Magento\Quote\Model\Quote\Address\RateResult\Method and change value of price before set to data['price'];

di.xml

<type name="Magento\Quote\Model\Quote\Address\RateResult\Method">
        <plugin name="namespace_change_price_of_shipping_rate" type="NameSpace\Module\Plugin\Model\Quote\Address\RateResult\Method"/>
</type>

NameSpace\Module\Plugin\Model\Quote\Address\RateResult\Method

namespace NameSpace\Module\Plugin\Model\Quote\Address\RateResult;

class Method
{

      protected $_customerSession;
    public function __construct(
        \Magento\Customer\Model\Session $customerSession)
    {
        $this->_customerSession = $customerSession;
    }


    /**
     * Round shipping carrier's method price
     *
     * @param string|float|int $price
     * @return $this
     */
    public function beforeSetPrice($subject, $price)
    {
        // @TODO returun inputed price
        return $price;
    }


    public function beforeSetCost($subject, $price)
    {
        // @TODO returun inputed price.
        return $price;
    }

    public function beforeSetData($subject, $key, $value){
        if('price' == $key || 'cost' == $key){
                //@TODO set inputed price to value
        }
        return [$key, $value];
    }
}

2: Edit your vendor/magento/module-checkout/view/frontend/web/template/shipping-address/shipping-method-item.html:16 file to Add an input for input custom price

<input type="text" attr="'value': customPrice "/>

3: Edit shipping javascript file vendor/magento/module-checkout/view/frontend/web/js/view/shipping.js to add a function that appends custom price to shipping data.

    selectShippingMethod: function (shippingMethod) {
        selectShippingMethodAction(shippingMethod);
        checkoutData.setSelectedShippingRate(shippingMethod['carrier_code'] + '_' + shippingMethod['method_code']);
        /** @TODO store custom input data to somewhere 
         * for  NameSpace\Module\Plugin\Model\Quote\Address\RateResult\Method 
         * can get and set to value of variable **price** */
        return true;
    },
1
1

You need to do code like this. We take example that, you need to price into carrier called "Tablerate".

  1. we need to create di.xml at following location.

[Vendor]/[Extension]/etc/di.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <preference for="Magento\OfflineShipping\Model\Carrier\Tablerate" type="Vendor\Extension\Model\Carrier\Tablerate" />
    </config>
  1. create our logic file here.

[Vendor]/[Extension]/Model/Carrier/Tablerate.php

<?php
namespace Vendor\Extension\Model\Carrier;

use Magento\Quote\Model\Quote\Address\RateRequest;

class Tablerate extends \Magento\OfflineShipping\Model\Carrier\Tablerate
{
    public function collectRates(RateRequest $request)
    {
        //Do your logic before this line and return your custom price.
        $method->setPrice($shippingPrice);
    }
}    
1
  • I want to add input field in admin for shipping price and apply that to order , how can i do that Commented Feb 5, 2021 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.