I need to get a list of ordered physical items to ship from the database. The list need to consist of name, sku, quantity, price, weight, and dimensions.
How can we fo this without using Magento’s own framework components.
The problem is that if a product contains options it appears multiple times in the table by different product types (simple, bundle, configurable, etc.) and different product ids.
$items = array();
$sql = (
"SELECT oi.name, oi.sku, oi.qty_ordered, oi.price, oi.weight
FROM {$db->prefix}sales_flat_order_item oi
WHERE oi.order_id = ". (int)$order_entity_id .";"
);
if ($result = $mysqli->query($sql)) {
while ($row = $result->fetch_assoc()) {
$items[] = [
'name' => $row['name'],
'sku' => $row['sku'],
'quantity' => (float)$row['qty_ordered'],
'unit_price' => (float)$row['price'],
'unit_weight' => (float)$row['weight'],
//'unit_length' => (float)$row['length'], // Not present?
//'unit_width' => (float)$row['width'], // Not present?
//'unit_height' => (float)$row['height'], // Not present?
];
}
$result->close();
}
If I pass a filter AND product_type = 'simple'
to get only the simple products, they do not contain price.
2
Answers
Try this:
Note that for Magento the SKU is an identifier and in sales_flat tables this the attribute used to link a bundle or configurable products to its simple. The parent product (configurable or bundle) has all the info that you need in your query.
*With this query to are assuming that in this shop you only sell bundle or configurable products.
If there is a configurable product which have multiple simple products with same price, then the price will be saved only in the configurable product. So you should not use the ‘simple product’ filter.
Try with below query,
This gives you the details of both Simple and Configurable product and you can filer the contents using conditions (if / else).