Skip to content
Snippets Groups Projects
Select Git revision
  • dev default protected
  • prod protected
  • 1.0.58
  • 1.0.57
  • 1.0.52
  • 1.0.56
  • 1.0.51
  • 1.0.50
  • 1.0.33
  • 1.0.32
  • 1.0.31
  • 1.0.30
  • 1.0.29
  • 1.0.28
  • 1.0.27
  • 1.0.26
  • 1.0.25
  • 1.0.24
  • 1.0.23
  • 1.0.22
  • 1.0.21
  • 1.0.20
22 results

Customshipping.php

Blame
  • Customshipping.php 3.35 KiB
    <?php
    
    namespace uafrica\Customshipping\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;
    
    /**
     * @category   uafrica
     * @package    uafrica_Customshipping
     * @author     info@bob.co.za
     * @website    https://www.bob.co.za
     */
    class Customshipping extends AbstractCarrier implements CarrierInterface
    {
        /**
         * Carrier's code
         *
         * @var string
         */
        protected $_code = 'uafrica';
    
        /**
         * Whether this carrier has fixed rates calculation
         *
         * @var bool
         */
        protected $_isFixed = true;
    
        /**
         * @var ResultFactory
         */
        protected $_rateResultFactory;
    
        /**
         * @var MethodFactory
         */
        protected $_rateMethodFactory;
    
        /**
         * @param ScopeConfigInterface $scopeConfig
         * @param ErrorFactory $rateErrorFactory
         * @param LoggerInterface $logger
         * @param ResultFactory $rateResultFactory
         * @param MethodFactory $rateMethodFactory
         * @param array $data
         */
        public function __construct(
            ScopeConfigInterface $scopeConfig,
            ErrorFactory $rateErrorFactory,
            LoggerInterface $logger,
            ResultFactory $rateResultFactory,
            MethodFactory $rateMethodFactory,
            array $data = []
        ) {
            $this->_rateResultFactory = $rateResultFactory;
            $this->_rateMethodFactory = $rateMethodFactory;
            parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
        }
    
        /**
         * Generates list of allowed carrier`s shipping methods
         * Displays on cart price rules page
         *
         * @return array
         * @api
         */
        public function getAllowedMethods()
        {
            return [$this->getCarrierCode() => __($this->getConfigData('name'))];
        }
    
        /**
         * Collect and get rates for storefront
         *
         * @SuppressWarnings(PHPMD.UnusedFormalParameter)
         * @param RateRequest $request
         * @return DataObject|bool|null
         * @api
         */
        public function collectRates(RateRequest $request)
        {
            /**
             * Make sure that Shipping method is enabled
             */
            if (!$this->isActive()) {
                return false;
            }
    
            /** @var \Magento\Shipping\Model\Rate\Result $result */
            $result = $this->_rateResultFactory->create();
    
            $shippingPrice = $this->getConfigData('price');
    
            $method = $this->_rateMethodFactory->create();
    
            /**
             * Set carrier's method data
             */
            $method->setCarrier($this->getCarrierCode());
            $method->setCarrierTitle($this->getConfigData('title'));
    
            /**
             * Displayed as shipping method under Carrier
             */
            $method->setMethod($this->getCarrierCode());
            $method->setMethodTitle($this->getConfigData('name'));
    
            $method->setPrice($shippingPrice);
            $method->setCost($shippingPrice);
    
            $result->append($method);
    
            return $result;
        }
    
    }