第一:我了解如何在后端自定义表中创建自定义模块。我已经这样做了,它工作得很好,我可以在后台保存一些信息。(名字:app/代码/TestModule/模块)
但是现在我尝试定制订单流程。我想保存一些数据,如'oder_id'和'order_date'在一个自定义表。
这个过程应该在订单成功后自动运行。
问题:我不明白如何在自定义表中存储这些信息。
我有这个在我的成功。phtml用于检查前端的数据:
<?php
$block->getOrderId()
$block->getShippingMethod()
?>
在我的Block/Onepage/Success.php
中,用函数调用数据。...
public function getShippingMethod()
{
return $this->_checkoutSession->getLastRealOrder()->getShippingMethod();
}
...
嗯,现在我对下一步没有任何想法了。例如,我在magento 2中看到这个图,getModel的正确方法是什么?但这对我没有帮助。
关于stackoverflow的其他问题只回答如何构建模块或如何显示订单信息。
我想我必须这样做:
$this->_Modulename->addData($data);
$this->_transaction->save();
但是经过3天的阅读,我在这里试试运气。我不需要一个最终的解决方案,我也不想复制和粘贴答案代码,我只是不明白如何在成功后将数据保存在自定义表中。作品。
我把你们的一切都拿走了,因为我的知识已经到了尽头。
TYIA
好的,我现在有了。
一些函数,如:
中的"order-id"app/代码/模块/Modulename/块/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个(瓶子和球))和每个产品数量(2x球,1x瓶)。我希望你明白我的意思;)
// 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"来将每个订购的产品保存在一个自定义表中。