Question

We have a store that sells clothing. We have an attribute of size. I'm attempting to get items the customer purchased on the success page to pass into some JavaScript. Getting the quantity and price was simple enough, but I cannot figure out how to get the item's size.

What is going wrong?

$_customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order');
$order->load($lastOrderId);
$_totalData = $order->getData();
$_grand = $_totalData['grand_total'];
$_allItems = $order->getAllVisibleItems();


<script type='text/javascript'>
var orderInfo =
{
  orderId : '<?php echo $lastOrderId; ?>',
  priceTotal : '<?php echo $_grand; ?>',
  itemCount : '<?php echo count($_allItems); ?>',
  items : 
    [
<?php 
foreach ($_allItems as $item) 
{
    $_itemSku = $item->getSku();
    $_itemId = $item->getId();
    $_itemData = $item->getData();
    $_productModel = Mage::getModel('catalog/product')->load($_itemId);
    $_itemSize = $_productModel->getAttributeText('Size');
    $_itemUrl = $_productModel->getProductUrl();
    ?>

        { itemId : '<?="hi" ?>', 
            sku: '<?=$_itemSku; ?>',
            size : '<?=$_itemSize ?>',
            priceUSD : '<?=$item->getPrice(); ?>', 
            url : '<?=$_productUrl ?>' 
        },
    <?php 
}
Mage::log("Item Size");
Mage::log("$_itemSize");
Mage::log("Item Data");
Mage::log($_itemData);
Mage::log('Prodouct Options');
Mage::log($_itemData['product_options']);
Mage::log(unserialize($_itemData['product_options']));
?>
    ]
}
Was it helpful?

Solution

Take a look @ How to get Magento Order data from Observer

To get item options

 foreach ($_allItems as $item){
      $_itemSku = $item->getSku();
      .....

      $optionArray = $item->getProductOptions()
      // Todo : check to see if set and is array $optionArray['options'] for product without options

      $_itemSize = '';
      foreach($optionArray['options'] as $option){
          // Mage::log($option)
          //echo $option['label']
          //$option['value']
          if($option['label'] == 'Size'){
              $_itemSize = $option['value'];
          }
      }
 }

 ...
 Mage::log("$_itemSize");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top