Get Parent ID from SKU
-
05-02-2021 - |
Question
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$_SERVER['MAGE_IS_DEVELOPER_MODE'] = true;
require_once "app/Mage.php";
Mage::app();
umask(0);
try {
echo('<h1>Completed Order Info</h1>');
// M100210170 | M100210167 | M100210149
$orderId = '10006-L4'; // this is entity id
$order = Mage::getSingleton('sales/order')->loadByIncrementId($orderId);
// To get some basic order details, subtotal, shipping cost, discount, tax and grand total.
echo "order number: ".$orderId."<br>";
echo "order subtotal: ".$order->getSubtotal()."<br>";
echo "shipping: ".$order->getShippingAmount()."<br>";
echo "grand total: ".$order->getGrandTotal()."<br><br><br>";
$orderItems = array();
$getTotal=0;
foreach($order->getItemsCollection() as $item)
{
//$product = Mage::getModel('catalog/product')->load($item->getProductId());
$parentIds = Mage::getSingleton('catalog/product_type_configurable')->getParentIdsByChild($item->getSku());
echo "SKU = ".$item->getSku()."<br /># of Items in \$parentIds = ".count($parentIds)."<br />";
if(count($parentIds)>0) {
echo "<pre>\$parentIds<br />";
echo print_r($parentIds);
echo "</pre>";
} else {
echo "no items to return...<hr><br />";
}
$row=array();
$row['sku'] = $item->getSku();
$row['price'] = $item->getPrice();
$row['qty_ordered']= (int)$item->getQtyOrdered();
$row['subtotal']= (int)$row['qty_ordered'] * $row['price'];
$row['item_total']= $item->getRowTotal();
$getTotal = $getTotal + floatval(((int)$row['qty_ordered'] * $row['price']));
$orderItems[]=$row;
}
echo "\$getTotal = ".$getTotal."<br>";
echo "All items in the order:<br><pre>".print_r($orderItems,true)."</pre><br><br><br>";
} catch (Exception $e) {
Mage::logException($e);
}
Solution
getParentIdsByChild function needs id, not sku.
So try :
$parentIds = Mage::getSingleton('catalog/product_type_configurable')->getParentIdsByChild($item->getId());
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange