I have created custom shipping method and working fine.

I want to know what i have to do for my custom shipping method to set price zero when cart price rule become true for free shipping.

For example: I have created "Test cart price rule" and set free shipping when product are from category id 3.

This is working on "flat rate shipping method". but not working on my custom shipping method.

I have created shipping method followed by https://webkul.com/blog/create-custom-shipping-method-in-magento2/

2 Answers 2

public function collectRates(RateRequest $request)
    $result = $this->_rateFactory->create();

     /*this is check is shipping free or not by any rule*/
    if ($request->getFreeShipping() === true) {
        $shippingPrice = 0;
    } else {
        $shippingPrice = 10;

    /*store shipping in session*/
    $method = $this->_rateMethodFactory->create();
    $method->setCarrierTitle('custom Shipping');
    /* Use method name */
    $method->setMethodTitle('Custom Shipping');
    return $result; 

To achieve this functionality first you need to review the flatrate shipping model.


There are 4 main functions which you need to review.

1) Magento\OfflineShipping\Model\Carrier::collectRates
2) Magento\OfflineShipping\Model\Carrier::getFreeBoxesCount
3) Magento\OfflineShipping\Model\Carrier::getFreeBoxesCountFromChildren
4) Magento\OfflineShipping\Model\Carrier::getShippingPrice

Because when you are creating a custom shipping method you need to keep in mind about all product types.

Also this should work with whole cart and particular item type discount.

Basically logic for free shipping is working as below.

  1. when cart rule is applied and product is eligible for the free shipping then Magento set free_shipping flag 1 in the quote_item table.
  2. when collectRates() method call for the shipping price then it ignore items which have free_shipping flag 1 and calculate shipping rate accordingly.

I hope this will help you to achieve your functionality.


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.