深入分析order對(duì)象的save方法執(zhí)行的操作

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市瞳步,隨后出現(xiàn)的幾起案子闷哆,更是在濱河造成了極大的恐慌,老刑警劉巖单起,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件抱怔,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡嘀倒,警方通過查閱死者的電腦和手機(jī)屈留,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門局冰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人灌危,你說我怎么就攤上這事康二。” “怎么了乍狐?”我有些...
    開封第一講書人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵赠摇,是天一觀的道長(zhǎng)固逗。 經(jīng)常有香客問我浅蚪,道長(zhǎng),這世上最難降的妖魔是什么烫罩? 我笑而不...
    開封第一講書人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任惜傲,我火速辦了婚禮,結(jié)果婚禮上贝攒,老公的妹妹穿的比我還像新娘盗誊。我一直安慰自己,他們只是感情好隘弊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開白布哈踱。 她就那樣靜靜地躺著,像睡著了一般梨熙。 火紅的嫁衣襯著肌膚如雪开镣。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評(píng)論 1 302
  • 那天咽扇,我揣著相機(jī)與錄音邪财,去河邊找鬼。 笑死质欲,一個(gè)胖子當(dāng)著我的面吹牛树埠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播嘶伟,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼怎憋,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了九昧?” 一聲冷哼從身側(cè)響起盛霎,我...
    開封第一講書人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎耽装,沒想到半個(gè)月后愤炸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡掉奄,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年规个,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了凤薛。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡诞仓,死狀恐怖缤苫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情墅拭,我是刑警寧澤活玲,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站谍婉,受9級(jí)特大地震影響舒憾,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜穗熬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一镀迂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧唤蔗,春花似錦探遵、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至棍掐,卻和暖如春藏雏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背塌衰。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工诉稍, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人最疆。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓杯巨,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親努酸。 傳聞我的和親對(duì)象是個(gè)殘疾皇子服爷,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)获诈,斷路器仍源,智...
    卡卡羅2017閱讀 134,656評(píng)論 18 139
  • 原文地址:http://magenticians.com/12-design-patterns-magento M...
    jimxu閱讀 1,174評(píng)論 0 1
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 3,811評(píng)論 0 11
  • 用戶下單未付款,用戶付款后申請(qǐng)取消舔涎,...笼踩,這些場(chǎng)景時(shí)我們需要取消magento中生成的訂單,取消的方式如下: 這...
    jimxu閱讀 646評(píng)論 1 0
  • 今日亡嫌,和朋友去逛街嚎于,又買了一堆衣服掘而,都說女人永遠(yuǎn)都差一件衣服,我以為自己不是這樣的女子于购,沒想到更是袍睡。不可理解的是買...
    周小周007閱讀 618評(píng)論 5 4