I have a custom module for viewing products in cart at the My account Dashboard. It's working fine, but some times it throws exceptional error at the getItemOptions()
function.
Customer/Block/Cart.php
class Mage_Customer_Block_Cart extends Mage_Core_Block_Template{
public function _construct(){
parent::_construct();
$this->setTemplate('customer/cart/cart.phtml');
Mage::app()->getFrontController()->getAction()->getLayout()->getBlock('root')
->setHeaderTitle(Mage::helper('customer')->__('mycart'));
}
public function getAllItems(){
$items = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
//$items = Mage::getModel('checkout/cart')->getAllVisibleItems();
return $items;
}
public function getSubTotal()
{
$subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();
return $subTotal;
}
public function getTotal(){
$grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();
return $grandTotal;
}
public function getCartUrl(){
return Mage::getUrl('checkout/cart', array('_secure'=>true));
}
public function getCheckoutUrl(){
return Mage::getUrl('checkout/onepage', array('_secure'=>true));
}
public function getItemOptions($item)
{
/* @var $helper Mage_Catalog_Helper_Product_Configuration */
$helper = Mage::helper('catalog/product_configuration');
$options = $helper->getConfigurableOptions($item);
return $options;
}
public function getFormatedOptionValue($optionValue)
{
/* @var $helper Mage_Catalog_Helper_Product_Configuration */
$helper = Mage::helper('catalog/product_configuration');
$params = array(
'max_length' => 55,
'cut_replacer' => ' <a href="#" class="dots" onclick="return false">...</a>'
);
return $helper->getFormattedOptionValue($optionValue, $params);
}
}
cart.phtml:
<div class="dashboard">
<div class="page-title">
<h1><?php echo $this->__('My Cart') ?></h1>
</div>
<?php $items = $this->getAllItems();?>
<form>
<fieldset>
<table class="data-table" id="customcarttable">
<thead>
<tr class="first last">
<td>Product</td>
<td>Size</td>
<td>Quantity</td>
<td>Price</td>
</tr>
</thead>
<tbody>
<?php foreach ($items as $_item) { ?>
<?php $_options = $this->getItemOptions($_item); ?>
<tr>
<td>
<?php $productModel =Mage::getModel('catalog/product'); $_prod = $productModel->loadByAttribute('sku', $_item->getSku());?>
<a class="product-image" href="#" title="<?php echo $_item->getName();?>">
<img class="productimage" src="<?php echo $_prod->getImageUrl();?>" width='113' height='113' alt="<?php echo $_item->getName();?>"/>
</a>
</td>
<td>
<?php if ($_options = $this->getItemOptions($_item)):?>
<dl class="item-options">
<dd>
<?php foreach ($_options as $_option) : ?>
<?php $_formatedOptionValue = $this->getFormatedOptionValue($_option) ?>
<?php if($_option['label'] == 'Country'){?>
<?php echo $_formatedOptionValue['value']; ?>
<?php }elseif($_option['label'] == 'Size'){?>
<?php echo $_formatedOptionValue['value'] ?>
<?php }?>
<?php endforeach; ?>
</dd>
</dl>
<?php endif;?>
</td>
<td>
<h3><?php echo $_item->getQty();?></h3>
</td>
<td>
<div class="price-box">
<span class="regular-price" id="product-price-117">
<span class="price"><?php echo Mage::helper('core')->formatPrice($_item->getPrice(), false);?></span>
</span>
</div>
</td>
</tr>
<?php }?>
<tr><td colspan="3">SubTotal</td>
<td>
<div class="price-box">
<span class="regular-price">
<span class="price"><?php echo Mage::helper('core')->formatPrice($this->getSubTotal(), false);?></span>
</span>
</div>
</td>
</tr>
<tr><td colspan="3">Total</td>
<td>
<div class="price-box">
<span class="regular-price">
<span class="price"><?php echo Mage::helper('core')->formatPrice($this->getTotal(), false);?></span>
</span>
</div>
</td>
</tr>
</tbody>
</table>
<div class="buttons-set buttons-set2 cart">
<a href="<?php echo $this->getCartUrl();?>" title="View Cart" class="view-cart">View Cart</a>
<a href="<?php echo $this->getCheckoutUrl();?>" title="Checkout" class="view-cart">Checkout</a>
</div>
</fieldset>
</form>
</div>
Exceptional Error:
a:5:{i:0;s:51:"Wrong product type to extract configurable options.";i:1;s:3609:"#0 /app/code/core/Mage/Catalog/Helper/Product/Configuration.php(106): Mage::throwException('Wrong product t...')
#1 app/code/local/Mage/Customer/Block/Cart.php(42): Mage_Catalog_Helper_Product_Configuration->getConfigurableOptions(Object(Mage_Sales_Model_Quote_Item))
#2 app/design/frontend/base/default/template/customer/cart/cart.phtml(39): Mage_Customer_Block_Cart->getItemOptions(Object(Mage_Sales_Model_Quote_Item))
#3 app/code/core/Mage/Core/Block/Template.php(241): include('/var/www/html/o...')
#4 app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('frontend/base/d...')
#5 app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#6 app/code/core/Mage/Core/Block/Abstract.php(919): Mage_Core_Block_Template->_toHtml()
#7 app/code/core/Mage/Core/Block/Abstract.php(637): Mage_Core_Block_Abstract->toHtml()
#8 app/code/core/Mage/Core/Block/Abstract.php(577): Mage_Core_Block_Abstract->_getChildHtml('customer_cart', true)
#9 app/code/core/Mage/Page/Block/Html/Wrapper.php(52): Mage_Core_Block_Abstract->getChildHtml('', true, true)
#10 app/code/core/Mage/Core/Block/Abstract.php(919): Mage_Page_Block_Html_Wrapper->_toHtml()
#11 app/code/core/Mage/Core/Block/Text/List.php(43): Mage_Core_Block_Abstract->toHtml()
#12 app/code/core/Mage/Core/Block/Abstract.php(919): Mage_Core_Block_Text_List->_toHtml()
#13 app/code/core/Mage/Core/Block/Abstract.php(637): Mage_Core_Block_Abstract->toHtml()
#14 app/code/core/Mage/Core/Block/Abstract.php(581): Mage_Core_Block_Abstract->_getChildHtml('content', true)
#15 app/design/frontend/alosmile/default/template/page/2columns-left.phtml(71): Mage_Core_Block_Abstract->getChildHtml('content')
#16 app/code/core/Mage/Core/Block/Template.php(241): include('/var/www/html/o...')
#17 app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('frontend/alosmi...')
#18 app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#19 app/code/core/Mage/Core/Block/Abstract.php(919): Mage_Core_Block_Template->_toHtml()
#20 app/code/core/Mage/Core/Model/Layout.php(555): Mage_Core_Block_Abstract->toHtml()
#21 app/code/core/Mage/Core/Controller/Varien/Action.php(390): Mage_Core_Model_Layout->getOutput()
#22 app/code/core/Mage/Customer/controllers/CartController.php(15): Mage_Core_Controller_Varien_Action->renderLayout()
#23 app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Customer_CartController->indexAction()
#24 app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('index')
#25 app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#26 app/code/core/Mage/Core/Model/App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
#27 app/Mage.php(684): Mage_Core_Model_App->run(Array)
#28 index.php(83): Mage::run('', 'store')
#29 {main}";s:3:"url";s:15:"/customer/cart/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:9:"english_2";}
I can't predict why this error occurs. I hope some will help on this.
Thanks