Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - how to retrieve bundled option images

I've been working on my first magento deploy. Got a pretty customized theme built up... tackling some of the non-standard customizations now:

One of my primary product types is an office chair which I have set up as a bundled product. There are many options available for this product type (about 100 fabric options, arm style, lumbar, headrest etc) and I need to be able to show an image for each of these on the catalog/product/view page.

Being a bundled product (and I'll refrain from any discussion about whether this was the right product type - we went around in circles debating between a configurable and a bundled) - each product is assembled from a number of simple products (as options). These simple products can have images uploaded to them, and I've done so. I now want to retrieve the urls to those from the media folder...

After some hunting - these seem to be the essential elements:

theme/../template/catalog/product/view/options.phtml

<?php $_options = Mage::helper('core')->decorateArray($this->getOptions()) ?>
<dl>
    <?php foreach($_options as $_option): ?>
        <?php echo $this->getOptionHtml($_option) ?>
    <?php endforeach; ?>
</dl>

theme/../template/bundle/catalog/product/view/type/bundle/option/select.phtml

<?php /* @var $this Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option_Select */ ?>
<?php $_option      = $this->getOption(); ?>
<?php $_selections  = $_option->getSelections(); ?>

I found finally found getSelections() in:

class Mage_Bundle_Model_Resource_Price_Index extends Mage_Core_Model_Resource_Db_Abstract
{ ...


    /**
        * Retrieve bundle options with selections and prices by product
       *
        * @param int $productId
        * @return array
        */
       public function getSelections($productId)
       {
           $options = array();
           $read = $this->_getReadAdapter();
           $select = $read->select()
               ->from(
                   array('option_table' => $this->getTable('bundle/option')),
                   array('option_id', 'required', 'type')
               )
               ->join(
                   array('selection_table' => $this->getTable('bundle/selection')),
                   'selection_table.option_id=option_table.option_id',
                   array('selection_id', 'product_id', 'selection_price_type',
                       'selection_price_value', 'selection_qty', 'selection_can_change_qty')
               )
               ->join(
                   array('e' => $this->getTable('catalog/product')),
                   'e.entity_id=selection_table.product_id AND e.required_options=0',
                   array()
               )
               ->where('option_table.parent_id=:product_id');

           $query = $read->query($select, array('product_id' => $productId));
           while ($row = $query->fetch()) {
               if (!isset($options[$row['option_id']])) {
                   $options[$row['option_id']] = array(
                       'option_id'     => $row['option_id'],
                       'required'      => $row['required'],
                       'type'          => $row['type'],
                       'selections'    => array()
                   );
               }
               $options[$row['option_id']]['selections'][$row['selection_id']] = array(
                   'selection_id'      => $row['selection_id'],
                   'product_id'        => $row['product_id'],
                   'price_type'        => $row['selection_price_type'],
                   'price_value'       => $row['selection_price_value'],
                   'qty'               => $row['selection_qty'],
                   'can_change_qty'    => $row['selection_can_change_qty']
               );
           }

           return $options;
       }

so we get back an array with selection_id, product_id, price_type etc... but nothing referring to the image url for that selection...

Given that:

class Mage_Catalog_Helper_Product extends Mage_Core_Helper_Url
{ ...
public function getImageUrl($product)
        {
            $url = false;
            if (!$product->getImage()) {
                $url = Mage::getDesign()->getSkinUrl('images/no_image.jpg');
            }
            elseif ($attribute = $product->getResource()->getAttribute('image')) {
               $url = $attribute->getFrontend()->getUrl($product);
           }
           return $url;
       }

I'm trying to build a js object with refs to each selection's image url kind of like so in theme/../template/bundle/catalog/product/view/type/bundle/option/select.phtml

var option_set_<?php echo $_option->getId() ?> = {};
<?php foreach ($_selections as $_selection): ?>
    option_set_<?php echo $_option->getId() ?>._<?php echo $_selection->getSelectionId() ?> = '<?php echo $_selection->getImageUrl(); ?>';
<?php endforeach; ?>

But $_selection obviously isn't typed correctly because it just bounces me to the placeholder graphic.

Assuming these options can be typed as a Product (or somehow obtain the simple product from the selection_id, it seems like something like that can work. I'm not sure if that's exactly how I want to manage this feature, but if I can just get a stack of urls - then I'll be in business

Weirdly, the interwebs is basically silent on this subject. Apparently nobody needs to show images for their product options (or, rather, the only solutions are paid extensions - which will be a last resort).

How I can go about figuring this out?

Jesus tapdancing christ. Could Mage be any more complicated?


Update

Got this to work as such:

<?php echo $this->helper('catalog/image')->init($_selection, 'small_image')->resize(40,40); ?>

The answer I selected will also work fine, if anybody needs this fix.

like image 829
Bosworth99 Avatar asked Jul 03 '12 20:07

Bosworth99


1 Answers

The product_id in the $_selections represents the id of the selected simple product. So all to do is:

  1. take the product_id of the $_selections,
  2. load the product by this product_id
  3. give the resulting product object to Mage_Catalog_Helper_Product::getImageUrl.
like image 86
Matthias Mann Avatar answered Sep 19 '22 18:09

Matthias Mann