Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CarrierFactory.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Shipping\Model;
7 
12 {
18  protected $_scopeConfig;
19 
23  protected $_objectManager;
24 
29  public function __construct(
30  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
32  ) {
33  $this->_scopeConfig = $scopeConfig;
34  $this->_objectManager = $objectManager;
35  }
36 
43  public function get($carrierCode)
44  {
45  $className = $this->_scopeConfig->getValue(
46  'carriers/' . $carrierCode . '/model',
47  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
48  );
49  if (!$className) {
50  return false;
51  }
52  $carrier = $this->_objectManager->get($className);
53  $carrier->setId($carrierCode);
54  return $carrier;
55  }
56 
64  public function create($carrierCode, $storeId = null)
65  {
66  $className = $this->_scopeConfig->getValue(
67  'carriers/' . $carrierCode . '/model',
68  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
69  $storeId
70  );
71  if (!$className) {
72  return false;
73  }
74  $carrier = $this->_objectManager->create($className);
75  $carrier->setId($carrierCode);
76  if ($storeId) {
77  $carrier->setStore($storeId);
78  }
79  return $carrier;
80  }
81 
88  public function getIfActive($carrierCode)
89  {
90  return $this->_scopeConfig->isSetFlag(
91  'carriers/' . $carrierCode . '/active',
92  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
93  ) ? $this->get(
94  $carrierCode
95  ) : false;
96  }
97 
105  public function createIfActive($carrierCode, $storeId = null)
106  {
107  return $this->_scopeConfig->isSetFlag(
108  'carriers/' . $carrierCode . '/active',
109  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
110  $storeId
111  ) ? $this->create(
112  $carrierCode,
113  $storeId
114  ) : false;
115  }
116 }
$objectManager
Definition: bootstrap.php:17
create($carrierCode, $storeId=null)
createIfActive($carrierCode, $storeId=null)
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\ObjectManagerInterface $objectManager)
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31