Advertisement
Guest User

Forkel\CustomShipping\Helper

a guest
Aug 29th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.61 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Forkel CustomShipping
  4.  *
  5.  * @category    Forkel
  6.  * @package     Forkel_CustomShipping
  7.  * @copyright   Copyright (c) 2017 Tobias Forkel (http://www.tobiasforkel.de)
  8.  * @license     http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  9.  */
  10.  
  11. namespace Forkel\CustomShipping\Helper;
  12.  
  13. class Config extends \Magento\Framework\App\Helper\AbstractHelper
  14. {
  15.  
  16.     /**
  17.      * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  18.      */
  19.     protected $_timezone;
  20.  
  21.     /**
  22.      * @var \Magento\Framework\App\Config\ScopeConfigInterface
  23.      */
  24.     protected $_scopeConfig;
  25.  
  26.     /**
  27.      * @var Session
  28.      */
  29.     protected $_session;
  30.     protected $_authSession;
  31.  
  32.     /**
  33.      * @param \Magento\Customer\Model\Session $session
  34.      * @var String
  35.      */
  36.     protected $_tab = 'carriers';
  37.  
  38.     /**
  39.      * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
  40.      * @param \Magento\Customer\Model\Session $session
  41.      */
  42.     public function __construct(
  43.         \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
  44.         \Magento\Customer\Model\Session $session,
  45.         \Magento\Backend\Model\Auth\Session $authSession,
  46.         \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  47.     )
  48.     {
  49.         $this->_timezone = $timezone;
  50.         $this->_session = $session;
  51.         $this->_authSession = $authSession;
  52.         $this->_scopeConfig = $scopeConfig;
  53.     }
  54.  
  55.     /**
  56.      * Get module configuration values from core_config_data
  57.      *
  58.      * @param $setting
  59.      * @return mixed
  60.      */
  61.     public function getConfig($setting)
  62.     {
  63.         return $this->_scopeConfig->getValue(
  64.             $this->_tab . '/forkel_customshipping/' . $setting,
  65.             \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  66.         );
  67.     }
  68.  
  69.     /**
  70.      * Get different values from core_config_data and decide if custom shipping method is available.
  71.      *
  72.      * @return boolean
  73.      */
  74.     public function isAvailable()
  75.     {
  76.  
  77.         $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/log.log'  );
  78.         $this->logger = new \Zend\Log\Logger();
  79.         $this->logger->addWriter($writer);
  80.         $this->logger->info(__METHOD__ . " begin ");
  81.  
  82.         $date_current = strtotime($this->_timezone->formatDate());
  83.         $date_start = strtotime($this->getConfig('date_start'));
  84.         $date_end = strtotime($this->getConfig('date_end'));
  85.         $frequency = $this->getConfig('frequency');
  86.         $day = strtolower(date('D', $date_current));
  87.  
  88.         /**
  89.          * Check if shipping method is actually enabled
  90.          */
  91.         if (!$this->getConfig('active'))
  92.         {
  93.             return false;
  94.         }
  95.  
  96.         /**
  97.          * Check if shipping method should be available for logged in users only
  98.          */
  99.         if ($this->getConfig('customer') && !$this->isCustomerLoggedIn()) {
  100.             return false;
  101.         }
  102.  
  103.         /**
  104.          * Check if shipping method should be visible in backend, frontend or both
  105.          */
  106.         if ( !($this->getConfig('availability') == 'backend'
  107.             && $this->isAdmin()) || ($this->getConfig('availability') == 'frontend' && !$this->isAdmin()) ) {
  108.  
  109.             $this->logger->info(__METHOD__ . " bad  backend/frontend + isAdmin ");            
  110.  
  111.             return false;
  112.         }
  113.  
  114.         /**
  115.          * Check if scheduler is enabled
  116.          */
  117.         if ($this->getConfig('scheduler_enabled')) {
  118.  
  119.             /**
  120.              * Check if shipping method should be visible at current name of day
  121.              */
  122.             if (strpos($frequency, $day) === false) {
  123.                 return false;
  124.             }
  125.  
  126.             /**
  127.              * Check if start date is in range
  128.              */
  129.             if ($date_start && $date_start > $date_current)
  130.             {
  131.                 return false;
  132.             }
  133.  
  134.             /**
  135.              * Check if start end is in range
  136.              */
  137.             if ($date_end && $date_end < $date_current)
  138.             {
  139.                 return false;
  140.             }
  141.         }
  142.  
  143.         $this->logger->info(__METHOD__ . " is available ");
  144.        
  145.         return true;
  146.     }
  147.  
  148.     /**
  149.      * Check if current user logged in as admin
  150.      *
  151.      * @return bool
  152.      */
  153.     protected function isAdmin()
  154.     {
  155.         return 'adminhtml' === $this->_session->getAreaCode() || $this->_authSession->getUser()->getEmail();
  156.     }
  157.  
  158.     /**
  159.      * Check if current user logged in
  160.      *
  161.      * @return bool
  162.      */
  163.     protected function isCustomerLoggedIn() {
  164.  
  165.         return $this->_session->isLoggedIn();
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement