Marais Rossouw
Marais Rossouw

Reputation: 957

Programmatically add bundle products to cart Magento

So I've got a bundle product, that contains 6 products. I've written code to add this bundle to the cart, programmatically adding all 6 options to the bundle... But I get this Call to a member function getPosition() on a non-object error... It happens in the shakeSelections function, but it doesn't make sense because all the objects and stuff needs to go, like the $aand $b need to be of a Mage_Catalog_Model_Product data type and they are, its just the getOption method does not exist within it... Is there something i'm doing wrong?

This is my custom addAction in my custom controller:

public function addAction() {
        $prdId = $this->getRequest()->getParam('product');

        $cart = Mage::getModel('checkout/cart');
        $cart->init();

        $_return = array('code' => 0, 'message' => 'Something went wrong...');

        if (!empty($prdId)) {
            $product = new Mage_Catalog_Model_Product();
            $product->load($prdId);

            if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
                $typeInstance = $product->getTypeInstance(true);
                $optionCollection = $typeInstance->getOptionsCollection($product);

                $selectionCollection = $typeInstance->getSelectionsCollection(
                    $typeInstance->getOptionsIds($product),
                    $product
                    );

                $this->_options = $optionCollection->appendSelections($selectionCollection, false,
                    Mage::helper('catalog/product')->getSkipSaleableCheck()
                    );

                $bundle_option = array();

                foreach ($this->_options as $key => $value) {
                    foreach ($value['selections'] as $selKey => $selection) {
                        $bundle_option[$selection->option_id][] = $selection['selection_id'];
                    }
                }

                $params = array(
                    'product' => $prdId,
                    'related_product' => '',
                    'bundle_option' => $bundle_option,
                    'qty' => '1',
                );

                $cart->addProduct($product, $params);
                $cart->save();
            }
        }
    }

The url is basically: /forty/cart/add/product/11 Where 11 is the id of the bundled product.

Upvotes: 1

Views: 5123

Answers (1)

liyakat
liyakat

Reputation: 11853

Here some modification in your code should be work

$cart = Mage::getModel('checkout/cart');
$cart->init();

$params = $this->getRequest()->getParams();
$productId = $this->getRequest()->getParam('product');

$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId);

if($product->getTypeId() == "bundle"){

    $bundled_items = array();
    $optionCollection = $product->getTypeInstance()->getOptionsCollection();
    $selectionCollection = $product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds());
    $options = $optionCollection->appendSelections($selectionCollection);

    foreach($options as $option) {
        $_selections = $option->getSelections();

        foreach($_selections as $selection) {
            $bundled_items[$option->getOptionId()][] = $selection->getSelectionId();
        }
    }

    $params = array('bundle_option' => $bundled_items,'qty' => 1,'product'=>$productId);
}

if (isset($params['qty'])) {
    $filter = new Zend_Filter_LocalizedToNormalized(
        array('locale' => Mage::app()->getLocale()->getLocaleCode())
        );
    $params['qty'] = $filter->filter($params['qty']);
}

$product = new Mage_Catalog_Model_Product();
$product->load($productId);

$cart->addProduct($product, $params);
$cart->save();

Mage::dispatchEvent('checkout_cart_add_product_complete',
    array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
);

Hope this will sure work for you.

Let me know if i can help you more.

Upvotes: 4

Related Questions