Solved

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

Posted on 2014-09-28
2
4,916 Views
Last Modified: 2016-03-02
Hello Experts

I have a CMS page inside my magento 1.9 setup.
I have a virtual product (ID=17) with a dynamic custom options called "Name" (ID=3)
It has a recurring profile set to 1 year and period_frequency to 1
All i want is for that product to get its option "Name" filled from a variable inside the same .phtml file.
With a button that adds it directly to the shopping cart.

In this CMS page i call a block that links to an phtml page.
{{block type="core/template" name="domainCheck" template="catalog/product/domaincheck.phtml"}}


Using this code in the phtml i cannot add the product because of the required options that are not specified.
This is a virtual product with a recurring profile and 1 custom option called Name

$_product = Mage::getModel('catalog/product')->loadByAttribute('sku',27005);
$buy = Mage::helper('checkout/cart')->getAddUrl($_product);
 ?>
<?php $buttonTitle = $this->__('Add to Cart');?>
<?php if($_product->isSaleable()): ?>
    <div class="add-to-cart">
        <button type="button" title="<?php echo $buttonTitle ?>" id="product-addtocart-button" class="button btn-cart" onclick="parent.location='<?php echo $buy; ?>'"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
<br/>
<?php endif; ?>
<?php echo $buy ?>

Open in new window


$buy outputs the following:
mysite.com/checkout/cart/add/uenc/aHR0cDovL2RiaXRzLmJlL2tlYmFiMi9kb21laW5uYW1lbg,,/product/17/form_key/zigNa5gBINy5wIgl/

I have tried using all kind of query strings, with or without form keys. But nothing seems to be working.

The closest thing i can find on google is this url
magento.stackexchange.com/questions/14718/add-virtual-products-to-cart-with-custom-options-progrmmatically

Also using virtual products with recurring profiles. But with dropdown custom options (mine is a free text field)
So putting the code from the link above i get no result, nothing happens, and no errors are shown.

$proid = 17;

$_product = Mage::getModel('catalog/product')->load($proid);
$_product->setIsRecurring('1');
$_product->setRecurringProfile(array(
    'period_unit' => year,
    'period_frequency' => 1
));

$_product->save();
    $params = array(
        'product' => $proid, // This would be $product->getId()
        'qty' => 1,
);

$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($_product,$params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Open in new window


Help will be welcome, as i have no clue on how to continue to achieve what i want.

Kind Regards
0
Comment
Question by:Th0R
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
Th0R earned 0 total points
ID: 40351009
Well in the end this did what i needed

$params_arr = array('product'=>17,'qty'=>1,'options[3]'=> $domain);
$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() ?>" />

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

Open in new window

0
 

Author Closing Comment

by:Th0R
ID: 40351010
Just pasted the code in my phtml file and i can now add custom products directly to the cart in magento 1.9 from a CMS page
1

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question