skip to Main Content

I managed to create my custom shipping method as flatrate with a custom module the shipping costs 5$.

I want to make it free if the cart total is more than 30$ for example.

Is there anyway to do this ?

I have no clue from where to start so I have no code to try please help.

Thanks !

2

Answers


  1. Chosen as BEST ANSWER

    Found it, In case someone needs this :

    In my shipping.php file there is a function called collectRates which is reponsible for returning the shipping rates, I added

        if($request->getBaseSubtotalInclTax()>100){
            $method->setPrice(0);
        }
    

    Basically If the total is more than 100$ I make the shipping price free, It would be better to make the 100$ a dynamic value which can be changed in the shipping method settings I will do it later.

    this is my collectRates Function in the end:

     * @param RateRequest $request
    
     * @return bool|Result
    
     */
    
    public function collectRates(RateRequest $request)
    
    {
        if (!$this->getConfigFlag('active')) {
            return false;
        }
    
        /** @var MagentoShippingModelRateResult $result */
    
        $result = $this->_rateResultFactory->create();
    
        /** @var MagentoQuoteModelQuoteAddressRateResultMethod $method */
    
        $method = $this->_rateMethodFactory->create();
        $method->setCarrier($this->_code);
        $method->setCarrierTitle($this->getConfigData('title'));
        $method->setMethod($this->_code);
        $method->setMethodTitle($this->getConfigData('name'));
        $amount = $this->getShippingPrice();
        $method->setPrice($amount);
        if($request->getBaseSubtotalInclTax()>100){
            $method->setPrice(0);
        }
        $method->setCost($amount);
        $result->append($method);
        return $result;
    
    }
    

  2. You can set up Cart Rules to do this in your admin.

    https://www.aitoc.com/blog/configuring-free-shipping-in-magento-2/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search