The basic knowledge about Magento 2 and order management
How Magento 2 deal with the orders?
From Magento 2, the orders are managed as a structured group of data that are shared in different database table instances.
There are several instances from which the main one is the Order table that contains the summary of all the orders that have been placed (date, total amount, tax, customer...).
There are also 3 sub-instances that concern the order details. Those instances describe which products have been ordered and give details about the customer's addresses:
- Product
- Shipping address
- Billing address
The 4 following sub-instances help to store the different steps in the order process:
- Payments
- Invoice
- Shipment
- Credit memo
Mass Order Export will help you to extract much more data from your Magento 2 database.
How the Mass Order Export extension exports the data in Magento 2?
Mass Order Export allows you to export all the orders data from your Magento 2 admin into XML or TXT/CSV files. These files can then be transferred into your CRM/ERP software or can be used for reporting purposes.
The way the Mass Order Export extension deals with the orders data is very simple. When configuring your export profile, you have to complete a pattern in which you can use static and dynamic values.
Static values are data that don't change from one order to another and that are added in the output file to make it readable whereas Dynamic values are data that are different for each order such as price, address, the product ordered...
To add the dynamic values to your pattern, you need to use a specific syntax as in the example below.
For example, you want to export all the orders placed into your Magento 2 website and you want to get a simple TXT file including the order id and the total amount of each order.
#123456789 $150.00
#234567891 $275.00
#345678912 $450.00
In your pattern, you should then copy:
#{{order.increment_id}} ${{order.total_paid}}
# and $ are static values while {{order.increment_id}} and {{order.total_paid}} are dynamic values that change for each order and that refer to the Order instance.
Mass Order Export includes a full library of all data available for all instances: order, products, payment, invoice, shipment, credit memo...
How to use Mass Order Export in 4 steps?
Mass Order Export is quite easy to use. You'll need to follow 4 steps to configure your extension for Magento 2. Once these steps completed, your extension will be ready to use.
STEP 1: Configure Mass Order Export in a general way
To configure Mass Order Export for Magento 2, you need to go to:
StoresSettingsConfiguration Wyomind Mass Order Export
There you get two subtabs.
Cron job reporting
Mass Order Export allows you to enable cron job reporting.
You have the possibility to send these generation reports to email addresses (they must be separated by a comma).
You can also change the report name as you want.
Advanced settings
In that tab, you will be able to enable the log files.
You will also have the possibility to automatically execute profiles when an order is placed by filling the profile ids that you will find in:
SalesMass Order ExportProfiles
In the case where the grid is already overwritten by another module, you'll be able to easily modify the orders grid to override.
STEP 2: Configure your profiles
During that step, the different tabs that can be found in the configuration of a profile will be described. First, the profile configuration will be detailed followed by the different kinds of templates, the filters that can be created to adjust your configuration, the different storage outputs, and the cron schedule in order to automatically generate the profile.
You can create or edit an existing profile from:
SaleSMass Order ExportProfiles
The Tool Box or so-called Black Box
Mass Order Export provides you a tool to help you configure your profiles as you want. When editing a profile you'll find a toolbox on the right of the window.
The toolbox is inevitably a very useful tool. This will help you to configure your profiles more quickly.
Here are the different options of the black box.
Preview
Library
Profile Configuration
When creating or editing an export profile in Mass Order Export, you have to be aware of some basic settings.
Export file(s) Configuration
You can start with the file configuration of the file.
- File name:
It is the base name of the exported files. Note that there is no need to add any file extension (XML, CSV, TXT) as the extension does it automatically.
file - Encoding type
Choose the encoding type you want to use between:
UTF-8: It is the most common format
UTF-8 without BOM: See more about BOM
ANSI - File name format
Your file name can be completed with the current date at the time of the exportation. Different formats are available.
file.txt2019-07-03-file.txt
file-2019-07-03.txt - Create one file for each order
Choose to export one order per file or not. - File name suffix
When choosing to export one order per file, you need to select a file name suffix between the order increment ID, the Magento internal order ID or the module internal auto-increment.
Export options
In that section, define how you want the file to be exported.
- Start with increment ID
You can add an ID from which the orders should be exported.
000000002To export all the orders, you can leave this field empty. - Register the latest exported increment ID
If enabled, this will automatically be updated with the latest ID. - Mark each order as exported
If enabled, this will mark each exported order with the name of the profile. Note that an order can receive several flags.
Exported toERP/CRM export/import - Export only unmarked order
If enabled, only orders that are not yet marked will be exported. Note that the orders must also match with the filters.
You should find an Exported to column inSalesOperations Orders . This will display for each order the profile to which the order has been exported. Orders will be marked only if they have been exported to a profile. If you need to export orders that have already been exported whereas you've defined Export only unmarked orders on YES, you can untag them by clicking on the cross.
- Update the order status
You have the possibility to update the order status. - New order status
Choose the status to apply to the exported orders.
For example:
Processing - Message in the order history
You also have the possibility to add a comment to the history.
Order exportedIf you don't want to add any message, you can leave this field empty.
Filters
In the Filters tab, you can define the types of orders and customers you want to include in your export profile. You have also the possibility to create more advanced filters that will best meet your needs.
Store view
In this tab, choose the store view from which to export the orders. At least one store view must be selected
Order status
This section allows you to specify the type of order you want to export. At least one status must be selected.
Customer Group
In this tab, choose the customer group of the orders you want to export. At least one customer group must be selected
Advanced Filters
In that tab, you can create more precise filters. For example, you can choose to export only one specific order by setting a filter:
order > increment_id | = | ID of your order
You can also export orders placed by customers whose last name ends with 'son':
order > customer_lastname | like | %son
You have a choice between several options:
- =, >, <...
Equal to, greater than, lower than, greater than or equal to, less than or equal to, different from. - LIKE / NOT LIKE
like: starts/ends with the character string / includes the character string.
Not like: doesn't start / end with the character string / doesn't include the character string. - IS NULL / IS NOT NULL
is null: the value is null.
is not null: the value is not null. - IN / NOT IN
in: is in (selection of several options).
not in: is not in (selection of several options).
- REGULAR EXPRESSION / FIND IN SET
Use regular expressions to describe a sequence of characters.
Template
In this tab, you'll be able to configure your profile template according to your needs. Mass Order Export allows you to create different kinds of file:
- XML
- CSV
- TXT
- TSV
- DIN
XML Export file settings
You can select the XML file type.
- File type
XML - Enclose XML tag content inside CDATA
When enabled, this encloses all node values with CDATA and avoids issues with special characters such as < (lower than), > (higher than) or & (ampersand). Note that this is recommended.
XML file template
An XML template is made up of 3 parts:
- Header
Is at the start of the file:
<orders> - Body
Is iterated for each order included in the file.
<order> ... </order>
- Footer
Is at the bottom of the file:
</orders>
The XML structure allows you to use iterative syntax in different parts of the template, as follows:
<items>
<?php foreach($products as $product): ?>
<item id={{product.item_id}}>{{product.name}}</item>
<weight>{{product.weight}}</weight>
<?php endforeach; ?>
</items>
In most cases, orders contain more than one product, so it may be useful to create an iteration that loops for each ordered product. All the content between the opening and closing markers will be repeated for each product and all dynamic values will be replaced:
<?php foreach($products as $product): ?>
...
<?php endforeach; ?>
These iteration markers can be used for all order instances:
<?php foreach($payments as $payment): ?>
...
<?php endforeach; ?>
<?php foreach($invoices as $invoice): ?>
...
<?php endforeach; ?>
<?php foreach($shipments as $shipment): ?>
...
<?php endforeach; ?>
<?php foreach($creditmemos as $creditmemo): ?>
...
<?php endforeach; ?>
CSV/TXT/TSV/DIN Export file settings
The file type can be CSV, TXT, TSV, or DIN. You have the possibility to create basic or advanced files.
- File format: Advanced format
It consists of a complex file structure divided into several blocks.
- File format: Basic format
It consists of one optional header and several rows applying the same pattern. - Include header
Allows you to include or not the column's name in the first row of your file.
- Delimiter character
Lets you choose which kind of delimiter you want to use to separate your fields. - Enclosure character
Character that surrounds each field of the rows. - Escape character
Lets you choose which kind of enclosure character you want to use for your data.
CSV/TXT/TSV/DIN file template
The template tab will be updated to match the file type you've selected.
Basic file format
Basic format for txt-like files consists of one optional header and several rows applying the same pattern.
- Extra Header
These are additional row(s) of headers that will be added at the very top of the file. - Header
Is for the attribute name that can be displayed at the top of the file. - Field Pattern
Works in the same way as in XML files. Like for XML files, you're allowed to use PHP code if you want to customize the pattern. You just have to write your PHP code in one single line. - Insert a new field
You can add a field by using this button. The fields can then be re-ordered easily by using the drag and drop arrows. - Extra Footer
These are additional row(s) that will be added at the very bottom of the file.
The main difference with the XML mode is that you can't use any iteration instructions. Instead, Mass Order Export uses an auto-consolidation internal process. This simply means if you try to get the value of any attribute of an instance (product for example) that contains several items, then the extension will automatically duplicate the TXT/CSV line for each item.
With the following configuration:
Logically the module should export one line for each order, but some orders may contain several product items. The extension will create several lines for the same order, as in the following output example:
Order# Product Name Product Sku
#10000001 my product A AAA
#10000001 my product B BBB
#10000001 my product C CCC
#10000002 my product D DDD
#10000003 my product B BBB
#10000003 my product E EEE
...
Advanced file format
Advanced format for txt-like files consists of a complex file structure divided into several blocks.
- Extra Header
These are additional row(s) of headers that will be added at the very top of the file. - Body
Is iterated for each order included in the file. - Extra Footer
These are additional row(s) that will be added at the very bottom of the file.
This format allows you to use iterative syntax in different parts of the template, as follows:
<?php foreach($products as $product): ?>
PRODUCT=
{{product.sku}}
{{product.qty_ordered}}
{{product.base_row_total output="number_format($self,2)"}}$
<?php endforeach; ?>
Output
With Mass Order Export, you have many possibilities to export your profiles.
Storage settings
FTP settings
You can also configure the FTP settings in order to upload the generated profile to an external FTP server. For example:
- Upload by FTP
Select Yes if you want to upload your profile using FTP. - Use SFTP
Select Yes if you are using SFTP. - Use passive mode
If you are using SFTP, you don't have to care about the passive/active mode. - Host
In that field, fill in the host.
wyomind.com - Port
It is the port to connect to server. - Login
It is your login to connect to server. - Password
It is your password to connect to server. - Destination directory
It is a relative path from the entry directory when you connect to the ftp.
/var/export/ftp
Email settings
Finally, you can choose to send your profiles by email. Don't forget to separate the email addresses with a comma.
- Send by email
You have the possibility to send the profile by email. - Email sender
This is the email of the sender. - Email recipients
This is the email(s) of the recipient(s). - Email subject
Define the subject of the email. - Email body
Add the content of your choice in the email body.
You have the possibility to check the email by clicking on Test Email.
A popup will confirm that the email has been.
- Send all files in the same email
You have the possibility to send all the files in the email. - Send all files in a zipped file
You can add the files in a zip file.
Cron Schedule
STEP 3: Export your orders
Now that your profiles are configured, you can export your orders into these files.
From your Magento 2 back office, you will be able to see to which profile each order has been exported, in:
SalesOperationsOrders
For each order, different notifications will be displayed in the Exported to column:
- √ default (X)
The order has been exported to the profile named default (only when the Mark each exported order option of the profile configuration is on YES). - No profile defined
No profile is defined for the product(s) of the order. - => default
The profile defined for that order is default.
You can export your orders to anyone of your profiles via several ways.
Generate the profile
Every time a profile is generated, the orders that are matching with the profile configuration are automatically exported to that file. From the Magento 2 back-office, choose a profile and simply select the Generate option from the Action dropdown, in:
SalesMass Order Exportprofiles
A message is displayed and indicates how many orders have been exported to the file.
Generate the profile via the command line
You can generate a profile using the below command line:
bin/magento wyomind:ordersexporttool:export [profile_id]
This allows you to run a profile using its ID.
For example:
bin/magento wyomind:ordersexporttool:export 5
If you don't know the ID of the profile you want to execute, you can run the below command:
bin/magento wyomind:ordersexporttool:list
This will list all your export profiles as well as their IDs.
Manually export the orders
Choose an order and click on View from:
SalesOperationsOrders
There you can select a profile for each product of the order. For example: txt_sample for a product and xml_sample for another one.
If you go back to:
SalesOperationsOrders
In the Exported to column, the order displays:
→ txt_sample
→ xml_sample
This means that the order will be exported to those profiles.
Now go back to the order View and click on Export.
Note that the order will be exported to the profile(s) you've defined only if the order is matching with the configuration of the profile.
Export only unmarked orders
2 options are interesting in the Export Options of the Configuration tab of your profile, from:
SalesMass Order Exportprofiles
- Mark each order as exported (in the orders grid)
- Export only unmarked orders
If you don't want to export several times the same orders, you can define both options on YES. Then only the unmarked orders will be exported.
If at any time, you want to export a new time an order that has already been exported, you have the possibility to unmark that order by clicking on the cross next to the profile name in:
SalesOperationsOrders
Then, when the profile will be run, the order will be exported a new time.
STEP 4: Customize your profiles
With Mass Order Export you can create your own custom variables and functions. You'll find examples in the following documentation that will help you to customize your profiles for Magento 2.
Create your custom variables
In some cases, you might need to get the value of one attribute that is not available in any of the default instances (order, product, address, payment, invoice, shipment or credit memo) or you might need to use the value of one attribute but with a computed output.
Mass Order Export allows creating your own customized variables directly from your Magento 2 admin panel by using some PHP code.
Create a custom variable
To create a custom variable, click on Add new from:
SalesMass Order ExportCustom variables
Below are some examples of customized variables.
- Get the description of the product of the order
Scope = ProductName = descriptionComment = Retrieve the description of each product. PHP Script =
<?php
$om = \Magento\Framework\App\ObjectManager::getInstance();
$model = $om->get('\Magento\Catalog\Model\Product');
$product = $model->load($item->getProductId());
return $product ->getDescription();
?>
- Get the SKUs of the ordered products:
Scope = OrderName = skusComment = Retrieve the sku of each ordered product. PHP Script =
<?php
$skus=array();
foreach($order->getItems() as $item){
$skus[]=$item->getSku();
}
return implode(", ",$skus);
?>
Add your custom variable to the template
Once you've created the custom variable, you can insert it into your XML or CSV file template.
For example, to get the SKUs of all the products that have been ordered, add to your file template:
{{order.skus}}
Create your custom functions
Mass Order Export also gives you the possibility to create your own custom PHP functions.
Create a custom function
To do so, click on Add new from:
SalesMass Order ExportCustom functions
You should now add your custom function in the PHP script field as follows:
<?php
if(!function_exists("example")){
function example($self,$argument_1,$argument_2){
... do something with the arguments...
return $something;
}
}
?>
The variable $self represents the value of the attribute called when you use the custom function.
Here is an example of a custom function:
<?php
if(!function_exists("parseDate")){
function parseDate($self,$format){
return date($format,strtotime($self));
}
?>
Add your custom function to the template
Once you've created the custom function and clicked on Save, you can insert it into your XML or CSV file template.
A custom function can be used in the template as follows:
{{any.variable output="example($self,'value 1','value 2')"}}
For example, to retrieve the formatted creation date of the order, add to your file template the below code:
{{order.created_at output="parseDate($self,'m/d/Y')"}}
<?php
$om = \Magento\Framework\App\ObjectManager::getInstance();
$model = $om->get('\Magento\Catalog\Model\Product');
$product = $model->load($item->getProductId());
return $product ->getDescription();
?>