1

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

4

1 回答 1

0

getSelectionsById()正在返回一个Magento 集合,这是一个用于包含其他模型(所有同一个类)的模型。将其视为增强型阵列。你可以在这里阅读更多关于它们的信息:在 Magento 中使用集合

由于一些 PHP 技巧,Magento 集合在许多情况下可以被视为一个数组。例如,以下代码有效:

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

尽管如此,从集合中获取实际的 PHP 数组有时还是很有用的。为此,您使用getItems(). 它是一种可用于任何 Magento 集合(特别是任何继承自 的对象Varien_Data_Collection)的方法。你不应该有任何理由覆盖它。它只是返回集合对象在内部存储和使用的普通 ol' PHP 数组。如您所见,没什么特别的:

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

因此,如果您的集合getItems()返回null或为空数组,则表示该集合不包含任何对象。这通常是因为应用于集合的查询返回了一个空结果集,尽管我不确定这是否适用于您的情况。

很难具体说明为什么您的代码无法返回包含您提供的信息的填充集合。如果您可以更详细地了解您是如何实现新产品类型的——您创建了哪些类、如何扩展核心捆绑包类型等,那将会很有帮助。

于 2013-04-24T03:58:26.410 回答