Search code examples
magentomagento-1.9

Magento - add to cart a product with advanced custom options setting option QTY


I am running Magento 1.9 with Advanced Custom Options by MageWorx. I am creating a module that needs to add a product with custom options to the cart. With the code below I can add the product to the cart with 1 option. Yet I can't figure out how to add a certain quantity for the custom option.

$product = $this->getProduct();
$product_id = $product->getId();

$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty'     => 1,
    'options' => array(     
        '1' => 1,
     )
);

    try {   
        $cart->addProduct($product, $params);
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
        $cart->save();

        echo '<div class="shiptext">Your order has been add to your cart.</div><br clear="all">';
    }
    catch (Exception $ex) {
        echo $ex->getMessage();
    } 

Maybe I can't increase the QTY this way as Magento core doesn't support QTY for custom options? Which case I am guessing I may need to use some class inside of the Advanced Custom Options module but I am not sure how I would do that. If anyone has experience with Advanced Custom Options, it would be greatly appreciated as to how one might do this.


Solution

  • Found the answer. You need to add the to the prams options_groupid_qty.

    $params = array(
        'product' => $product_id,
        'qty'     => 1,
        'options' => array(     
            '1' => 1,
         ),
         'options_1_qty' => 30
    );