Magento – Parent configurable product image displayed in wishlist instead of child simple product image

ce-1.9.1.0configurable-productsimple-productwishlist

When I select a child product of a configurable product, and add it to the cart the correct simple child product image is displayed in the cart.

However if I add the same child product to the wishlist, the parent configurable product image is displayed.

I found the setting in Magento back end that controlled which image would be displayed in the cart, but I couldn't find a similar setting for the wishlist.

I also have been trying to fix this situation by changing the code in

app/design/frontend/customtheme/default/template/wishlist/item/column/image.phtml. 

However I have been unsuccessful.

Any help would be appreciated! Thanks

This is the code I have done so far; however now I don't have any image being displayed in my Wishlist. The file I am modifying is

app/design/frontend/customtheme/default/template/wishlist/item/column/image.phtml

and code is below

<?php
$item = $this->getItem();
$product = $item->getProduct();
$item_s = Mage::getModel('wishlist/item')->loadWithOptions($item->getId(), 'simple_product')->getOptionsByCode();
$simple_product = $item_s['simple_product']->getData();
$simple_product_id = $simple_product_data['product_id'];
$simple_product_attr = Mage::getModel('catalog/product')->load($simple_product_id);
?>
<a class="product-image" href="<?php echo $this->getProductUrl($item) ?>" title="<?php echo $this->escapeHtml($product->getName()) ?>">
     <img src="<?php echo $this->helper('catalog/image')->init($simple_product_attr ,'small_image'); ?>" alt="<?php echo $this->escapeHtml($product->getName()) ?>" />
</a>

Also here is the code with extraneous code removed:

$item = $this->getItem();
$product = $item->getProduct();
$item_s = Mage::getModel('wishlist/item')
     ->loadWithOptions($item->getId(), 'simple_product')
     ->getOptionsByCode();
$simple_product = $item_s['simple_product']->getData();
$simple_product_id = $simple_product_data['product_id'];
$simple_product_attr = Mage::getModel('catalog/product')
    ->load($simple_product_id);
<img src="<?php echo $this->helper('catalog/image')
    ->init($simple_product_attr ,'small_image'); ?>" />

Best Answer

$_product_id = $item->getData('product_id');
$_simple_product_id = Mage::helper('nhuthep')->getSimpleProductIdInWishlist($item->getData('wishlist_item_id'), $_product_id);
if ($_product_id != $_simple_product_id) {
    $_simple_product = Mage::getModel('catalog/product')->load($_simple_product_id);
}

if (isset($_simple_product) && $_simple_product->getId()) {
    $_small_image = $this->helper('catalog/image')->init($_simple_product, 'small_image');
} else {
    $_small_image = $this->helper('catalog/image')->init($product, 'small_image');
}

with helper nhuthep I have a function getSimpleProductIdInWishlist like this:

public function getSimpleProductIdInWishlist($wishlistItemId, $itemProductId)
{
   $wishlistItemOptions = Mage::getModel('wishlist/item_option')
      ->getCollection()
      ->addFieldToFilter('wishlist_item_id', $wishlistItemId);

   foreach ($wishlistItemOptions as $option) {
       $optionProductId = $option->getProductId();
       if ($optionProductId != $itemProductId) {
           return $optionProductId;
          }
       }

   return $itemProductId;
}

You can also get product name or product url of child product.