Retrieving Price Rules from an Order in Magento
In Magento, getting the catalog and cart price rules applied to an order requires a combination of querying the relevant database tables and processing the data to determine which rules were actually used for each order item. Here’s a step-by-step guide on how to achieve this:
Step 1: Retrieve the order information
First, let’s fetch the order details using Magento’s built-in API or repository:
// Using OrderRepositoryInterface
$orderRepository = $objectManager->get('Magento\Sales\Api\OrderRepositoryInterface');
$order = $orderRepository->get($incrementId);
Step 2: Get all the applied price rules
Magento stores information about applied price rules in the salesrule_application table. To retrieve these, you can query this table using a database connection:
// Using Zend_Db_Adapter_Pdo_Mysql (for example)
$connection = $objectManager->get('Magento\Framework\App\ResourceConnection')->getConnection('default');
$sql = "SELECT * FROM salesrule_application WHERE order_id = :order_id";
$bindParams = [':order_id' => $orderId];
$rules = $connection->fetchAll($sql, $bindParams);
Step 3: Process the price rules data
The retrieved data includes both catalog and cart price rules. To determine which rules were applied to each item, you’ll need to join this information with the salesrule table and process it:
// Assuming $rules is an array of fetched records
$processedRules = [];
foreach ($rules as $ruleData) {
$rule = $objectManager->get('Magento\SalesRule\Model\Rule')->load($ruleData['rule_id']);
// Process each rule - check conditions, actions, and if it was actually applied to the order item
// For example, you can use the Magento_SalesRule_Model_Observer::applyRules() method as a reference point
$processedRules[] = [
'rule_name' => $rule->getName(),
'discount_amount' => $ruleData['discount_amount'],
// Add other relevant data fields here
];
}
Step 4: Integrate the processed rules with your order export interface
Now that you have an array of processed price rules, you can integrate this data into your order export functionality:
// Assuming $orderExportModel is your custom order export model
$orderExportData = [
'increment_id' => $order->getIncrementId(),
'rules_applied' => $processedRules,
// Add other order details here
];
// Save or send this data to the desired output format (e.g., CSV, JSON, etc.)
Conclusion
Retrieving price rules from an order in Magento involves querying the database and processing the retrieved data. By following these steps, you can successfully determine which catalog and cart price rules were applied to each order item during the checkout process.
Remember that this is a simplified example; in a real-world scenario, you should use Magento’s object manager or dependency injection for fetching objects, encapsulate the logic inside a service class, and ensure proper exception handling and performance optimization.
For further reference on Magento’s price rule application process, consider reading our detailed guide on Magento price rule application at Laravel Company.