cancel
Showing results for 
Search instead for 
Did you mean: 

Help : How to Add Bundle Product to Quote

Highlighted

Help : How to Add Bundle Product to Quote

Hello Everyone,

I am new to Magento and recently I've been trying to create an extension helping me create an order, so far oi managed to create orders for simple and configurable products but I'm having an issue with the bundles, it's been 2 days that I'm struggling with it.

So the thing is that I do send the params as required,  as followed

$array= new Varien_Object([
                        'product' => $productDetails["product_id"],
                        'qty' => $productDetails["qty"],
                        'bundle_option' => $productDetails["bundle_option"],
                        'bundle_option_qty' => $productDetails["bundle_option_qty"]
                    ]);
$quote->addProduct($product, $config);

but I keep getting the error

Selected required options are not available.

and I found out that the $product->getOptions() returns an empty array, I believe that's the problem and I can't find out why does it return an empty array

 

I would appreciate any help from you guys, thank you!

9 REPLIES 9
Highlighted

Re: Help : How to Add Bundle Product to Quote

@ayoubmoumg2deb You can use https://magento.stackexchange.com/questions/186786/magento-1-9-get-only-default-items-from-options-o...  to find all the options of bundle product.

 

Let me know if you are still facing any issue in this.

 

Thanks

Highlighted

Re: Help : How to Add Bundle Product to Quote

@Rahul Gupta  I already get to retrieve the bundle information using almost the same code

$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()]['product_name'] = $option->getDefaultTitle();
					$bundled_items[$option->getOptionId()][$selection->getSelectionId()] = array(
						'sku' => $selection->getSKU(),
						'name' => $selection->getName(),
						'price' => $selection->getSpecialPrice()
					);
				}
			}

The problem is when I try to add the product to the quote

$quote->addProduct($product, $config);

it returns the error

when I tried to see what's the reason behind it I found out that

$product->getOptions() // returns empty array

 

Highlighted

Re: Help : How to Add Bundle Product to Quote

@ayoubmoumg2deb Can you try the solution mention in the below link.

They are doing the same which you have mentioned.

https://magento.stackexchange.com/questions/146745/how-to-add-bundle-product-to-cart-programmaticall...

 

Thanks

Highlighted

Re: Help : How to Add Bundle Product to Quote

I have tried the solutions proposed but it didn't work.

 

what I'm doing is the following :

$product = Mage::getModel('catalog/product')->load($productDetails["product_id"]);
$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(); } } $config = new Varien_Object([ 'product' => $productDetails["product_id"], // X 'qty' => $productDetails["qty"], // 2 'bundle_options' => $bundled_items, // array( 234 => 1070, 235 => 1646 ) 'bundle_option_qty' => $productDetails["bundle_option_qty"], // array( 234 => 2, 235=>2 ) ]); $quote->addProduct($product, $config);

is it wrong what I'm doing?

PS: @rahul Gupta  I'm on Magento 1.9

Highlighted

Re: Help : How to Add Bundle Product to Quote

@ayoubmoumg2deb instead of adding to quote, have you tried to add them to cart so that they will automatically added to quote also.

 

$qty = 1;

        $product = $this->productFactory->create()->load($productId);
        $productsArray = $this->getBundleOptions($product);
        $params = [
            'product' => $productId,
            'bundle_option' => $productsArray,
            'qty' => $qty
        ];

        try {
            /**
             * Add bundle product in cart
             */
            if ($product->getId()) {
                $this->cart->addProduct($product, $params);
                $this->cart->save();
            }
        } catch (\Exception $e) {
            $resultData = [
                'status' => 'fail',
                'status_code' => 0,
                'message' => __('Unable to add the Product. Exception ' . $e->getMessage())
            ];
            return $result->setData($resultData);
        }
        return $result->setData($resultData);
    }

    /**
     * get all the selection products used in bundle product
     * @param $product
     * @return mixed
     */
    private function getBundleOptions(Product $product)
    {
        $selectionCollection = $product->getTypeInstance()
            ->getSelectionsCollection(
                $product->getTypeInstance()->getOptionsIds($product),
                $product
            );
        $bundleOptions = [];
        foreach ($selectionCollection as $selection) {
            $bundleOptions[$selection->getOptionId()][] = $selection->getSelectionId();
        }
        return $bundleOptions;
    }

Feel free to ask if you have any queries.

 

Thanks

Highlighted

Re: Help : How to Add Bundle Product to Quote

@rahul Gupta  when I try to add the product to the cart I get this error

The product could not be found
Highlighted

Re: Help : How to Add Bundle Product to Quote

@ayoubmoumg2deb can you please share the code?

Highlighted

Re: Help : How to Add Bundle Product to Quote

Sure of course

$cart = Mage::getModel('checkout/cart');
$cart->init();
$product = Mage::getModel('catalog/product')->load($productDetails["product_id"]);
			
if($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE){
	$params = new Varien_Object([
			'product' => $productDetails["product_id"],
			'qty' => $productDetails["qty"],
			'super_attribute' => $productDetails["attributes"],
		]);
}elseif($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE){
	$params = new Varien_Object([
			'product' => $productDetails["product_id"],
			'qty' => $productDetails["qty"],
			'bundle_option' => $productDetails["bundle_option"],
			'bundle_option_qty' => $productDetails["bundle_option_qty"],
		]);
}else{
	$params = $productDetails['qty'];
}

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

 

On addProduct() function at first, Magento calls the function _getProduct(), inside there is a condition. this one 

if (!$product
            || !$product->getId()
            || !is_array($product->getWebsiteIds())
            || !in_array($currentWebsiteId, $product->getWebsiteIds())
        ) {
            Mage::throwException(Mage::helper('checkout')->__('The product could not be found.'));
        }

and that's where it returns the message 

The product could not be found.

and when I comment the exception throwing, it returns 

Selected required options are not available.

 

 

Highlighted

Re: Help : How to Add Bundle Product to Quote

i have shared my experience When the bundle added to the opportunity, quote, order, or invoice includes optional products, the total price is calculated by adding the total ...