1

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

4
  • You can use webshopapps matrixrate which provides you the options you needed. This is the extension url: marketplace.magento.com/webshopapps-module-matrixrate.html Sep 4 '18 at 7:37
  • I tried this one out but earlier, seems way too behind for actual working. highly recommended not to use this
    – Gayashan
    Sep 4 '18 at 12:16
  • I think matrixrate perfect is this can you share your csv which we use in matrixrate Sep 4 '18 at 12:33
  • @PrashantBarot I tried the mentioned module, the problem I'm facing is flat rate I set up from backend includes no shipping cost at " Order summary"
    – Gayashan
    Sep 6 '18 at 8:49
1

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;
    }
}
5
  • That really seems to be working. but if you could little bit more clarify the logic where the conditions are met true. plus this not a discount but a fixed rate for the store till the store exists alive.
    – Gayashan
    Sep 4 '18 at 12:25
  • @Gayashan yes this is not a discount I have just copy pasted the code from one of my custom module to give you with an idea about how you can remove the shipping rate . I guess you can get a cart total and check the conditions .
    – Mac
    Sep 4 '18 at 12:39
  • please check the updated code .
    – Mac
    Sep 4 '18 at 13:11
  • I tried the module on my local project. no luck "Sorry, no quotes are available for this order at this time" this error persists
    – Gayashan
    Sep 5 '18 at 7:53
  • @Gayashan please check this link hope it will help you - help.onestepcheckout.com/knowledge_base/topics/shipping-methods also check magento.stackexchange.com/questions/149586/…
    – Mac
    Sep 6 '18 at 15:05
0

I'm not familiar with the module you've mentioned but this is something you can do using Magento 2's table rates feature.

Step 1: add the cities manually to the end of directory_country_region table with ID, 'COUNTRYCODE','REGIONCODE','Region/city name' format for example 999, 'US','KS','Kansas' .

Step 2: enable table rate: Store -> Configuration -> Store view:Main Website -> Sales -> Shipping Method -> Table Rates:

  • Enabled: Yes
  • Condition: Price vs Destination

Export CSV to download the stock table rate. Edit it.

Ar01: for each city from 1 to 15, you need to add 2 lines:

COUNTRYCODE,REGIONCODE,*,0,L
COUNTRYCODE,REGIONCODE,*,X,0

This translates to: pay L for shipping to REGION if total is between 0 and X, free shipping ( fee = 0 ) if total is higher than X.

Ar02: add one line:

COUNTRYCODE,*,*,0,L

( For every other regions in COUNTRY, shipping is fixed at L )

Step 3: upload the edited csv using Import button, edit Title and Method name for store view, then Save Config -> Refresh cache.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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