1
votes

I need help with a certain custom extension I am building for Magento. I made an extension that would allow bundled product types to be associated with a "parent" bundle product.

So think of it as a I was selling a bundled product, "keyboard and mouse", that was bundled with a DESKTOP computer.

I have it working on the admin and I have it being displayed on the product view. However I am running into an issue when I am trying to add this "DESKTOP COMPUTER" to my shopping cart. I was able to trace it down to a function that called *_prepareProduct()* which is under /app/code/core/Mage/Bundle/Model/Product/Type.php.

Below is a snippet of the code that I am finding the problem. As you can see I have dumped out the selectionIds and that returns an array of what options I have selected from the product view page. The second dump is the selections->getItems (which I have no idea where this function is at, it wont let me focus it). However when I view this DUMP from a BUNDLE PRODUCT that only contains simple products (i.e., the bundle product that contains the keyboard and mouse) it will output data/object for selections->getItems(...). When I dump this from a bundled product that contains bundled products (i.e., the desktop computer that has the bundled product that contains the keyboard and mouse and other things ) it returns nothing for selections->getItems(...).

            // If product has not been configured yet then $selections array should be empty
        if (!empty($selectionIds)) {
            $selections = $this->getSelectionsByIds($selectionIds, $product);

            var_dump($selectionIds);
            var_dump($selections->getItems());    
            // Check if added selections are still on sale
            foreach ($selections->getItems() as $key => $selection) {

Can anyone help me understand "getSelectionsByIds" and how I can override it so it will not return an empty object for a bundled product when I add an item to my cart and/or help me understand getItems and how I can override that as well? I know how to override getSelectionsById but i don't understand what is causing the function to return nothing.

Thanks

1

1 Answers

0
votes

getSelectionsById() is returning a Magento collection, which is a model used to contain other models (all of the same class). Think of it as a beefed-up array. You can read more about them here: Using Collections in Magento

Because of some PHP tricks, a Magento collection can, in many cases, be treated like an array. For example, the following code works:

$productCollection = Mage::getModel('catalog/product')->getCollection();
foreach ($productCollection as $product) {
    // do stuff
}

Despite this, it's sometimes useful to get an actual PHP array from a collection. For this you use getItems(). It's a method available to any Magento collection (specifically, any object that inherits from Varien_Data_Collection). You shouldn't have any reason to override it. It simply returns the plain ol' PHP array that the collection object stores and uses internally. Nothing fancy, as you can see:

/**
 * Retrieve collection items
 *
 * @return array
 */
public function getItems()
{
    $this->load();
    return $this->_items;
}

So if getItems() return null or an empty array for your collection, that means the collection contains no objects. This is often because a query applied to the collection returned an empty result set, although I'm not sure if that's applicable that is to your situation.

It's hard to say specifically why your code is failing to return a populated collection with the information you've given. It would be helpful if you could go into a bit more detail about how you've implemented your new product type--what classes you've created, how you're extending the core bundle type, etc.