I want to overwrite summary block on cart page. I'm fetching dynamic values from an API, based on zip code user have provided. I need to

  • fetch input value that user entered in zip code box
  • passing this value to the API call
  • getting result and displayed near shipping method name using ajax.

Here I'm trying to integrate dynamic value with flat rate shipping method. The possible problem that may appear is that dynamic value will be inserted into core_config_data table if a user tried to add any product to cart. When any other user gets into cart page, shipping method price that displayed will be same price that previous user had, which is fetched from DB (core_config_data). Any solution for this

  1. To overwrite summary block
  2. To resolve possible issue.

I followed this method to overwrite flat rate shipping method.

  • Anyone who can help me here? Feb 25, 2017 at 4:21

1 Answer 1


You can override the Magento\OfflineShipping\Model\Carrier\Flatrate class by creating a preference in your modules etc/di.xml:

<preference for="Magento\OfflineShipping\Model\Carrier\Flatrate" type="Your\Model\Model\Carrier\Flatrate"/>

The class Your\Model\Model\Carrier\Flatrate would be a copy of Magento\OfflineShipping\Model\Carrier\Flatrate with an alteration made to the getShippingPrice, for example:

private function getShippingPrice(RateRequest $request, $freeBoxes)
    $shippingPrice = false;

    $configPrice = $this->getConfigData('price');
    if ($this->getConfigData('type') === 'O') {
        // per order
        $shippingPrice = $this->itemPriceCalculator->getShippingPricePerOrder($request, $configPrice, $freeBoxes);
    } elseif ($this->getConfigData('type') === 'I') {
        // per item
        $shippingPrice = $this->itemPriceCalculator->getShippingPricePerItem($request, $configPrice, $freeBoxes);

    // alter shipping price based on zip code
    if ($request->getDestPostcode() == 78747) {
        $shippingPrice += 10;

    $shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);

    if ($shippingPrice !== false && (
            $request->getFreeShipping() === true || $request->getPackageQty() == $freeBoxes
    ) {
        $shippingPrice = '0.00';
    return $shippingPrice;
  • Is this working if i typed a zip code in the Zip/Postal Code Block and it will display the price amount fetched from api ? Similar working of ups/FedEx shipping? Feb 25, 2017 at 7:26
  • Yes, it will update the price of the flat rate shipping method when you enter a zip code. Feb 25, 2017 at 7:47
  • 2
    How can I fetch the shipping price value from a controller execute function here? Feb 27, 2017 at 9:11

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.