cancel
Showing results for 
Search instead for 
Did you mean: 

How to add to cart with multiple custom options?

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

How to add to cart with multiple custom options?

I use this code in list.phtml:

<div class="btn-addto">
                            <?php if(!$_product->canConfigure() && $_product->isSaleable()): ?>
                                
                                <!--<button type="button" title="<?php /*echo $this->quoteEscape($this->__('Add to Cart')) ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart')*/ ?></span></span></button>-->
                             
                            
                            <?php 
                            /*
                             * querystring link: http://www.website.com/checkout/cart/add?product=[productID]&qty=1&options[98]=Testtext
                             * GET option id $opval->getOptionId()
                             * 
                             */
                            
                                $product = Mage::getModel("catalog/product")->load($_product->getId());
                                $theoptions = $product->getOptions();
                                
                                $array= array();
                                foreach($theoptions as $opkey=> $opval){
                                    echo $opval->getLabel();
                                    $optionID = $opval->getOptionId();                                                                      
                                    //echo $opVal->getTitle();
                                    foreach($opval->getValues() as $valuesKey => $valuesVal) {
                                        
                                        $defaultVal= $valuesVal->getTitle();
                                        $defaultVal2= $valuesVal->getId();
                                        $array[$optionID] = $defaultVal2;                                        
                                        echo $defaultVal." ".$defaultVal2."<br>";
                                        break;
                                    }                                                                        
                                    //break;
                                }
                                
                                $newarray= array();
                                $newArray = array_keys($array);
                                /*echo "key ".$newArray[0]."<br>";                                
                                echo "valor ".$array[$newArray[0]];*/
                                
                            
                                
                            
                                $params_arr = array('product'=>$_product->getId(),'qty'=>1,"options[".$newArray[0]."]"=>$array[$newArray[0]],"options[$newArray[1]]"=>$array[$newArray[1]]);
                                
                                $url = 'checkout/cart/add?';
                                foreach($params_arr as $paramKey => $paramVal){
                                    $url_arr[] = $paramKey.'='.$paramVal;                                    
                                }
                                $url .= implode('&',$url_arr);
                                unset($url_arr);
                                ?>

                                <form action="<?php echo Mage::getUrl($url) ?>" method="post">
                                    <input type="hidden" name="product" value="1" />
                                    <input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />

                                    <fieldset class="add-to-cart-box">
                                    <input class="button btn-cart" type="submit" title="<?php echo $this->__('Add to Cart') ?>" class="addtocart" value="<?php echo $this->__('Add to Cart') ?>" /> 
                                    </fieldset>
                                </form>
                            
                            
                            <!--<a href="<?php /*echo Mage::getBaseUrl();?>checkout/cart/add?product=[<?php echo $_product->getId(); ?>]&qty=1&options[<?php echo $optionID;*/ ?>]=1">comprar</a>-->
                            
                            
                            <?php elseif($_product->getStockItem() && $_product->getStockItem()->getIsInStock()): ?>
                                <a title="<?php echo $this->quoteEscape($this->__('View Details')) ?>" class="button" href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->__('View Details') ?></a>
                            <?php else: ?>
                                <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                            <?php endif; ?>                        
                        </div>

My product have 2 custom options. But only get first option

How can add to cart with multiple custom options?