Magento 2 - 如何在自定义订单成功后保存数据 table
Magento 2 - How to save data after the order was successfull in a custom table
首先: 我了解如何在后端的自定义table 中创建自定义模块。我已经这样做了,它工作正常,我可以在后端用它保存一些信息。
(姓名:app/code/TestModule/Module)
但现在我尝试自定义订单流程。
我想保存一些数据,比如 'oder_id' 和自定义 table 中的 'order_date'。
此过程应在订单成功后自动运行。
问题:
我不明白如何在自定义 table.
中存储此信息
我的 success.phtml 中有这个用于检查前端的数据:
<?php
$block->getOrderId()
$block->getShippingMethod()
?>
数据是用函数调用的,在我这里Block/Onepage/Success.php
...
public function getShippingMethod()
{
return $this->_checkoutSession->getLastRealOrder()->getShippingMethod();
}
...
好吧,现在我对接下来的步骤没有任何想法。例如,我在这里看到了这个图 但它对我没有帮助。
Whosebug 上的其他问题仅回答如何构建模块或如何显示订单信息。
我想我必须做这样的事情:
$this->_Modulename->addData($data);
$this->_transaction->save();
但是看了3天之后,我在这里碰碰运气。
我不需要最终解决方案,也不需要答案代码的复制和粘贴,我只是不明白这个“成功后在自定义 table 中保存数据”是如何工作的。
我从你们那里拿走了一切,因为我已经学不会了。
蒂亚
好的,我现在有了。
某些功能如 "order-id" 中的:
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();
}
}
以下 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');
创建连接数据库
$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);
}
}
我构建此 "save-function" 以将每个订购的产品保存在自定义 table 中。
首先: 我了解如何在后端的自定义table 中创建自定义模块。我已经这样做了,它工作正常,我可以在后端用它保存一些信息。 (姓名:app/code/TestModule/Module)
但现在我尝试自定义订单流程。 我想保存一些数据,比如 'oder_id' 和自定义 table 中的 'order_date'。
此过程应在订单成功后自动运行。
问题: 我不明白如何在自定义 table.
中存储此信息我的 success.phtml 中有这个用于检查前端的数据:
<?php
$block->getOrderId()
$block->getShippingMethod()
?>
数据是用函数调用的,在我这里Block/Onepage/Success.php
...
public function getShippingMethod()
{
return $this->_checkoutSession->getLastRealOrder()->getShippingMethod();
}
...
好吧,现在我对接下来的步骤没有任何想法。例如,我在这里看到了这个图
Whosebug 上的其他问题仅回答如何构建模块或如何显示订单信息。
我想我必须做这样的事情:
$this->_Modulename->addData($data);
$this->_transaction->save();
但是看了3天之后,我在这里碰碰运气。 我不需要最终解决方案,也不需要答案代码的复制和粘贴,我只是不明白这个“成功后在自定义 table 中保存数据”是如何工作的。
我从你们那里拿走了一切,因为我已经学不会了。
蒂亚
好的,我现在有了。
某些功能如 "order-id" 中的:
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();
}
}
以下 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');
创建连接数据库
$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);
}
}
我构建此 "save-function" 以将每个订购的产品保存在自定义 table 中。