Is there any way we can archive orders in Magento 2 community edition? What are the steps to create one? Which things are needed to manage it?
-
does modifying 1 into 01 not fixing it ? Cause i don't think the format is valid neither. – Claims Apr 11 '22 at 13:56
-
any thoughts on this ? – Hamendra Sunthwal Apr 13 '22 at 03:39
-
What exactly are you trying to accomplish? Export orders to a CSV or archive them like the Commerce edition? https://docs.magento.com/user-guide/sales/order-archive-configure.html – Mike Dubs Apr 13 '22 at 19:22
-
Yes, that's right. – Hamendra Sunthwal Apr 15 '22 at 03:23
-
I am trying to create an API that would archive order not delete it. – Hamendra Sunthwal Apr 15 '22 at 13:07
-
Any thoughts on this ? – Hamendra Sunthwal Apr 15 '22 at 14:00
4 Answers
please use this document for this, it will help you to create the script export order Configure the order archive
- 565
- 5
- 31
You can create a stand alone page on root of the magento folder and proceed in this manner :
<?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $orderData = getOrderCollection(); echo '<pre>';print_r($orderData);die; function getOrderCollection()
{
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$order = $objectManager->create('Magento\Sales\Model\Order')->getCollection();
//echo '<pre>';print_r($order->getData());die;
foreach($order as $_order)
{
$orderData[$_order->getId()]['customer_firstname'] = $_order->getData('customer_firstname');
$orderData[$_order->getId()]['customer_lastname'] = $_order->getData('customer_lastname');
$orderData[$_order->getId()]['customer_email'] = $_order->getData('customer_email');
$orderItems = $_order->getAllItems();
foreach($orderItems as $item)
{
//echo '<pre>';print_r($item->getData());die;
$orderData[$_order->getId()][$item->getId()]['item_name'] = $item->getName();
}
}
return $orderData;
}
- 565
- 5
- 31
I created custom script for delete order.
=> pub/deleteorde.php
<?php
use Magento\Framework\App\Bootstrap;
require '../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$orders = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
$order_ids = $objectManager->get('Magento\Sales\Model\Order')->getCollection()->addFieldToSelect('entity_id')->getData();
if(sizeof($order_ids))
{
$registry = $objectManager->get('Magento\Framework\Registry');
foreach ($order_ids as $id) {
$order = $bootstrap->getObjectManager()->create('Magento\Sales\Model\Order')->load($id);
$registry->register('isSecureArea','true');
$order->delete();
$registry->unregister('isSecureArea');
echo "order deleted =" .$id;
echo "<br/>";
}
}
echo 'Order Deleted';
=> Below script is for direct truncate query, before execute it check carefully, and keep DB backup. it will truncate all order related data. if you had created any custom table then push table name in order_tables array.
<?php
use Magento\Framework\App\Bootstrap;
require '../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$order_tables = array('gift_message','quote','quote_address','quote_address_item','quote_id_mask','quote_item', 'quote_item_option', 'quote_payment', 'quote_shipping_rate', 'reporting_orders',
'sales_bestsellers_aggregated_daily', 'sales_bestsellers_aggregated_monthly', 'sales_bestsellers_aggregated_yearly', 'sales_creditmemo', 'sales_creditmemo_comment',
'sales_creditmemo_grid', 'sales_creditmemo_item', 'sales_invoice', 'sales_invoiced_aggregated',
'sales_invoiced_aggregated_order', 'sales_invoice_comment', 'sales_invoice_grid', 'sales_invoice_item',
'sales_order', 'sales_order_address', 'sales_order_aggregated_created', 'sales_order_aggregated_updated', 'sales_order_grid', 'sales_order_item', 'sales_order_payment', 'sales_order_status_history',
'sales_order_tax', 'sales_order_tax_item', 'sales_payment_transaction', 'sales_refunded_aggregated',
'sales_refunded_aggregated_order', 'sales_shipment', 'sales_shipment_comment', 'sales_shipment_grid',
'sales_shipment_item', 'sales_shipment_track', 'sales_shipping_aggregated', 'sales_shipping_aggregated_order', 'tax_order_aggregated_created', 'tax_order_aggregated_updated');
$connection = $resource->getConnection();
$truncate = "SET FOREIGN_KEY_CHECKS = 0";
$connection->query($truncate);
foreach($order_tables as $order_table)
{
try
{
$connection = $resource->getConnection();
$tableName = $resource->getTableName($order_table);
$truncate = "TRUNCATE TABLE " . $tableName ;;
$connection->query($truncate);
}
catch(Exception $e)
{
echo "failed";
echo "<br/>";
echo $e->getMessage();
die();
}
}
$connection = $resource->getConnection();
$truncate = "SET FOREIGN_KEY_CHECKS = 1";
$connection->query($truncate);
echo "success";
die();
reference
Magento 2: How to truncate customers, products, reviews and orders table
- 1,534
- 11
- 16