3

Synopsis

As per a previous question I have asked (which for now is irrelevant, although an indication to this question). I have a Grouped Product with associated Simple Products these associated simple products also have custom options (The options are not required).

The moment I add custom options to my Simple Products' they disappear from the Grouped Product view (within the table of associated products). The moment I remove the custom options, the product is visible to the frontend henceforth, that said the same goes for the Associated Products Grid in the back-end.

The above is explained furthermore in my previous question.

The goal

Is to use a Helper to generate the product options' fields in a table row below the Simple Product on the Grouped Product view for associated products that have been given Custom Options.

My attempts

So I created a custom module and helper to query the database for the options and their values for a given product - now I wish to set the option data to the product, but I don't know how to, neither can I find any useful resources.

Although I am aware of $product = $product->load($product->getId()); In my opinion, this is hacky to generate a heavy query n times on top of the already heavy query to get the associated products fields and grouped products' information. Even If I did use this method, my problem still exists I cannot place the Custom Options fields in my view.

If it helps, I used grep as per usual and discovered container2 but I could not find anything about what this is; how it is configured; how it is generated; how to use it; etc.

Example

<?php

class My_Catalog_Helper_Product extends Mage_Core_Helper_Abstract
{
    public function getOptions(Mage_Catalog_Model_Product $product)
    {
        $resource = Mage::getResourceModel('catalog/product_option_collection');
        $select   = $resource->getOptions(Mage::app()->getStore()->getId())
            ->getSelect()
                ->where('`main_table`.`product_id` = ?', (int) $product->getId());

        # XXX - Attempt 1 - returns the right value but the array keys are not 
        # option_id but an empty sting.
        $options = Mage::getModel('catalog/product_option')
            ->setProductId($product->getId())
            ->setStoreId($product->getStoreId())
            ->addData($resource->load()->getItems());

        $product->addOption($options);
        var_dump($product->getOptions());die;

        # XXX - Attempt 2 - The options returns an empty array - this is inconsistent
        # to the normal way of set<PropertyName>( $params ) - setData as we know clears
        # any existing data for the $params - so is not what I want.
        $product->setOptions($options);
        var_dump($product->getOptions());die;

        # XXX - Attempt 3 - returns the same as above with the correct option_id
        # as the key of the option instance.
        $options = array();
        foreach ($resource->load()->getItems() as $optionId => $option) {
            $options[$optionId] = $option;
        }

        # XXX - This appears to be the result that I expect, but I cannot add this to
        # my product, how can I do this?
        var_dump($options);die;

        # XXX- This next block was a test to get the values for the options but I don't
        # know if this is necessary or even how to apply this.
        $resource = Mage::getResourceModel('catalog/product_option_value_collection')
            ->addOptionToFilter(array_keys($options))
            ->load();

        $data = array();
        foreach ($resource->getItems() as $optionValueId => $option) {
            $optionId = $option->getOptionId();
            $data[$optionId] = $optionValueId;
        }

        # XXX Again I don't know what to do with this yet.
        $product->setOptions($data);
        var_dump($product->getOptions());die;

        return $options;
    }
}
  • I am looking at a very similar situation, and have found articles on solvingmagento.com to be quite useful. If you're still looking at this, I suggest at least going over the bundle/grouped product articles (as, at least in my case, either could be an appropriate choice for a highly customized requirement). – pspahn Oct 30 '14 at 6:57