Get Adobe Flash player

    postheadericon Magento 1.9 - add to cart from CMS page with custom options

    WHAT:

    I wanted a add to cart button on my magento 1.9 cms page.

    The problem is much of the info on google is outdated ( version 1.4-1.7 )

    And i needed to be able to fill a custom option value as a free text field value.

    I did this with a virtual product with 1 text field required custom option

     

    HOWTO:

    1.Well start off with making a new CMS page.

    2.Then make a new .phtml file in your template directory example: mysite.com/app/design/frontend/mytheme/default/template/catalog/product/mynewfile.phtml"

    3.Point your CMS page in the magento backend to this new file with the following line:

    {{block type="core/template" name="mynewfilename" template="catalog/product/mynewfile.phtml"}}

    Make sure you are in "the hide editor mode" of the CMS page

    4. Everything you show on the phtml file should now be visable in on the frontend.

     

    Now to add the product and add to cart function to the .phtml file

     

    5. Add this code

    $myvar = "something";

    // 17 is the product ID options[3] is the options ID these can be found out by doing a var_dump *

    $params_arr = array('product'=>17,'qty'=>1,'options[3]'=> $myvar);
    $url = 'checkout/cart/add?';
    foreach($params_arr as $paramKey => $paramVal) $url_arr[] = $paramKey.'='.$paramVal;
    $url .= implode('&',$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() ?>" /> //getting unique form key

    <fieldset class="add-to-cart-box">
    <input type="submit" title="<?php echo $this->__('Add to Cart') ?>" class="addtocart" />
    </fieldset>
    </form>

     

    6. *to var_dump

    //Load var with product data :

    $_product = Mage::getModel('catalog/product')->loadByAttribute('sku',yourSKU);

    var_dump($_product);

    This will display options properties, you can also search the mysql db by the name of your option to get the ID.

     

    7. Using this i can correctly add products from CMS to cart with passing along a custom option in magento 1.9.

     


    Last Updated (Monday, 29 September 2014 22:30)

     
    Comments (1)
    Help
    1 Saturday, 15 November 2014 23:38
    I am a novice coder but i was able to follow your directions in place the code in the correct spots. how should this work for my site please help!

    Add your comment

    Your name:
    Your email:
    Your website:
    Subject:
    Comment: