Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Order.php
Go to the documentation of this file.
1 <?php
7 
16 
23 class Order extends SalesResource implements OrderResourceInterface
24 {
30  protected $_eventPrefix = 'sales_order_resource';
31 
37  protected $_eventObject = 'resource';
38 
42  protected $stateHandler;
43 
49  protected function _construct()
50  {
51  $this->_init('sales_order', 'entity_id');
52  }
53 
63  public function __construct(
64  \Magento\Framework\Model\ResourceModel\Db\Context $context,
65  Snapshot $entitySnapshot,
66  RelationComposite $entityRelationComposite,
69  StateHandler $stateHandler,
70  $connectionName = null
71  ) {
72  $this->stateHandler = $stateHandler;
73  parent::__construct(
74  $context,
75  $entitySnapshot,
76  $entityRelationComposite,
77  $attribute,
80  );
81  }
82 
91  public function aggregateProductsByTypes($orderId, $productTypeIds = [], $isProductTypeIn = false)
92  {
93  $connection = $this->getConnection();
94  $select = $connection->select()
95  ->from(
96  ['o' => $this->getTable('sales_order_item')],
97  ['o.product_type', new \Zend_Db_Expr('COUNT(*)')]
98  )
99  ->where('o.order_id=?', $orderId)
100  ->where('o.product_id IS NOT NULL')
101  ->group('o.product_type');
102  if ($productTypeIds) {
103  $select->where(
104  sprintf(
105  '(o.product_type %s (?))',
106  $isProductTypeIn ? 'IN' : 'NOT IN'
107  ),
108  $productTypeIds
109  );
110  }
111  return $connection->fetchPairs($select);
112  }
113 
120  protected function calculateItems(\Magento\Sales\Model\Order $object)
121  {
122  $itemsCount = 0;
123  if (!$object->getId()) {
124  foreach ($object->getAllItems() as $item) {
126  $parent = $item->getQuoteParentItemId();
127  if ($parent && !$item->getParentItem()) {
128  $item->setParentItem($object->getItemByQuoteItemId($parent));
129  }
130  $childItems = $item->getChildrenItems();
131  if (empty($childItems)) {
132  $itemsCount++;
133  }
134  }
135  }
136  return $itemsCount;
137  }
138 
143  protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
144  {
145  if (!$object->getId()) {
147  $store = $object->getStore();
148  $name = [
149  $store->getWebsite()->getName(),
150  $store->getGroup()->getName(),
151  $store->getName(),
152  ];
153  $object->setStoreName(implode(PHP_EOL, $name));
154  $object->setTotalItemCount($this->calculateItems($object));
155  }
156  $object->setData(
157  'protect_code',
158  substr(
159  hash('sha256', uniqid(Random::getRandomNumber(), true) . ':' . microtime(true)),
160  5,
161  32
162  )
163  );
164  $isNewCustomer = !$object->getCustomerId() || $object->getCustomerId() === true;
165  if ($isNewCustomer && $object->getCustomer()) {
166  $object->setCustomerId($object->getCustomer()->getId());
167  }
168  return parent::_beforeSave($object);
169  }
170 
174  public function save(\Magento\Framework\Model\AbstractModel $object)
175  {
177  $this->stateHandler->check($object);
178  return parent::save($object);
179  }
180 }
aggregateProductsByTypes($orderId, $productTypeIds=[], $isProductTypeIn=false)
Definition: Order.php:91
static getRandomNumber($min=0, $max=null)
Definition: Random.php:62
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, Snapshot $entitySnapshot, RelationComposite $entityRelationComposite, Attribute $attribute, Manager $sequenceManager, StateHandler $stateHandler, $connectionName=null)
Definition: Order.php:63
_beforeSave(\Magento\Framework\Model\AbstractModel $object)
Definition: AbstractDb.php:653
save(\Magento\Framework\Model\AbstractModel $object)
$connection
Definition: bulk.php:13
if(!isset($_GET['name'])) $name
Definition: log.php:14