Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Status.php
Go to the documentation of this file.
1 <?php
8 
10 
19 {
23  protected $_storeManager;
24 
35  public function __construct(
36  \Magento\Framework\Model\Context $context,
37  \Magento\Framework\Registry $registry,
38  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
41  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
42  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
43  array $data = []
44  ) {
45  parent::__construct(
46  $context,
47  $registry,
48  $extensionFactory,
50  $resource,
51  $resourceCollection,
52  $data
53  );
54  $this->_storeManager = $storeManager;
55  }
56 
60  protected function _construct()
61  {
62  $this->_init(\Magento\Sales\Model\ResourceModel\Order\Status::class);
63  }
64 
74  public function assignState($state, $isDefault = false, $visibleOnFront = false)
75  {
77  $resource = $this->_getResource();
78  $resource->beginTransaction();
79  try {
80  $resource->assignState($this->getStatus(), $state, $isDefault, $visibleOnFront);
81  $resource->commit();
82  } catch (\Exception $e) {
83  $resource->rollBack();
84  throw $e;
85  }
86  return $this;
87  }
88 
94  protected function validateBeforeUnassign($state)
95  {
96  if ($this->getResource()->checkIsStateLast($state)) {
97  throw new LocalizedException(
98  __("The last status can't be changed and needs to stay assigned to its current state.")
99  );
100  }
101  if ($this->getResource()->checkIsStatusUsed($this->getStatus())) {
102  throw new LocalizedException(
103  __("The status can't be unassigned because the status is currently used by an order.")
104  );
105  }
106  }
107 
115  public function unassignState($state)
116  {
117  $this->validateBeforeUnassign($state);
118  $this->getResource()->unassignState($this->getStatus(), $state);
119  $this->_eventManager->dispatch(
120  'sales_order_status_unassign',
121  [
122  'status' => $this->getStatus(),
123  'state' => $state
124  ]
125  );
126  return $this;
127  }
128 
134  public function getStoreLabels()
135  {
136  if ($this->hasData('store_labels')) {
137  return $this->_getData('store_labels');
138  }
139  $labels = $this->_getResource()->getStoreLabels($this);
140  $this->setData('store_labels', $labels);
141  return $labels;
142  }
143 
150  public function getStoreLabel($store = null)
151  {
152  $store = $this->_storeManager->getStore($store);
153  $labels = $this->getStoreLabels();
154  if (isset($labels[$store->getId()])) {
155  return $labels[$store->getId()];
156  } else {
157  return __($this->getLabel());
158  }
159  }
160 
167  public function loadDefaultByState($state)
168  {
169  $this->load($state, 'default_state');
170  return $this;
171  }
172 }
$storeManager
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Status.php:35