Magento – M2 Free shipping if subtotal is higher than X amount

databasejavascriptmagento2PHP

I am developing M2 store for a company that just serves only for domestic users. I recently bought the Amasty Inventory module and made the domestic cities.(No shipping available to the countries different) )
I have two logical pattern where I have no idea to start off with..

Ar01
If total cart price is greater than X amount, free delivery available within city 1 to city 15,
even if the cart price is lower than X amount(in city 1 to city 15) that he will have to pay L amount(A fixed rate).

Ar02
even if the cart price greater or lower than X amount if he's not entitled the upon city list(city 1 to city 15) that he'll have to pay for shipping L amount(The fixed rate]

I am using Magento 2.2.2

if someone has an idea how to start off this module please let me know in real quick

Best Answer

What I understand from your question is you wanted to set shipping amount to 0 if some specific conditions matches .

You can override a collect method in your custom module and can set shipping amount to 0 off course after checking some conditions .

create some custom module Custom/Shippingamount

add registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Custom_Shippingamount',
    __DIR__
);

add module.xml file inside Custom/Shippingamount/etc directoy

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Custom_Shippingamount" setup_version="0.0.1"/>
</config>

add sales.xml file inside Custom/Shippingamount/etc directoy

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
    <section name="quote">
        <group name="totals">
            <item name="shipping_amount" instance="Custom\Shippingamount\Model\RemoveShippingPrice" sort_order="510"/>
        </group>
    </section>
</config>

now create RemoveShippingPrice.php file inside Custom/Shippingamount/Model directory where we are going to set the shipping price to 0 .

      <?php


namespace Custom\Shippingamount\Model;


use \Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
use \Magento\Quote\Model\Quote;
use \Magento\Quote\Api\Data\ShippingAssignmentInterface;
use \Magento\Quote\Model\Quote\Address\Total;
use \Magento\Checkout\Model\Cart;


class RemoveShippingPrice extends AbstractTotal
{

    protected  $cart;

    public function __construct(Cart $cart)
    {
        $this->cart = $cart;
    }

    /**
     *
     * @param Quote                       $quote              quote
     * @param ShippingAssignmentInterface $shippingAssignment shipping
     * @param Total                       $total              total
     *
     * @return $this
     */
    public function collect(
        Quote $quote,
        ShippingAssignmentInterface $shippingAssignment,
        Total $total
    ) {
        parent::collect($quote, $shippingAssignment, $total);

        /**
         * you can check your conditions
         * here or add some logic when to
         * make the shipping amount to 0
         */


        $subTotal = $this->cart->getQuote()->getSubtotal();

       //you can check here subtotal is greater than or less than your x amount 

        $amount = $quote->getShippingAddress()->getShippingAmount();
        $total->setShippingAmount(0);
        $total->addTotalAmount($this->getCode(), -$amount);
        $total->addBaseTotalAmount($this->getCode(), -$amount);

        return $this;
    }

    /**
     * This function will fetch the quote details
     *
     * @param Quote $quote quote
     * @param Total $total total
     *
     * @return array|null
     */
    public function fetch(Quote $quote, Total $total)
    {
        $result = null;
        $amount = $total->getDiscountAmount();
        if ($amount != 0) {
            $description = $total->getDiscountDescription();
            $result = [
                'code' => $this->getCode(),
                'title' => $description,
                'value' => $amount
            ];
        }
        return $result;
    }
}
Related Topic