2016-08-19 58 views
1

第一條:我瞭解如何在後臺的自定義表中創建自定義模塊。我已經做到了這一點,它工作正常,我可以在後端保存一些信息。 (名稱:app/code/TestModule/Module)Magento 2 - 如何在定製表成功訂單後保存數據

但現在我嘗試自定義訂單流程。 我想保存一些數據,如'oder_id'和'order_date'在自定義表中。

該過程應該在訂單成功後自動工作。

問題: 我不明白我如何可以將這些信息存儲在自定義表中。

我有這個在我的success.phtml在前端檢查數據:

<?php 
$block->getOrderId() 
$block->getShippingMethod() 
?> 

數據在我座/ Onepage/Success.php

調用一個函數,在這裏
... 

public function getShippingMethod() 
{ 
    return $this->_checkoutSession->getLastRealOrder()->getShippingMethod(); 
} 

... 

好吧,現在我對接下來的步驟沒有任何想法。例如:我在這裏看到這個嘖嘖In magento 2 what is the correct way for getModel?,但它不幫助我。

有關stackflow的其他問題只回答如何構建模塊或如何顯示訂單信息。

我認爲我必須做這樣的事情:

$this->_Modulename->addData($data); 
    $this->_transaction->save(); 

但經過閱讀3天,我在這裏試試我的運氣。 我不需要最終的解決方案,我不想複製和粘貼答案代碼,我只是想了解如何「保存成功後自定義表格中的數據」的作品。

我把你們所有的事情都拿走了,因爲我知道了自己的知識。

TYIA

+0

你看過Magento中的事件觀察者嗎?他們確實存在這種問題 - 您可以定義一個函數,以便在訂單被保存時觸發,然後以您認爲合適的任何方式對訂單數據進行操作。在這種情況下,您只需檢查訂單是否是新的,如果是,請將必要的數據複製到自定義表格中。由於您建議您需要order_id,因此您需要使用sales_order_save_after事件。 http://devdocs.magento.com/guides/v2.0/extension-dev-guide/events-and-observers.html –

+0

嗨,謝謝你的信息。那麼,我明白你的意思,但我不知道如何以這種方式使用'sales_order_save_after事件'。請給我舉個例子嗎? – Irv

回答

0

好的,我現在有。

像 「訂單ID」 的一些功能:

應用程序/代碼/模塊/模塊名/ 座/ Onepage /成功。PHP

<?php 
namespace Modules\Modulename\Block\Onepage; 

class Success extends \Magento\Checkout\Block\Onepage\Success 
{ 
    public function getEntityId() 
    { 
     return $this->_checkoutSession->getLastRealOrder()->getEntityId(); 
    } 
} 

以下save-function是在一個文件中:app/code/Modules/Modulename/view/frontend/templates/succes.phtml

// get order-id (the EntityId) ### 
    $order_id = $block->getEntityId(); 

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); 

創建連接DB

$connection = $resource->getConnection(); 

創建查詢,並從數據庫中提取數據:

$tableName = $resource->getTableName('sales_order_item'); 
    $sql = "Select * FROM ". $tableName. " WHERE order_id =". $order_id; 
    $result = $connection->fetchAll($sql); 

現在我們用foreach得到它們。 「$ n」是所有產品的計數,「$ productQty」是每個產品的數量。 例子:

下令:

2個紅球

1個綠瓶子

所以,你必須對所有的產品(2(瓶和球))和各產品數量(2個球,1個瓶子)。我希望這是明確的我的意思;)

// do this for all products (in my case 2 times) 
    $n = 0; 
    foreach ($result as $allOptionkey) { 

    $allOptionkey = array(unserialize($result[$n]['product_options'])); 
    $productTitle = $result[$n]['name']; 

     foreach ($allOptionkey as $keys) { 
      $product = $keys['info_buyRequest']; 
      $options = $keys['options']; 

      $productKeyArray = array('0' => $product); 

      foreach ($productKeyArray as $productKey => $productVar) { 
       $productName = $productVar['product']; 
       // get "product qty" 
       $productQty = $productVar['qty']; 
      } 
     } 
    $qn=0; 
    $n++; 
     // do this for each product qty (im my case 2x ball and 1 bottle) 
     while($productQty > $qn) { 
       $qncounter = $qn++; 

       $tableModulename = $resource->getTableName('table_name'); 
       $sqlInsert = "Insert Into ".$tableModulename." (options,email) Values ('".$productTitle."', '".$email."')"; 
      $connection->query($sqlInsert); 
     } 
    } 

我建立這個「保存功能」,以保存每個訂購的產品在自定義表。

相關問題