Same Day Shipping

Extension Feature :
Option to enable same day delivery.
Add dynamic holiday dates to exclude from delivery.
Enable with a limited time interval.
Option to disable with Weekend.

Create Module With namespace Ap and Name with Shipping.

Step 1 :
Ap\Shipping\etc\config.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <carriers>
            <ap_shipping>
                <active>0</active>
                <sallowspecific>0</sallowspecific>
                <model>Ap\Shipping\Model\Carrier\Shipping</model>
                <name>Same Day Shipping</name>
                <price>2.00</price>
                <title>Same Day Shipping</title>
                <specific_error_msg>This shipping method is not available. To use this shipping method, please contact us.</specific_error_msg>
            </ap_shipping>
        </carriers>
    </default>
</config>


Step 2:
Ap\Shipping\etc\adminhtml\system.xml
<?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Config/etc/system_file.xsd">
  <system>
   <section id="carriers" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
    <group id="ap_shipping" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
     <label>Ap Shipping Module</label>
     <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
      <label>Enabled</label>
      <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
     </field>

     <field id="title" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Title</label>
     </field>

     <field id="name" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Method Name</label>
     </field>
                                        
                                        <field id="aptimefrom" translate="label" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Time Interval From</label>
     </field>
                                        
                                        <field id="aptimeto" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Time Interval To</label>
     </field>
                                        
                                        <field id="holidays" translate="label" type="text" sortOrder="6" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Holidays</label>
     </field>
                                        
                                         <field id="deliveryday" translate="label" type="text" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Allowed Days For Delivery</label>
     </field>

     <field id="price" translate="label" type="text" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="0">
      <label>Shipping Cost</label>
      <validate>validate-number validate-zero-or-greater</validate>
     </field>

     <field id="specificerrmsg" translate="label" type="textarea" sortOrder="80" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Displayed Error Message</label>
     </field>

     <field id="sallowspecific" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="1" showInStore="0">
      <label>Ship to Applicable Countries</label>
      <frontend_class>shipping-applicable-country</frontend_class>
      <source_model>Magento\Shipping\Model\Config\Source\Allspecificcountries</source_model>
     </field>

     <field id="specificcountry" translate="label" type="multiselect" sortOrder="91" showInDefault="1" showInWebsite="1" showInStore="0">
      <label>Ship to Specific Countries</label>
      <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
      <can_be_empty>1</can_be_empty>
     </field>

     <field id="showmethod" translate="label" type="select" sortOrder="92" showInDefault="1" showInWebsite="1" showInStore="0">
      <label>Show Method if Not Applicable</label>
      <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
     </field>

     <field id="sort_order" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="0">
      <label>Sort Order</label>
     </field>
    </group>
   </section>
  </system>
 </config>


Step 3:
Ap\Shipping\Model\Carrier\Shipping.php
namespace Ap\Shipping\Model\Carrier;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;
use Magento\Shipping\Model\Config;
use Magento\Shipping\Model\Rate\ResultFactory;
use Magento\Store\Model\ScopeInterface;
use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory;
use Magento\Quote\Model\Quote\Address\RateResult\Method;
use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Psr\Log\LoggerInterface;
 
class Shipping extends AbstractCarrier implements CarrierInterface
{
 protected $_code = 'ap_shipping';
 protected $_isFixed = true;
 protected $_rateResultFactory;
 protected $_rateMethodFactory;
        protected $_dateTime;
        protected $_timezone;
 
 public function __construct(
 ScopeConfigInterface $scopeConfig,
 ErrorFactory $rateErrorFactory,
 LoggerInterface $logger,
 ResultFactory $rateResultFactory,
 MethodFactory $rateMethodFactory,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
        \Magento\Framework\Stdlib\DateTime $dateTime,        
 array $data = []
 )
 
 {
  $this->_rateResultFactory = $rateResultFactory;
  $this->_rateMethodFactory = $rateMethodFactory;
                $this->_dateTime = $dateTime;
                $this->_timezone = $timezone;
  parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
 }
 
 public function getAllowedMethods()
 {
  return [$this->getCarrierCode() => __($this->getConfigData('name'))];
 }
 
 public function collectRates(RateRequest $request)
 {
            if (!$this->isActive())
            {
                    return false;
            }

            $date = $this->_timezone->date();
            $toDayDate =  $this->_timezone->date($date)->format('Y-m-d');
            $holidays = explode(",",$this->getConfigData('holidays'));
            // If Current Date Matches with holidays date
            if(in_array($toDayDate, $holidays))
            {
               return;
            }
            
            // Check deliveryday
            $dayName=$this->_timezone->date($date)->format('D'); 
            if(!in_array($dayName, explode(",",$this->getConfigData('deliveryday'))))
            {
               return;
            }
            
            // Check current time between timeinterval from and to
            $currentTime = $this->_timezone->date($date)->format('H'); 
            if((int)$currentTime > (int)$this->getConfigData('aptimefrom') && (int)$currentTime < (int)$this->getConfigData('aptimeto'))
            {
                $result = $this->_rateResultFactory->create();

                $shippingPrice = $this->getConfigData('price');

                $method = $this->_rateMethodFactory->create();

                $method->setCarrier($this->getCarrierCode());
                $method->setCarrierTitle($this->getConfigData('title'));

                $method->setMethod($this->getCarrierCode());
                $method->setMethodTitle($this->getConfigData('name'));

                $method->setPrice($shippingPrice);
                $method->setCost($shippingPrice);

                $result->append($method);
                return $result;
            }
            else
            {
                return; 
            }
            
 }
}

Now Go to Stores => Configuration => Sales => Shipping Method Your Custom shipping Method is ready to use.  

Comments