20170330149085600641699.png
保存訂單對(duì)象時(shí)除了更新sales_flat_order表厉熟,還會(huì):
- _beforeSave方法
- _afterSave方法
- order_save_after事件
before save
- 檢查state
- 設(shè)置店名
- 設(shè)置訂單號(hào)
magento自帶的通過對(duì)象的方式生成訂單號(hào)的做法影響下單時(shí)的性能刃鳄,可以通過redis的方式自動(dòng)生成訂單號(hào)
- 設(shè)置item數(shù)量
- 設(shè)置用戶id
- 設(shè)置賬單地址
- 設(shè)置物流地址
protected function _beforeSave()
{
parent::_beforeSave();//觸發(fā)model_save_before和core_abstract_save_before事件
$this->_checkState();//檢查state滥崩,注1
if (!$this->getId()) {//新建訂單設(shè)置store名稱
$store = $this->getStore();
$name = array($store->getWebsite()->getName(),$store->getGroup()->getName(),$store->getName());
$this->setStoreName(implode("\n", $name));
}
if (!$this->getIncrementId()) {//沒有訂單號(hào)
$incrementId = Mage::getSingleton('eav/config')
->getEntityType('order')
->fetchNewIncrementId($this->getStoreId());//生成訂單號(hào),注2
$this->setIncrementId($incrementId);//設(shè)置訂單號(hào)
}
/**
* Process items dependency for new order
*/
if (!$this->getId()) {
$itemsCount = 0;
foreach ($this->getAllItems() as $item) {
$parent = $item->getQuoteParentItemId();
if ($parent && !$item->getParentItem()) {
$item->setParentItem($this->getItemByQuoteItemId($parent));
} elseif (!$parent) {
$itemsCount++;
}
}
// Set items count
$this->setTotalItemCount($itemsCount);
}
if ($this->getCustomer()) {
$this->setCustomerId($this->getCustomer()->getId());
}
if ($this->hasBillingAddressId() && $this->getBillingAddressId() === null) {
$this->unsBillingAddressId();
}
if ($this->hasShippingAddressId() && $this->getShippingAddressId() === null) {
$this->unsShippingAddressId();
}
$this->setData('protect_code', substr(md5(uniqid(mt_rand(), true) . ':' . microtime(true)), 5, 6));
return $this;
}
其中,checkState方法有一個(gè)注意事項(xiàng):
如果訂單實(shí)付金額為0(使用了優(yōu)惠券等情況下)牌借,訂單的state不是canceled拔稳,也不是complete诡宗,那么就會(huì)被置為closed
protected function _checkState()
{
if (!$this->getId()) {//新建的訂單下面的代碼不會(huì)生效
return $this;
}
$userNotification = $this->hasCustomerNoteNotify() ? $this->getCustomerNoteNotify() : null;//通知顧客
if (!$this->isCanceled()
&& !$this->canUnhold()
&& !$this->canInvoice()
&& !$this->canShip()) {
if (0 == $this->getBaseGrandTotal() || $this->canCreditmemo()) {
if ($this->getState() !== self::STATE_COMPLETE) {
$this->_setState(self::STATE_COMPLETE, true, '', $userNotification);
}
}
/**
* Order can be closed just in case when we have refunded amount.
* In case of "0" grand total order checking ForcedCanCreditmemo flag
*/
elseif (floatval($this->getTotalRefunded()) || (!$this->getTotalRefunded()
&& $this->hasForcedCanCreditmemo())
) {
if ($this->getState() !== self::STATE_CLOSED) {
$this->_setState(self::STATE_CLOSED, true, '', $userNotification);
}
}
}
if ($this->getState() == self::STATE_NEW && $this->getIsInProcess()) {
$this->setState(self::STATE_PROCESSING, true, '', $userNotification);
}
return $this;
}
after save
protected function _afterSave()
{
if (null !== $this->_addresses) {
$this->_addresses->save();
$billingAddress = $this->getBillingAddress();
$attributesForSave = array();
if ($billingAddress && $this->getBillingAddressId() != $billingAddress->getId()) {
$this->setBillingAddressId($billingAddress->getId());
$attributesForSave[] = 'billing_address_id';
}
$shippingAddress = $this->getShippingAddress();
if ($shippingAddress && $this->getShippigAddressId() != $shippingAddress->getId()) {
$this->setShippingAddressId($shippingAddress->getId());
$attributesForSave[] = 'shipping_address_id';
}
if (!empty($attributesForSave)) {
$this->_getResource()->saveAttribute($this, $attributesForSave);
}
}
if (null !== $this->_items) {
$this->_items->save();
}
if (null !== $this->_payments) {
$this->_payments->save();
}
if (null !== $this->_statusHistory) {
$this->_statusHistory->save();
}
foreach ($this->getRelatedObjects() as $object) {
$object->save();
}
return parent::_afterSave(); //觸發(fā)model_save_after和core_abstract_save_after事件
}
監(jiān)控sales_order_save_after事件
2017033014908558291593.png