Issue
First: I understand how to create a custom module within a customtable in the backend. I had already done this and it works fine and I can save some information with it in the backend.
(Name: app/code/TestModule/Module)
But now I try to customize the order process.
I want to save some data, like the ‘oder_id’ and the ‘order_date’ in a custom table.
This process should work automatically after the order was successful.
Problem:
I don’t understand how I can store this information in a custom table.
I have this in my success.phtml for checking the data in the frontend:
<?php
$block->getOrderId()
$block->getShippingMethod()
?>
The data is called with a function, here in my Block/Onepage/Success.php
...
public function getShippingMethod()
{
return $this->_checkoutSession->getLastRealOrder()->getShippingMethod();
}
...
Well, and now I don’t have any ideas for the next steps. For example, I saw this tut here In magento 2 what is the correct way for getModel? but it does not help me.
Other questions on stackoverflow answer only how to build a module or how to show order information.
I think i have to do something like this:
$this->_Modulename->addData($data);
$this->_transaction->save();
But after 3 days of reading, I try my luck here.
I don’t need a final solution and I don’t want a copy and paste of the answer code, I just won’t understand how this "save data in the custom table after success" works.
I take everything from you guys because I am at the end of my knowledge.
TYIA
Solution
Ok, I have it now.
Some functions like the “order-id” in:
app/code/Modules/Modulename/Block/Onepage/Success.php
<?php
namespace Modules\Modulename\Block\Onepage;
class Success extends \Magento\Checkout\Block\Onepage\Success
{
public function getEntityId()
{
return $this->_checkoutSession->getLastRealOrder()->getEntityId();
}
}
The following save-function
is in one file: 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');
create connection DB
$connection = $resource->getConnection();
Create query and fetch data from database:
$tableName = $resource->getTableName('sales_order_item');
$sql = "Select * FROM ". $tableName. " WHERE order_id =". $order_id;
$result = $connection->fetchAll($sql);
And now we get them with a foreach. The “$n” is the count for all products and the “$productQty” is for each product qty.
Example:
ordered:
2 x red ball
1 x green bottle
So you have to count for all products (2 (bottle and ball)) and for each product qty (2x ball, 1x bottle). I hope it is clear what i mean 😉
// 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);
}
}
I build this “save-function” to save each ordered product in a custom table.
Answered By – Irv
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0